From 1f19fed93b8a9f5a6b2c243a1c59e0fca2d57e84 Mon Sep 17 00:00:00 2001 From: Marcus Date: Thu, 25 Apr 2024 01:54:14 +0200 Subject: [PATCH] firmware wip - add simple /api/sensors which returns all values in json --- Arduino/CanGrow/CanGrow.ino | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/Arduino/CanGrow/CanGrow.ino b/Arduino/CanGrow/CanGrow.ino index 8a69198..47b517c 100644 --- a/Arduino/CanGrow/CanGrow.ino +++ b/Arduino/CanGrow/CanGrow.ino @@ -1065,6 +1065,8 @@ void loop() { // check if secondsToday is larger then secondsSunrise time AND if secondsToday is smaller then the sum of secondsSunrise + seconds of lightHours if( (secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60) ) )){ // turn on light + + // TODO write a LED control function which takes also takes care of UseLEDrelais analogWrite(PINled, PINled_PWM); } else { @@ -1129,6 +1131,8 @@ void WebHandler() { // switching MOSFETs webserver.on("/switch", HTTP_POST, POSTswitchMOSFET); + // api stuff + webserver.on("/api/sensors", HTTP_GET, APIgetSensors); } @@ -1913,8 +1917,32 @@ void POSTswitchMOSFET() { } } + + +/* + * API section + * + */ + +// return as json all sensor readings +void APIgetSensors() { + JsonDocument jsonSensors; + JsonArray arraySoilmoisture = jsonSensors["soilmoisture"].to(); + arraySoilmoisture.add(getSoilmoisture(1)); + arraySoilmoisture.add(getSoilmoisture(2)); + JsonArray arrayTemperature = jsonSensors["temperature"].to(); + arrayTemperature.add(getTemperature(1)); + arrayTemperature.add(getTemperature(2)); + jsonSensors["humidity"] = getHumidity(); + jsonSensors["chirpLight"] = getLightchirp(); + + String body; + serializeJsonPretty(jsonSensors, body); + webserver.send(200, "text/json", body); + +}