Ajout des capteurs
This commit is contained in:
@@ -1,13 +1,9 @@
|
|||||||
#include <SPI.h>
|
|
||||||
#include <Ethernet.h>
|
#include <Ethernet.h>
|
||||||
#include <SdFat.h>
|
#include <SdFat.h>
|
||||||
#include <String.h>
|
#include <OneWire.h>
|
||||||
|
|
||||||
byte mac[] = {
|
|
||||||
0xA8, 0x61, 0x0A, 0xAE, 0x6F, 0x4A
|
|
||||||
};
|
|
||||||
SdFat SD;
|
|
||||||
EthernetServer server(80);
|
EthernetServer server(80);
|
||||||
|
SdFat SD;
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
Ethernet.init(10);
|
Ethernet.init(10);
|
||||||
@@ -17,17 +13,15 @@ void setup() {
|
|||||||
}
|
}
|
||||||
Serial.println("Arduino Ethernet WebServer");
|
Serial.println("Arduino Ethernet WebServer");
|
||||||
|
|
||||||
Serial.print("Starting SD...");
|
|
||||||
if (!SD.begin(4)) {
|
if (!SD.begin(4)) {
|
||||||
Serial.println("failed");
|
Serial.println("Failed initialize SD Card");
|
||||||
while (true) {
|
while (true) {
|
||||||
delay(15);
|
delay(15);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else Serial.println("ok");
|
|
||||||
|
|
||||||
Serial.println("Initialize Ethernet with DHCP:");
|
Serial.println("Initialize Ethernet with DHCP:");
|
||||||
|
byte mac[] = { 0xA8, 0x61, 0x0A, 0xAE, 0x6F, 0x4A };
|
||||||
if (Ethernet.begin(mac) == 0) {
|
if (Ethernet.begin(mac) == 0) {
|
||||||
Serial.println("Failed to configure Ethernet using DHCP");
|
Serial.println("Failed to configure Ethernet using DHCP");
|
||||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||||
@@ -49,26 +43,33 @@ void setup() {
|
|||||||
void loop() {
|
void loop() {
|
||||||
EthernetClient client = server.available();
|
EthernetClient client = server.available();
|
||||||
if (client) {
|
if (client) {
|
||||||
SdFile file;
|
|
||||||
{
|
{
|
||||||
char* path;
|
char* path;
|
||||||
{
|
free(readStringUntil(client, ' '));
|
||||||
String res = client.readStringUntil(" HTTP/1.1");
|
path = readStringUntil(client, ' ');
|
||||||
char char_array[res.length() + 1];
|
|
||||||
res.toCharArray(char_array, res.length() + 1);
|
|
||||||
strtok(char_array, " ");
|
|
||||||
path = strtok(0, " ");
|
|
||||||
}
|
|
||||||
if (strcmp(path, "/") == 0) {
|
if (strcmp(path, "/") == 0) {
|
||||||
|
free(path);
|
||||||
path = "/index.html";
|
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);
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void sendFile(char* path, EthernetClient client) {
|
||||||
|
SdFile file;
|
||||||
if (!file.open(path)) {
|
if (!file.open(path)) {
|
||||||
sendNotFound(client);
|
sendNotFound(client);
|
||||||
delay(15);
|
|
||||||
client.stop();
|
client.stop();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
{
|
{
|
||||||
char buf[25];
|
char buf[25];
|
||||||
sendHeaders(client);
|
sendHeaders(client);
|
||||||
@@ -77,10 +78,24 @@ void loop() {
|
|||||||
file.read(buf, sizeof(buf));
|
file.read(buf, sizeof(buf));
|
||||||
client.write(buf, sizeof(buf));
|
client.write(buf, sizeof(buf));
|
||||||
}
|
}
|
||||||
delay(15);
|
file.close();
|
||||||
client.stop();
|
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) {
|
void sendHeaders(EthernetClient client) {
|
||||||
@@ -90,6 +105,12 @@ void sendHeaders(EthernetClient client) {
|
|||||||
client.println();
|
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) {
|
void sendNotFound(EthernetClient client) {
|
||||||
client.println("HTTP/1.1 404 Not Found");
|
client.println("HTTP/1.1 404 Not Found");
|
||||||
client.println("Content-Type: text/html");
|
client.println("Content-Type: text/html");
|
||||||
@@ -106,3 +127,67 @@ void sendNotFound(EthernetClient client) {
|
|||||||
client.println("</html>");
|
client.println("</html>");
|
||||||
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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
<h4 id="userName">
|
<h4 id="userName">
|
||||||
<img id="pictureUser" src="/assets/profile.png"/>
|
<img id="pictureUser" src="/p.png"/>
|
||||||
<span class="profile-name">
|
<span class="profile-name">
|
||||||
Aurélie
|
Aurélie
|
||||||
</span>
|
</span>
|
||||||
@@ -9,5 +9,8 @@
|
|||||||
</h4>
|
</h4>
|
||||||
|
|
||||||
<div class="container">
|
<div class="container">
|
||||||
<h1>Salut</h1>
|
<div *ngFor="let s of sensors">
|
||||||
|
<h1>{{ s.name }}</h1>
|
||||||
|
<div>{{ s.value }}°C</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { Component } from '@angular/core';
|
import { Component } from '@angular/core';
|
||||||
|
import { Sensor, SensorsService } from './sensors.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
@@ -7,4 +8,16 @@ import { Component } from '@angular/core';
|
|||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
title = 'arduino-interface';
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,14 +3,18 @@ import { BrowserModule } from '@angular/platform-browser';
|
|||||||
|
|
||||||
import { AppComponent } from './app.component';
|
import { AppComponent } from './app.component';
|
||||||
|
|
||||||
|
import {HttpClientModule} from "@angular/common/http";
|
||||||
|
import { SensorsService } from './sensors.service';
|
||||||
|
|
||||||
@NgModule({
|
@NgModule({
|
||||||
declarations: [
|
declarations: [
|
||||||
AppComponent
|
AppComponent
|
||||||
],
|
],
|
||||||
imports: [
|
imports: [
|
||||||
BrowserModule
|
BrowserModule,
|
||||||
|
HttpClientModule
|
||||||
],
|
],
|
||||||
providers: [],
|
providers: [SensorsService],
|
||||||
bootstrap: [AppComponent]
|
bootstrap: [AppComponent]
|
||||||
})
|
})
|
||||||
export class AppModule { }
|
export class AppModule { }
|
||||||
|
|||||||
16
src/app/sensors.service.spec.ts
Normal file
16
src/app/sensors.service.spec.ts
Normal file
@@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
20
src/app/sensors.service.ts
Normal file
20
src/app/sensors.service.ts
Normal file
@@ -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<Sensor[]> {
|
||||||
|
return this.http.get<Sensor[]>('/sensors').toPromise();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
Before Width: | Height: | Size: 116 KiB After Width: | Height: | Size: 116 KiB |
Reference in New Issue
Block a user