diff --git a/solarpump/temperature.ino b/solarpump/temperature.ino index 33a9a52e4714615917c33086ee7cf6bee41252c9..35534d4360556fc03ad778dc4af4057767ee95ef 100644 --- a/solarpump/temperature.ino +++ b/solarpump/temperature.ino @@ -6,7 +6,7 @@ /* No new valid temperature for < 60 seconds? I sleep. * No new valid temperature for > 60 seconds? REAL PROBLEM! */ -const uint32_t ERRORTIME = 60000; +const uint32_t ERRORTIME = 60000; // Update the temperature only every second const uint32_t TEMPUPDATETIME = 1000; @@ -14,158 +14,117 @@ const uint32_t TEMPUPDATETIME = 1000; static OneWire oneWireT1up(T1UP); static DallasTemperature dallasTemps(&oneWireT1up); UPDATESTATE t1up(float* temp) { - static uint32_t prevMillis = 0; - static uint32_t errorStartMillis = 0; - uint32_t curMillis = millis(); - if (curMillis - prevMillis < TEMPUPDATETIME) { - return UNCHANGED; - } - dallasTemps.requestTemperatures(); - float tempC = dallasTemps.getTempCByIndex(0); - if (tempC == DEVICE_DISCONNECTED_C) { - if (errorStartMillis) { - if (curMillis - errorStartMillis > ERRORTIME) { - return LONG_TIME_ERROR; - } - } else { - errorStartMillis = curMillis; - } - return ERROR; - } + static uint32_t prevMillis = 0; + static uint32_t errorStartMillis = 0; + uint32_t curMillis = millis(); + if (curMillis - prevMillis < TEMPUPDATETIME) { + return UNCHANGED; + } + dallasTemps.requestTemperatures(); + float tempC = dallasTemps.getTempCByIndex(0); + if (tempC == DEVICE_DISCONNECTED_C) { + if (errorStartMillis) { + if (curMillis - errorStartMillis > ERRORTIME) { + return LONG_TIME_ERROR; + } + } else { + errorStartMillis = curMillis; + } + return ERROR; + } - *temp = tempC; - errorStartMillis = 0; - prevMillis = curMillis; - return SUCCESS; -} - -struct temperatureSensor -{ - float tempValue; - uint32_t prevTime; - uint32_t currentTime; - uint32_t errorTime; - DHT sensorClass; -}; - -temperatureSensor t1down{ 0.0, 0,0 ,0, DHT(T1DOWN, DHT11) }; -temperatureSensor t2up{ 0.0, 0,0 ,0, DHT(T2UP, DHT11) }; -temperatureSensor t2down{ 0.0, 0,0 ,0, DHT(T2DOWN, DHT11) }; - -UPDATESTATE temperatureSensor(temperatureSensor& sensor) -{ - sensor.currentTime = millis(); - if (sensor.currentTime - sensor.prevTime < TEMPUPDATETIME) - { - return UNCHANGED; - } - float tmpTemperature = sensor.sensorClass.readTemperature(); - if (isnan(tmpTemperature)) - { - if(sensor.errorTime) - { - if (sensor.currentTime - sensor.errorTime > ERRORTIME) - { - return LONG_TIME_ERROR; - } - } - else - { - return ERROR; - } - } - - sensor.tempValue = tmpTemperature; - sensor.errorTime = 0; - sensor.prevTime = sensor.currentTime; - return SUCCESS; + *temp = tempC; + errorStartMillis = 0; + prevMillis = curMillis; + return SUCCESS; } // DOWN temperature for solar system 1 static DHT dhtT1down(T1DOWN, DHT11); UPDATESTATE t1down(float* temp) { - static uint32_t prevMillis = 0; - static uint32_t errorStartMillis = 0; - uint32_t curMillis = millis(); - if (curMillis - prevMillis < TEMPUPDATETIME) { - return UNCHANGED; - } - float tempC = dhtT1down.readTemperature(); - if (isnan(tempC)) { - if (errorStartMillis) { - if (curMillis - errorStartMillis > ERRORTIME) { - return LONG_TIME_ERROR; - } - } else { - errorStartMillis = curMillis; - } - return ERROR; - } + static uint32_t prevMillis = 0; + static uint32_t errorStartMillis = 0; + uint32_t curMillis = millis(); + if (curMillis - prevMillis < TEMPUPDATETIME) { + return UNCHANGED; + } + float tempC = dhtT1down.readTemperature(); + if (isnan(tempC)) { + if (errorStartMillis) { + if (curMillis - errorStartMillis > ERRORTIME) { + return LONG_TIME_ERROR; + } + } else { + errorStartMillis = curMillis; + } + return ERROR; + } - *temp = tempC; - errorStartMillis = 0; - prevMillis = curMillis; - return SUCCESS; + *temp = tempC; + errorStartMillis = 0; + prevMillis = curMillis; + return SUCCESS; } // UP temperature for solar system 2 static DHT dhtT2up(T2UP, DHT11); UPDATESTATE t2up(float* temp) { - static uint32_t prevMillis = 0; - static uint32_t errorStartMillis = 0; - uint32_t curMillis = millis(); - if (curMillis - prevMillis < TEMPUPDATETIME) { - return UNCHANGED; - } - float tempC = dhtT2up.readTemperature(); - if (isnan(tempC)) { - if (errorStartMillis) { - if (curMillis - errorStartMillis > ERRORTIME) { - return LONG_TIME_ERROR; - } - } else { - errorStartMillis = curMillis; - } - return ERROR; - } + static uint32_t prevMillis = 0; + static uint32_t errorStartMillis = 0; + uint32_t curMillis = millis(); + if (curMillis - prevMillis < TEMPUPDATETIME) { + return UNCHANGED; + } + float tempC = dhtT2up.readTemperature(); + if (isnan(tempC)) { + if (errorStartMillis) { + if (curMillis - errorStartMillis > ERRORTIME) { + return LONG_TIME_ERROR; + } + } else { + errorStartMillis = curMillis; + } + return ERROR; + } - *temp = tempC; - errorStartMillis = 0; - prevMillis = curMillis; - return SUCCESS; + *temp = tempC; + errorStartMillis = 0; + prevMillis = curMillis; + return SUCCESS; } // DOWN temperature for solar system 2 static DHT dhtT2down(T2DOWN, DHT11); UPDATESTATE t2down(float* temp) { - static uint32_t prevMillis = 0; - static uint32_t errorStartMillis = 0; - uint32_t curMillis = millis(); - if (curMillis - prevMillis < TEMPUPDATETIME) { - return UNCHANGED; - } - float tempC = dhtT2down.readTemperature(); - if (isnan(tempC)) { - if (errorStartMillis) { - if (curMillis - errorStartMillis > ERRORTIME) { - return LONG_TIME_ERROR; - } - } else { - errorStartMillis = curMillis; - } - return ERROR; - } + static uint32_t prevMillis = 0; + static uint32_t errorStartMillis = 0; + uint32_t curMillis = millis(); + if (curMillis - prevMillis < TEMPUPDATETIME) { + return UNCHANGED; + } + float tempC = dhtT2down.readTemperature(); + if (isnan(tempC)) { + if (errorStartMillis) { + if (curMillis - errorStartMillis > ERRORTIME) { + return LONG_TIME_ERROR; + } + } else { + errorStartMillis = curMillis; + } + return ERROR; + } - *temp = tempC; - errorStartMillis = 0; - prevMillis = curMillis; - return SUCCESS; + *temp = tempC; + errorStartMillis = 0; + prevMillis = curMillis; + return SUCCESS; } // start the relevant libraries void temperatureBegin(void) { - dallasTemps.begin(); - dhtT1down.begin(); - dhtT2up.begin(); - dhtT2down.begin(); -} \ No newline at end of file + dallasTemps.begin(); + dhtT1down.begin(); + dhtT2up.begin(); + dhtT2down.begin(); +} +