From 48d8ca4ac2b47fff7a5b111888ea624277fc1197 Mon Sep 17 00:00:00 2001 From: Youen Toupin Date: Tue, 15 Mar 2022 23:23:18 +0100 Subject: [PATCH] test code that reads a voltage and displays it in the web browser --- ESP32/vehicle-monitor.cpp | 12 ++++++++++-- WebApp/src/main-page.tsx | 27 ++++++++++++++++++++++++++- WebApp/src/monitor-api.ts | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 WebApp/src/monitor-api.ts diff --git a/ESP32/vehicle-monitor.cpp b/ESP32/vehicle-monitor.cpp index bd88526..3523be8 100644 --- a/ESP32/vehicle-monitor.cpp +++ b/ESP32/vehicle-monitor.cpp @@ -9,6 +9,8 @@ AsyncWebServer server(80); +int16_t batteryVoltage = -1; //in mV + void setup() { Serial.begin(115200); @@ -31,9 +33,9 @@ void setup() Serial.println(WiFi.localIP()); server.on("/api/status", HTTP_GET, [](AsyncWebServerRequest *request){ - int batteryVoltage = random(30000, 42000); + int v = batteryVoltage; char json[128]; - sprintf(json, "{\"v\":%d}", batteryVoltage); + sprintf(json, "{\"v\":%d,\"c\":1000}", v); request->send(200, "text/json", json); }); @@ -50,5 +52,11 @@ void setup() void loop() { + const int potPin = 34; + delay(1000); + int16_t analogV = analogRead(potPin); + float v = (float)analogV / 4096.0f * 3.3f; + batteryVoltage = (int16_t)(v * 1000.0f + 0.5f); + //Serial.println(potValue); } diff --git a/WebApp/src/main-page.tsx b/WebApp/src/main-page.tsx index 0dab169..92dd795 100644 --- a/WebApp/src/main-page.tsx +++ b/WebApp/src/main-page.tsx @@ -1,7 +1,32 @@ import m from 'mithril'; +import { MonitorApi, Status } from './monitor-api'; export default class MainPage { + api = new MonitorApi(false); + status?: Status; + autoRefresh = true; + + oninit() { + this.refresh(); + } + + onbeforeremove() { + this.autoRefresh = false; + } + + async refresh() { + this.status = await this.api.getStatus(); + if(this.autoRefresh) + setTimeout(() => { if(this.autoRefresh) this.refresh(); }, 500); + } + view() { - return

Hello, world!

+ return this.status + ?
+

Tension batterie : {this.status.batteryVoltage.toFixed(1)}V

+

Courant : {this.status.motorCurrent.toFixed(1)}A

+

Puissance : {(this.status.batteryVoltage * this.status.motorCurrent).toFixed(1)}W

+
+ :

Chargement...

; } } \ No newline at end of file diff --git a/WebApp/src/monitor-api.ts b/WebApp/src/monitor-api.ts new file mode 100644 index 0000000..884ee9d --- /dev/null +++ b/WebApp/src/monitor-api.ts @@ -0,0 +1,39 @@ +import m from 'mithril'; + +export interface Status { + batteryVoltage: number; + motorCurrent: number; +}; + +interface ApiStatus { + v: number; + c: number; +} + +export class MonitorApi { + constructor(private mockServer: boolean) { + } + + async getStatus(): Promise { + let apiStatus: ApiStatus; + + if(this.mockServer) { + await new Promise(resolve => setTimeout(resolve, 200)); + apiStatus = { + v: Math.random() * 20000 + 20000, + c: Math.random() * 30000 + } + setTimeout(() => m.redraw(), 0); + } else { + apiStatus = await m.request({ + method: "GET", + url: "/api/status" + }); + } + + return { + batteryVoltage: apiStatus.v / 1000, + motorCurrent: apiStatus.c / 1000 + }; + } +}