diff --git a/arduino/main/main.ino b/arduino/main/main.ino index 628f9f5..6dab3e8 100644 --- a/arduino/main/main.ino +++ b/arduino/main/main.ino @@ -1,13 +1,9 @@ -#include #include #include -#include +#include -byte mac[] = { - 0xA8, 0x61, 0x0A, 0xAE, 0x6F, 0x4A -}; -SdFat SD; EthernetServer server(80); +SdFat SD; void setup() { Ethernet.init(10); @@ -17,17 +13,15 @@ void setup() { } Serial.println("Arduino Ethernet WebServer"); - Serial.print("Starting SD..."); if (!SD.begin(4)) { - Serial.println("failed"); + Serial.println("Failed initialize SD Card"); while (true) { delay(15); } } - else Serial.println("ok"); Serial.println("Initialize Ethernet with DHCP:"); - + byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x6F, 0x4A }; if (Ethernet.begin(mac) == 0) { Serial.println("Failed to configure Ethernet using DHCP"); if (Ethernet.hardwareStatus() == EthernetNoHardware) { @@ -49,40 +43,61 @@ void setup() { void loop() { EthernetClient client = server.available(); if (client) { - SdFile file; { char* path; - { - String res = client.readStringUntil(" HTTP/1.1"); - char char_array[res.length() + 1]; - res.toCharArray(char_array, res.length() + 1); - strtok(char_array, " "); - path = strtok(0, " "); - } + free(readStringUntil(client, ' ')); + path = readStringUntil(client, ' '); if (strcmp(path, "/") == 0) { + free(path); path = "/index.html"; + sendFile(path, client); + } else if (strcmp(path, "/sensors") == 0) { + free(path); + sendHeaders(client); + sendSensors(client); + } else { + sendFile(path, client); + free(path); } - Serial.println(path); - if (!file.open(path)) { - sendNotFound(client); - delay(15); - client.stop(); - } - } - { - char buf[25]; - sendHeaders(client); - while (file.available()) - { - file.read(buf, sizeof(buf)); - client.write(buf, sizeof(buf)); - } - delay(15); - client.stop(); + } } } +void sendFile(char* path, EthernetClient client) { + SdFile file; + if (!file.open(path)) { + sendNotFound(client); + client.stop(); + } + { + char buf[25]; + sendHeaders(client); + while (file.available()) + { + file.read(buf, sizeof(buf)); + client.write(buf, sizeof(buf)); + } + file.close(); + client.stop(); + } +} + +void sendSensors(EthernetClient client) { + float temperature; + if (getTemperature(&temperature, true) != 0) { + client.print("[{ \"name\": \"Room temperature\", \"value\": "); + client.print(0); + client.print("}]"); + client.stop(); + return; + } + client.print("[{ \"name\": \"Room temperature\", \"value\": "); + client.print(temperature, 2); + client.print("}]"); + client.stop(); +} + void sendHeaders(EthernetClient client) { client.println("HTTP/1.1 200 OK"); client.println("Cache-Control: max-age=31536000"); @@ -90,6 +105,12 @@ void sendHeaders(EthernetClient client) { client.println(); } +void sendInternalServerError(EthernetClient client) { + client.println("HTTP/1.1 500 Internal Server Error"); + client.println("Connection: close"); + client.println(); +} + void sendNotFound(EthernetClient client) { client.println("HTTP/1.1 404 Not Found"); client.println("Content-Type: text/html"); @@ -106,3 +127,67 @@ void sendNotFound(EthernetClient client) { client.println(""); client.println(); } + +byte getTemperature(float *temperature, byte reset_search) { + OneWire ds(2); + byte data[9], addr[8]; + // data[] : Données lues depuis le scratchpad + // addr[] : Adresse du module 1-Wire détecté + + if (reset_search) { + ds.reset_search(); + } + + if (!ds.search(addr)) { + // Pas de capteur + return 1; + } + + if (OneWire::crc8(addr, 7) != addr[7]) { + // Adresse invalide + return 2; + } + + if (addr[0] != 0x28) { + // Mauvais type de capteur + return 3; + } + + ds.reset(); + ds.select(addr); + + ds.write(0x44, 1); + delay(800); + + ds.reset(); + ds.select(addr); + ds.write(0xBE); + + for (byte i = 0; i < 9; i++) { + data[i] = ds.read(); + } + + *temperature = (int16_t) ((data[1] << 8) | data[0]) * 0.0625; + + // Pas d'erreur + return 0; +} + +char* readStringUntil(EthernetClient client, char c) { + char last = '\0'; + char* result = malloc(sizeof(char) * 10); + for (byte i = 0; i < 9; i++) { + result[i] = ' '; + } + result[9] = '\0'; + byte i = 0; + while (last != c && i < 9) { + last = client.read(); + if (last != c) { + result[i] = last; + i++; + } + } + result[i] = '\0'; + return result; +} diff --git a/src/app/app.component.html b/src/app/app.component.html index c4cac6a..d867658 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1,5 +1,5 @@

- + Aurélie @@ -9,5 +9,8 @@

-

Salut

+
+

{{ s.name }}

+
{{ s.value }}°C
+
diff --git a/src/app/app.component.ts b/src/app/app.component.ts index e1aebdc..34de7a0 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,4 +1,5 @@ import { Component } from '@angular/core'; +import { Sensor, SensorsService } from './sensors.service'; @Component({ selector: 'app-root', @@ -7,4 +8,16 @@ import { Component } from '@angular/core'; }) export class AppComponent { title = 'arduino-interface'; + + sensors: Sensor[] = [] + + constructor(private sensorsService: SensorsService) { + setTimeout(this.getValues, 0, this); + } + + getValues(component: AppComponent) { + component.sensorsService.getSensors().then((val) => component.sensors = val); + setTimeout(component.getValues, 1000, component); + } + } diff --git a/src/app/app.module.ts b/src/app/app.module.ts index 8dfc1d6..04a2f89 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -3,14 +3,18 @@ import { BrowserModule } from '@angular/platform-browser'; import { AppComponent } from './app.component'; +import {HttpClientModule} from "@angular/common/http"; +import { SensorsService } from './sensors.service'; + @NgModule({ declarations: [ AppComponent ], imports: [ - BrowserModule + BrowserModule, + HttpClientModule ], - providers: [], + providers: [SensorsService], bootstrap: [AppComponent] }) export class AppModule { } diff --git a/src/app/sensors.service.spec.ts b/src/app/sensors.service.spec.ts new file mode 100644 index 0000000..d44793c --- /dev/null +++ b/src/app/sensors.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { SensorsService } from './sensors.service'; + +describe('SensorsService', () => { + let service: SensorsService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(SensorsService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/sensors.service.ts b/src/app/sensors.service.ts new file mode 100644 index 0000000..57567d4 --- /dev/null +++ b/src/app/sensors.service.ts @@ -0,0 +1,20 @@ +import { Injectable } from '@angular/core'; +import { HttpClient } from '@angular/common/http'; + +export class Sensor { + name!: string; + value!: number; +} + +@Injectable({ + providedIn: 'root' +}) +export class SensorsService { + + constructor(private http: HttpClient) { } + + public async getSensors(): Promise { + return this.http.get('/sensors').toPromise(); + } + +} diff --git a/src/assets/profile.png b/src/assets/p.png similarity index 100% rename from src/assets/profile.png rename to src/assets/p.png diff --git a/src/p.png b/src/p.png new file mode 100644 index 0000000..1189a8e Binary files /dev/null and b/src/p.png differ