From f8629586f863c45aa032bc9f96f5732871106833 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 24 Feb 2024 16:21:39 +0100 Subject: [PATCH] Calculate water percentage below 30 percent --- esp32/src/main.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index e58c320..93718e0 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -1067,9 +1067,21 @@ void plantcontrol() if (!isnan(waterRawSensor.getAverage())) { /* measuring the distance from top -> smaller value means more water: */ - sensorWater.setProperty("remaining").send(String(100.0 * (waterRawSensor.getAverage()/ESP_ADC_MAX))); + long averageWater = waterRawSensor.getAverage(); + if (averageWater < ESP_ADC_MAX) { + long percentage = (averageWater * averageWater); + percentage = percentage / 570000; + /* Add offset */ + if (percentage > 0) + { + percentage += 4; + } + sensorWater.setProperty("remaining").send(String(percentage)); + } else { + sensorWater.setProperty("remaining").send(String("100")); + } - sensorWater.setProperty("distance").send(String(waterRawSensor.getAverage())); + sensorWater.setProperty("raw").send(String(waterRawSensor.getAverage())); } } sensorLipo.setProperty("percent").send(String(100 * mBatteryVoltage / VOLT_MAX_BATT));