diff --git a/esp32/src/DS18B20.cpp b/esp32/src/DS18B20.cpp index dbe8814..73c52cf 100644 --- a/esp32/src/DS18B20.cpp +++ b/esp32/src/DS18B20.cpp @@ -36,21 +36,15 @@ int Ds18B20::readAllTemperatures(float* pTemperatures, int maxTemperatures) { byte addr[8]; uint8_t scratchPad[SCRATCHPADSIZE]; int currentTemp = 0; -#ifdef DS_DEBUG - int i; -#endif while (this->mDs->search(addr)) { -#ifdef DS_DEBUG - Serial.print(" ROM ="); - for (i = 0; i < 8; i++) { - Serial.write(' '); - Serial.print(addr[i], HEX); - } -#endif this->mDs->reset(); this->mDs->select(addr); this->mDs->write(STARTCONV); + } + delay(750); + + while (this->mDs->search(addr)) { this->mDs->reset(); this->mDs->select(addr); this->mDs->write(READSCRATCH); @@ -68,14 +62,6 @@ int Ds18B20::readAllTemperatures(float* pTemperatures, int maxTemperatures) { // byte 7: DS18S20: COUNT_PER_C // DS18B20 & DS1822: store for crc // byte 8: SCRATCHPAD_CRC -#ifdef DS_DEBUG - Serial.write("\r\nDATA:"); - for (uint8_t i = 0; i < 9; i++) { - Serial.print(scratchPad[i], HEX); - } -#else - delay(50); -#endif for (uint8_t i = 0; i < 9; i++) { scratchPad[i] = this->mDs->read(); } diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index d7c828f..cb17175 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -51,8 +51,8 @@ RTC_DATA_ATTR long lastWaterValue = 0; const char* ntpServer = "pool.ntp.org"; bool warmBoot = true; -bool mode3Active = false; /**< Controller must not sleep */ -bool mDeepsleep = false; +bool volatile mode3Active = false; /**< Controller must not sleep */ +bool volatile mDeepsleep = false; int plantSensor1 = 0; @@ -121,7 +121,9 @@ bool prepareSleep(void *) { } void espDeepSleepFor(long seconds, bool activatePump = false){ - delay(1500); + if(mode3Active){ + return; + } esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); @@ -147,7 +149,8 @@ void espDeepSleepFor(long seconds, bool activatePump = false){ Serial.print(seconds); Serial.println(" seconds"); esp_sleep_enable_timer_wakeup( (seconds * 1000U * 1000U) ); - wait4sleep.in(500, prepareSleep); + //wait4sleep.in(500, prepareSleep); + mDeepsleep=true; } @@ -183,27 +186,16 @@ void mode2MQTT(){ sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage() ) / 4095)); sensorSolar.setProperty("volt").send( String(getSolarVoltage()) ); - float temp[2] = { TEMP_INIT_VALUE, TEMP_INIT_VALUE }; - float* pFloat = temp; - int devices = dallas.readAllTemperatures(pFloat, 2); - if (devices < 2) { - if ((pFloat[0] > TEMP_INIT_VALUE) && (pFloat[0] < TEMP_MAX_VALUE) ) { - sensorTemp.setProperty("control").send( String(pFloat[0])); - } - } else if (devices >= 2) { - if ((pFloat[0] > TEMP_INIT_VALUE) && (pFloat[0] < TEMP_MAX_VALUE) ) { - sensorTemp.setProperty("temp").send( String(pFloat[0])); - } - if ((pFloat[1] > TEMP_INIT_VALUE) && (pFloat[1] < TEMP_MAX_VALUE) ) { - sensorTemp.setProperty("control").send( String(pFloat[1])); - } - } + float t1 = temp1.getMedian(); + float t2 = temp2.getMedian(); + sensorTemp.setProperty("control").send( String(t1)); + sensorTemp.setProperty("temp").send( String(t2)); - bool lipoTempWarning = abs(temp[0] - temp[1]) > 5; + bool lipoTempWarning = t1!=85 && t2!=85 && abs(t1 - t2) > 10; if(lipoTempWarning){ - Serial.println("Lipo temp incorrect, panic mode deepsleep"); - espDeepSleepFor(PANIK_MODE_DEEPSLEEP); - return; + Serial.println("Lipo temp incorrect, panic mode deepsleep TODO"); + //espDeepSleepFor(PANIK_MODE_DEEPSLEEP); + //return; } bool hasWater = true;//FIXMEmWaterGone > waterLevelMin.get(); @@ -339,18 +331,12 @@ void readSensors() { /* Required to read the temperature once */ float temp[2] = {0, 0}; float* pFloat = temp; - for(int i=0; i < 10; i++) { - // first read returns crap, ignore result and read twice - if (dallas.readAllTemperatures(pFloat, 2) > 0) { - Serial << "t1: " << String(temp[0]) << endl; - Serial << "t2: " << String(temp[1]) << endl; - } - delay(200); - if (i <= 2) { - temp1.add(temp[0]); - temp2.add(temp[1]); - } + if (dallas.readAllTemperatures(pFloat, 2) > 0) { + Serial << "t1: " << String(temp[0]) << endl; + Serial << "t2: " << String(temp[1]) << endl; } + temp1.add(temp[0]); + temp2.add(temp[1]); /* Use the Ultrasonic sensor to measure waterLevel */