Check for NAN temperatures

This commit is contained in:
Ollo 2020-12-28 14:24:55 +01:00
parent 2ac72fcd33
commit a8837d3119
2 changed files with 59 additions and 38 deletions

View File

@ -99,5 +99,8 @@
#define SOLAR_DELTA_VOLT_ADC 3 #define SOLAR_DELTA_VOLT_ADC 3
#define LIPO_DELTA_VOLT_ADC 0.2 /**< trigger for lipo voltage */ #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 #endif

View File

@ -88,8 +88,8 @@ long nextBlink = 0; /**< Time needed in main loop to support expected bl
RunningMedian lipoRawSensor = RunningMedian(5); RunningMedian lipoRawSensor = RunningMedian(5);
RunningMedian solarRawSensor = RunningMedian(5); RunningMedian solarRawSensor = RunningMedian(5);
RunningMedian waterRawSensor = RunningMedian(5); RunningMedian waterRawSensor = RunningMedian(5);
RunningMedian lipoTempSensor = RunningMedian(5); RunningMedian lipoTempSensor = RunningMedian(TEMP_SENSOR_MEASURE_SERIES);
RunningMedian waterTempSensor = RunningMedian(5); RunningMedian waterTempSensor = RunningMedian(TEMP_SENSOR_MEASURE_SERIES);
OneWire oneWire(SENSOR_DS18B20); OneWire oneWire(SENSOR_DS18B20);
DallasTemperature sensors(&oneWire); DallasTemperature sensors(&oneWire);
@ -301,14 +301,18 @@ void mode2MQTT()
rtcWaterTempIndex = waterSensorIndex.get(); rtcWaterTempIndex = waterSensorIndex.get();
float lipoTempCurrent = lipoTempSensor.getMedian(); float lipoTempCurrent = lipoTempSensor.getMedian();
if (lipoTempCurrent != NAN)
if (! isnan(lipoTempCurrent))
{ {
sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent)); sensorTemp.setProperty(TEMPERATUR_SENSOR_LIPO).send(String(lipoTempCurrent));
Serial << "Lipo Temperatur " << lipoTempCurrent << " °C " << endl;
} }
float t2 = waterTempSensor.getMedian(); float t2 = waterTempSensor.getMedian();
if (t2 != NAN) if (! isnan(t2))
{ {
sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2)); sensorTemp.setProperty(TEMPERATUR_SENSOR_WATER).send(String(t2));
Serial << "Water Temperatur " << lipoTempCurrent << " °C " << endl;
} }
//give mqtt time, use via publish callback instead? //give mqtt time, use via publish callback instead?
@ -430,6 +434,49 @@ void readDistance()
} }
} }
/**
* @brief read all temperatur sensors
*
* @return int
* <code>0</code> device can sleep, no change in the temperatures
* <code>1</code> 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. * @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
* These sensors (ADC2) can only be read when no Wifi is used. * These sensors (ADC2) can only be read when no Wifi is used.
@ -437,8 +484,8 @@ void readDistance()
bool readSensors() bool readSensors()
{ {
bool leaveMode1 = false; bool leaveMode1 = false;
int timeoutTemp = millis() + TEMPERATUR_TIMEOUT;
int sensorCount = 0; int sensorCount = 0;
int timeoutTemp = millis() + 2000;
Serial << "Read Sensors" << endl; Serial << "Read Sensors" << endl;
@ -490,6 +537,7 @@ bool readSensors()
rtcLastBatteryVoltage = getBatteryVoltage(); rtcLastBatteryVoltage = getBatteryVoltage();
rtcLastSolarVoltage = getSolarVoltage(); rtcLastSolarVoltage = getSolarVoltage();
/* Required to read the temperature at least once */
while (sensorCount == 0 && millis() < timeoutTemp) while (sensorCount == 0 && millis() < timeoutTemp)
{ {
sensors.begin(); sensors.begin();
@ -507,38 +555,8 @@ bool readSensors()
/* Read the distance and give the temperature sensors some time */ /* Read the distance and give the temperature sensors some time */
readDistance(); readDistance();
Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl; Serial << "Distance sensor " << waterRawSensor.getAverage() << " cm" << endl;
leaveMode1 |= readTemp();
/* 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);
}
for (int i = 0; i < sensorCount; i++) for (int i = 0; i < sensorCount; i++)
{ {
Serial << "OnwWire sensor " << i << " has value " << sensors.getTempCByIndex(i) << endl; Serial << "OnwWire sensor " << i << " has value " << sensors.getTempCByIndex(i) << endl;