New HTTP endpoints: /header and /datasets

This commit is contained in:
Ollo 2021-12-16 20:40:44 +01:00
parent 2b930a6902
commit 5955ca1a5c
2 changed files with 47 additions and 3 deletions

View File

@ -47,7 +47,7 @@
// Test chart // Test chart
const ctx = document.getElementById('myChart'); const ctx = document.getElementById('myChart');
const myChart = new Chart(ctx, { const myChart = new Chart(ctx, {
type: 'bar', type: 'line',
data: { data: {
labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'], labels: ['Red', 'Blue', 'Yellow', 'Green', 'Purple', 'Orange'],
datasets: [{ datasets: [{
@ -127,6 +127,6 @@
</tr> </tr>
</tbody> </tbody>
</table> </table>
<canvas id="myChart" width="400" height="400"></canvas> <canvas id="myChart" width="100%" height="400"></canvas>
</body> </body>
</html> </html>

View File

@ -340,6 +340,41 @@ String sensorAsJSON(void) {
return buffer; 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 * @brief Main loop, triggered by the Homie API
* All logic needs to be done here. * 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 */ /* Clean cycles buttons */
if ((!mConnectedNonMQTT) && (mButtonPressed <= BUTTON_MIN_ACTION_CYCLE)) { if ((!mConnectedNonMQTT) && (mButtonPressed <= BUTTON_MIN_ACTION_CYCLE)) {
buttonNode.setProperty(NODE_BUTTON).send("0"); buttonNode.setProperty(NODE_BUTTON).send("0");
@ -535,7 +573,13 @@ void setup()
request->send(200, "text/plain", String(ESP.getFreeHeap())); request->send(200, "text/plain", String(ESP.getFreeHeap()));
}); });
mHttp->on("/sensors", HTTP_GET, [](AsyncWebServerRequest *request){ 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->serveStatic("/", SPIFFS, "/").setDefaultFile("index.htm");
mHttp->begin(); mHttp->begin();