From a8837d311991716b508599e091e1db3fd51fc0a1 Mon Sep 17 00:00:00 2001 From: Ollo Date: Mon, 28 Dec 2020 14:24:55 +0100 Subject: [PATCH] Check for NAN temperatures --- esp32/include/ControllerConfiguration.h | 3 + esp32/src/main.cpp | 94 +++++++++++++++---------- 2 files changed, 59 insertions(+), 38 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index d4cf226..85db895 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -99,5 +99,8 @@ #define SOLAR_DELTA_VOLT_ADC 3 #define LIPO_DELTA_VOLT_ADC 0.2 /**< trigger for lipo voltage */ +#define TEMPERATUR_TIMEOUT 3000 /**< 3 Seconds timeout for the temperatur sensors */ +#define TEMP_SENSOR_MEASURE_SERIES 5 + /* @} */ #endif \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index e494e09..21e2c22 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -88,8 +88,8 @@ long nextBlink = 0; /**< Time needed in main loop to support expected bl RunningMedian lipoRawSensor = RunningMedian(5); RunningMedian solarRawSensor = RunningMedian(5); RunningMedian waterRawSensor = RunningMedian(5); -RunningMedian lipoTempSensor = RunningMedian(5); -RunningMedian waterTempSensor = RunningMedian(5); +RunningMedian lipoTempSensor = RunningMedian(TEMP_SENSOR_MEASURE_SERIES); +RunningMedian waterTempSensor = RunningMedian(TEMP_SENSOR_MEASURE_SERIES); OneWire oneWire(SENSOR_DS18B20); DallasTemperature sensors(&oneWire); @@ -301,14 +301,18 @@ void mode2MQTT() rtcWaterTempIndex = waterSensorIndex.get(); float lipoTempCurrent = lipoTempSensor.getMedian(); - if (lipoTempCurrent != NAN) + + if (! isnan(lipoTempCurrent)) { sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent)); + Serial << "Lipo Temperatur " << lipoTempCurrent << " °C " << endl; } + float t2 = waterTempSensor.getMedian(); - if (t2 != NAN) + if (! isnan(t2)) { sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2)); + Serial << "Water Temperatur " << lipoTempCurrent << " °C " << endl; } //give mqtt time, use via publish callback instead? @@ -430,6 +434,49 @@ void readDistance() } } +/** + * @brief read all temperatur sensors + * + * @return int + * 0 device can sleep, no change in the temperatures + * 1 something changed and the temperatures shall be published via MQTT + */ +int readTemp() { + int readAgain = TEMP_SENSOR_MEASURE_SERIES; + int sensorCount = 0; + int leaveMode1 = 0; + while (readAgain > 0) + { + sensors.requestTemperatures(); + if (sensorCount > 0) + { + if (rtcLipoTempIndex != -1) + { + float temp1Raw = sensors.getTempCByIndex(rtcLipoTempIndex); + Serial << "lipoTempCurrent: " << temp1Raw << endl; + lipoTempSensor.add(temp1Raw); + } + else + { + Serial << "missing lipotemp, proceed to mode2: " << endl; + leaveMode1 = 1; + readAgain = 0; + wakeUpReason = WAKEUP_REASON_RTC_MISSING; + } + } + if (sensorCount > 1 && rtcWaterTempIndex != -1) + { + float temp2Raw = sensors.getTempCByIndex(rtcWaterTempIndex); + Serial << "waterTempCurrent: " << temp2Raw << endl; + waterTempSensor.add(temp2Raw); + } + + readAgain--; + delay(50); + } + return leaveMode1; +} + /** * @brief Sensors, that are connected to GPIOs, mandatory for WIFI. * These sensors (ADC2) can only be read when no Wifi is used. @@ -437,9 +484,9 @@ void readDistance() bool readSensors() { bool leaveMode1 = false; + int timeoutTemp = millis() + TEMPERATUR_TIMEOUT; int sensorCount = 0; - int timeoutTemp = millis() + 2000; - + Serial << "Read Sensors" << endl; readSystemSensors(); @@ -490,6 +537,7 @@ bool readSensors() rtcLastBatteryVoltage = getBatteryVoltage(); rtcLastSolarVoltage = getSolarVoltage(); + /* Required to read the temperature at least once */ while (sensorCount == 0 && millis() < timeoutTemp) { sensors.begin(); @@ -507,38 +555,8 @@ bool readSensors() /* Read the distance and give the temperature sensors some time */ readDistance(); Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl; - - /* Required to read the temperature once */ - int readAgain = 5; - while (readAgain > 0) - { - sensors.requestTemperatures(); - if (sensorCount > 0) - { - if (rtcLipoTempIndex != -1) - { - float temp1Raw = sensors.getTempCByIndex(rtcLipoTempIndex); - Serial << "lipoTempCurrent: " << temp1Raw << endl; - lipoTempSensor.add(temp1Raw); - } - else - { - Serial << "missing lipotemp, proceed to mode2: " << endl; - leaveMode1 = 1; - readAgain = 0; - wakeUpReason = WAKEUP_REASON_RTC_MISSING; - } - } - if (sensorCount > 1 && rtcWaterTempIndex != -1) - { - float temp2Raw = sensors.getTempCByIndex(rtcWaterTempIndex); - Serial << "waterTempCurrent: " << temp2Raw << endl; - waterTempSensor.add(temp2Raw); - } - - readAgain--; - delay(50); - } + leaveMode1 |= readTemp(); + for (int i = 0; i < sensorCount; i++) { Serial << "OnwWire sensor " << i << " has value " << sensors.getTempCByIndex(i) << endl;