From 704684d4374d2deb7a441026c36715640f2d0533 Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 24 Mar 2023 18:06:21 +0100 Subject: [PATCH] Fixed water distance and publish percentage --- esp32/src/main.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 890391d..1482d48 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -1040,7 +1040,7 @@ void plantcontrol() readOneWireSensors(); - Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl; + Serial << "W : " << waterRawSensor.getAverage() << " mm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << " mm left)" << endl; float batteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY); float chipTemp = battery.getTemperature(); @@ -1048,14 +1048,19 @@ void plantcontrol() if (aliveWasRead()) { - float remaining = waterLevelMax.get() - waterRawSensor.getAverage(); - if (!isnan(remaining)) + /* Publish water values, if available */ + if (waterRawSensor.getCount() > 0) { - sensorWater.setProperty("remaining").send(String(remaining)); - } - if (!isnan(waterRawSensor.getAverage())) - { - sensorWater.setProperty("distance").send(String(waterRawSensor.getAverage())); + float remaining = (waterLevelMax.get() - waterRawSensor.getAverage()); + if (!isnan(remaining)) + { + /* measuring the distance from top -> smaller value means more water: */ + sensorWater.setProperty("remaining").send(String(100.0 - (remaining/100))); + } + if (!isnan(waterRawSensor.getAverage())) + { + sensorWater.setProperty("distance").send(String(waterRawSensor.getAverage())); + } } sensorLipo.setProperty("percent").send(String(100 * batteryVoltage / VOLT_MAX_BATT)); sensorLipo.setProperty("volt").send(String(batteryVoltage));