From 5955ca1a5cae0ef3b819593a80f5382fb1e118bc Mon Sep 17 00:00:00 2001 From: Ollo Date: Thu, 16 Dec 2021 20:40:44 +0100 Subject: [PATCH] New HTTP endpoints: /header and /datasets --- data/index.htm | 4 ++-- src/main.cpp | 46 +++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/data/index.htm b/data/index.htm index e3606a9..eb02e1a 100644 --- a/data/index.htm +++ b/data/index.htm @@ -47,7 +47,7 @@ // Test chart const ctx = document.getElementById('myChart'); const myChart = new Chart(ctx, { - type: 'bar', + type: 'line', data: { labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], datasets: [{ @@ -127,6 +127,6 @@ - + diff --git a/src/main.cpp b/src/main.cpp index 83bd5e8..1d44fc6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -340,6 +340,41 @@ String sensorAsJSON(void) { return buffer; } +/** + * @brief Generate JSON with all available sensors + * + * @return String + */ +String sensorHeader(void) { + String buffer; + StaticJsonDocument<500> doc; + doc.add("milli"); + doc.add("temp"); +#ifdef BME680 + doc.add("gas"); + doc.add("humidity"); +#endif + doc.add("altitude"); + doc.add("pressure"); + doc.add("particle"); + serializeJson(doc, buffer); + return buffer; +} + +/** + * @brief Generate JSON with all sensor values + * Array of JSON with all measured elements until now + * + * @return String + */ +String sensorDatasets(void) { + String buffer; + StaticJsonDocument<500> doc; + doc["count"] = String(mMeasureIndex); + serializeJson(doc, buffer); + return buffer; +} + /** * @brief Main loop, triggered by the Homie API * All logic needs to be done here. @@ -380,6 +415,9 @@ void loopHandler() } } + // FIXME: add the measured data into the big list + mMeasureIndex++; + /* Clean cycles buttons */ if ((!mConnectedNonMQTT) && (mButtonPressed <= BUTTON_MIN_ACTION_CYCLE)) { buttonNode.setProperty(NODE_BUTTON).send("0"); @@ -535,7 +573,13 @@ void setup() request->send(200, "text/plain", String(ESP.getFreeHeap())); }); mHttp->on("/sensors", HTTP_GET, [](AsyncWebServerRequest *request){ - request->send(200, "text/plain", sensorAsJSON()); + request->send(200, "application/json", sensorAsJSON()); + }); + mHttp->on("/header", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "application/json", sensorHeader()); + }); + mHttp->on("/datasets", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "application/json", sensorDatasets()); }); mHttp->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm"); mHttp->begin();