diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 4768d39..5de1673 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -127,7 +127,7 @@ #define FIRMWARE_BASENAME "PlantControl" #define FIRMWARE_NAME FIRMWARE_BASENAME FIRMWARE_FEATURE1 FIRMWARE_FEATURE2 -#define FIRMWARE_VERSIONNMUMBER "3.011" +#define FIRMWARE_VERSIONNMUMBER "3.012" #ifdef HWREVISION07 #define FIRMWARE_VERSION FIRMWARE_VERSIONNMUMBER " HW0.7" @@ -158,6 +158,7 @@ #define SOLAR_CHARGE_MAX_VOLTAGE 9 /**< Sun is shining (noon) */ #define SOLAR_MAX_VOLTAGE_POSSIBLE 100 /**< higher values are treated as not connected sensor */ #define VOLT_MAX_BATT 4.2f +#define VOLT_SENSORS_BATT 3.6f /**< Minimum battery voltage for sensor (and pump) usage */ #define VOLT_MIN_BATT 3.2f /**< Minimum battery voltage for normal operation */ #define LOWVOLT_SLEEP_FACTOR 6 /**< Factor for nightsleep delay, if the battery drops below minimum (@see VOLT_MIN_BATT) */ #define LOWVOLT_SLEEP_MINIMUM 7200 /**< At low voltage sleep at least for 120 minutes (two hours) */ diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index c68d3a9..64102e1 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -80,9 +80,9 @@ RTC_DATA_ATTR long consecutiveWateringPlant[MAX_PLANTS] = {0}; /****************************************************************************** * LOCAL VARIABLES ******************************************************************************/ -bool volatile mDownloadMode = false; /**< Controller must not sleep */ -bool volatile mSensorsRead = false; /**< Sensors are read without Wifi or MQTT */ -int volatile pumpToRun = -1; /** pump to run at the end of the cycle */ +bool volatile vmDownloadMode = false; /**< Controller must not sleep */ +bool volatile vmSensorsRead = false; /**< Sensors are read without Wifi or MQTT */ +int volatile vmPumpToRun = -1; /** pump to run at the end of the cycle */ int volatile selfTestPumpRun = -1; /** pump to run at the end of the cycle */ bool mConfigured = false; @@ -177,7 +177,7 @@ void espDeepSleep(bool afterPump = false) { /* download mode or normal operation should restore device status*/ finsihedCycleSucessfully(); - if (mDownloadMode) + if (vmDownloadMode) { log(LOG_LEVEL_DEBUG, "abort deepsleep, DownloadMode active", LOG_DEBUG_CODE); return; @@ -403,12 +403,12 @@ void onHomieEvent(const HomieEvent &event) case HomieEventType::SENDING_STATISTICS: break; case HomieEventType::MQTT_READY: - if (mSensorsRead) + if (vmSensorsRead) { Serial.printf("Timeout occured... too late!\r\n"); return; } - mSensorsRead = true; // MQTT is working, deactivate timeout logic + vmSensorsRead = true; // MQTT is working, deactivate timeout logic configTime(UTC_OFFSET_DE, UTF_OFFSET_DE_DST, ntpServer.get()); startMQTTRoundtripTest(); @@ -419,7 +419,7 @@ void onHomieEvent(const HomieEvent &event) mPlants[i].deactivatePump(); } otaRunning = true; - mDownloadMode = true; + vmDownloadMode = true; break; case HomieEventType::OTA_SUCCESSFUL: digitalWrite(OUTPUT_ENABLE_SENSOR, LOW); @@ -511,7 +511,7 @@ int determineNextPump(bool isLowLight) } else { - if (mDownloadMode) + if (vmDownloadMode) { plant.publishState(PLANTSTATE_NUM_ACTIVE_SUPESSED, PLANTSTATE_STR_ACTIVE_SUPESSED); } @@ -571,15 +571,15 @@ bool aliveHandler(const HomieRange &range, const String &value) if (value.equals("ON") || value.equals("On") || value.equals("1")) { - mDownloadMode = true; + vmDownloadMode = true; } else { - if (mDownloadMode) + if (vmDownloadMode) { esp_restart(); } - mDownloadMode = false; + vmDownloadMode = false; } return true; @@ -656,11 +656,11 @@ void initPumpLogic() pcnt_counter_resume(unit); #endif pumpStartTime = millis(); - pumpTarget = millis() + (mPlants[pumpToRun].getPumpDuration() * 1000); + pumpTarget = millis() + (mPlants[vmPumpToRun].getPumpDuration() * 1000); #ifdef FLOWMETER_PIN log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) + " for " + String(mPlants[pumpToRun].getPumpDuration()) + "s or " + String(pumpTargetMl) + "ml", LOG_PUMP_STARTED_CODE); #else - log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) + " for " + String(mPlants[pumpToRun].getPumpDuration()) + "s", LOG_PUMP_STARTED_CODE); + log(LOG_LEVEL_INFO, "Starting pump " + String(vmPumpToRun) + " for " + String(mPlants[vmPumpToRun].getPumpDuration()) + "s", LOG_PUMP_STARTED_CODE); #endif @@ -670,7 +670,7 @@ void initPumpLogic() delay(100); WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); - mPlants[pumpToRun].activatePump(); + mPlants[vmPumpToRun].activatePump(); } void pumpActiveLoop() @@ -681,7 +681,7 @@ void pumpActiveLoop() { initPumpLogic(); pumpStarted = true; - rtcLastWateringPlant[pumpToRun] = getCurrentTime(); + rtcLastWateringPlant[vmPumpToRun] = getCurrentTime(); } bool mqttUpdateTick = false; @@ -723,12 +723,12 @@ void pumpActiveLoop() if (millis() > pumpTarget) { - mPlants[pumpToRun].setProperty("watertime").send(String(duration)); + mPlants[vmPumpToRun].setProperty("watertime").send(String(duration)); targetReached = true; } else if (mqttUpdateTick) { - mPlants[pumpToRun].setProperty("watertime").send(String(duration)); + mPlants[vmPumpToRun].setProperty("watertime").send(String(duration)); } if (targetReached) @@ -736,11 +736,11 @@ void pumpActiveLoop() // disable all digitalWrite(OUTPUT_ENABLE_PUMP, LOW); - mPlants[pumpToRun].deactivatePump(); + mPlants[vmPumpToRun].deactivatePump(); // disable loop, to prevent multi processing pumpStarted = false; // if runtime is larger than cooldown, else it would run continously - rtcLastWateringPlant[pumpToRun] = getCurrentTime(); + rtcLastWateringPlant[vmPumpToRun] = getCurrentTime(); espDeepSleep(true); } } @@ -830,10 +830,6 @@ void safeSetup() { mPlants[i].initSensors(); } - readPowerSwitchedSensors(); - Serial << "Reading Homie Config..." << endl; - Homie.setup(); - /************************* Start One-Wire bus ***************/ int tempInitStartTime = millis(); @@ -847,6 +843,17 @@ void safeSetup() sensorCount = sensors.getDS18Count(); delay(50); } + mBatteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY); + + if (mBatteryVoltage >= VOLT_SENSORS_BATT) + { + /* read all sensors with additional power source */ + readPowerSwitchedSensors(); + } + + /* Start Homie and Wifi */ + Serial << "Reading Homie Config..." << endl; + Homie.setup(); Serial << "DS18S20 count: " << sensorCount << " found in " << (millis() - tempInitStartTime) << " ms" << endl; Serial.flush(); @@ -929,7 +936,7 @@ void safeSetup() delay(100); WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 1); Serial.println("Initial Setup. Start Accesspoint..."); - mDownloadMode = true; + vmDownloadMode = true; } stayAlive.advertise("alive").setName("Alive").setDatatype(NUMBER_TYPE).settable(aliveHandler); setupFinishedTimestamp = millis(); @@ -965,7 +972,7 @@ void selfTest() if (selfTestPumpRun >= 0 && selfTestPumpRun < MAX_PLANTS) { - Serial << "self test mode pump deactivate " << pumpToRun << endl; + Serial << "self test mode pump deactivate " << vmPumpToRun << endl; Serial.flush(); mPlants[selfTestPumpRun].deactivatePump(); } @@ -997,7 +1004,7 @@ void loop() { Homie.loop(); /* Toggel Senor LED to visualize mode 3 */ - if (mDownloadMode) + if (vmDownloadMode) { if (nextBlink < millis()) { @@ -1022,9 +1029,9 @@ void loop() else { unsigned long timeSinceSetup = millis() - setupFinishedTimestamp; - if ((timeSinceSetup > MQTT_TIMEOUT) && (!mSensorsRead)) + if ((timeSinceSetup > MQTT_TIMEOUT) && (!vmSensorsRead)) { - mSensorsRead = true; + vmSensorsRead = true; /* Disable Wifi and put modem into sleep mode */ WiFi.mode(WIFI_OFF); Serial << "Wifi mode set to " << WIFI_OFF << " mqqt was no reached within " << timeSinceSetup << "ms , fallback to offline mode " << endl; @@ -1034,14 +1041,14 @@ void loop() } /** Timeout always stopping the ESP -> no endless power consumption */ - if (millis() > ESP_STALE_TIMEOUT && !mDownloadMode) + if (millis() > ESP_STALE_TIMEOUT && !vmDownloadMode) { Serial << (millis() / 1000) << "not terminated watchdog reset" << endl; Serial.flush(); esp_restart(); } - if (pumpToRun != -1) + if (vmPumpToRun != -1) { pumpActiveLoop(); } @@ -1066,7 +1073,6 @@ void plantcontrol() Serial << "W : " << waterRawSensor.getAverage() << " mm " << endl; - mBatteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY); float chipTemp = battery.getTemperature(); Serial << "Chip Temperatur " << chipTemp << " °C " << endl; @@ -1144,18 +1150,22 @@ bool isLowLight = (mSolarVoltage <= SOLAR_CHARGE_MIN_VOLTAGE); hasWater = waterRawSensor.getAverage() > WATER_LEVEL_MINIMUM; } - // FIXME no water warning message - pumpToRun = determineNextPump(isLowLight); + if (mBatteryVoltage >= VOLT_SENSORS_BATT) + { + // FIXME no water warning message + vmPumpToRun = determineNextPump(isLowLight); + } + // early aborts - if (pumpToRun != -1) + if (vmPumpToRun != -1) { if(isLiquid){ if (hasWater) { - if (mDownloadMode) + if (vmDownloadMode) { log(LOG_LEVEL_INFO, LOG_PUMP_AND_DOWNLOADMODE, LOG_PUMP_AND_DOWNLOADMODE_CODE); - pumpToRun = -1; + vmPumpToRun = -1; } else { /* Pump can be used :) */ } @@ -1163,18 +1173,18 @@ bool isLowLight = (mSolarVoltage <= SOLAR_CHARGE_MIN_VOLTAGE); else { log(LOG_LEVEL_ERROR, LOG_PUMP_BUTNOTANK_MESSAGE, LOG_PUMP_BUTNOTANK_CODE); - pumpToRun = -1; + vmPumpToRun = -1; } } else { log(LOG_LEVEL_ERROR, LOG_VERY_COLD_WATER, LOG_VERY_COLD_WATER_CODE); - pumpToRun = -1; + vmPumpToRun = -1; } } // go directly to sleep, skipping the pump loop - if (pumpToRun == -1) + if (vmPumpToRun == -1) { espDeepSleep(); }