firmware wip - add simple /api/sensors which returns all values in json

This commit is contained in:
Marcus 2024-04-25 01:54:14 +02:00
parent 01b8043569
commit 1f19fed93b

View file

@ -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 // 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) ) )){ if( (secondsToday >= secondsSunrise) && (secondsToday <= ( secondsSunrise + (lightHours * 60 * 60) ) )){
// turn on light // turn on light
// TODO write a LED control function which takes also takes care of UseLEDrelais // TODO write a LED control function which takes also takes care of UseLEDrelais
analogWrite(PINled, PINled_PWM); analogWrite(PINled, PINled_PWM);
} else { } else {
@ -1129,6 +1131,8 @@ void WebHandler() {
// switching MOSFETs // switching MOSFETs
webserver.on("/switch", HTTP_POST, POSTswitchMOSFET); 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<JsonArray>();
arraySoilmoisture.add(getSoilmoisture(1));
arraySoilmoisture.add(getSoilmoisture(2));
JsonArray arrayTemperature = jsonSensors["temperature"].to<JsonArray>();
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);
}