From 47aba5387b7ec43970cb2212939ab3a2c01742c0 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 7 Apr 2021 21:26:11 +0200 Subject: [PATCH] Handle cooldown time for watering; add start and end time when pumping is allowed --- esp32/include/HomieConfiguration.h | 2 +- esp32/include/PlantCtrl.h | 33 +++++++++++--------- esp32/src/main.cpp | 50 ++++++++++++++++++++---------- 3 files changed, 52 insertions(+), 33 deletions(-) diff --git a/esp32/include/HomieConfiguration.h b/esp32/include/HomieConfiguration.h index a14f490..f8fc4e2 100644 --- a/esp32/include/HomieConfiguration.h +++ b/esp32/include/HomieConfiguration.h @@ -91,7 +91,7 @@ HomieSetting ntpServer("ntpServer", "NTP server (pool.ntp.org as d HomieSetting mSensorDry##plant = HomieSetting("moistdry" strplant, "Plant " strplant "- Moist sensor dry threshold"); \ HomieSetting mPumpAllowedHourRangeStart##plant = HomieSetting("rangehourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \ HomieSetting mPumpAllowedHourRangeEnd##plant = HomieSetting("rangehourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \ - HomieSetting mPumpOnlyWhenLowLight##plant = HomieSetting("onlyWhenLowLightZ" strplant, "Plant" strplant " - Enable the Pump only, when there is light but not enought to charge battery"); \ + HomieSetting mPumpOnlyWhenLowLight##plant = HomieSetting("onlyWhenLowLightZ" strplant, "Plant" strplant " - Enable the Pump only, when there is no sunlight"); \ HomieSetting mPumpCooldownInHours##plant = HomieSetting("cooldownpump" strplant, "Plant" strplant " - How long to wait until the pump is activated again (minutes)"); \ PlantSettings_t mSetting##plant = {&mSensorDry##plant, &mPumpAllowedHourRangeStart##plant, &mPumpAllowedHourRangeEnd##plant, &mPumpOnlyWhenLowLight##plant, &mPumpCooldownInHours##plant}; \ /**< Generate all settings for one plant \ diff --git a/esp32/include/PlantCtrl.h b/esp32/include/PlantCtrl.h index 5ad476d..6bf96c8 100644 --- a/esp32/include/PlantCtrl.h +++ b/esp32/include/PlantCtrl.h @@ -98,23 +98,26 @@ public: void init(void); - /** @fn bool isInCooldown(long sinceLastActivation) - * @brief determine, if the plant was recently casted - * @param sinceLastActivation timestamp of last time - */ - bool isInCooldown(long sinceLastActivation) - { - /* if the time difference is greater than one month, we know these are initial values */ - if (sinceLastActivation > (60 * 60 * 24 * 30)) - { - return false; - } - - return (getCooldownInSeconds() > sinceLastActivation); + long getCooldownInSeconds() { + return this->mSetting->pPumpCooldownInHours->get()*60*60; } - long getCooldownInSeconds(){ - return this->mSetting->pPumpCooldownInHours->get()*60*60; + /** + * @brief Get the Hours when pumping should start + * + * @return hour + */ + int getHoursStart() { + return this->mSetting->pPumpAllowedHourRangeStart->get(); + } + + /** + * @brief Get the Hours when pumping should end + * + * @return hour + */ + int getHoursEnd() { + return this->mSetting->pPumpAllowedHourRangeEnd->get(); } bool isAllowedOnlyAtLowLight(void) diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 7737ef3..6c4df42 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -50,6 +50,7 @@ RTC_DATA_ATTR int lastPumpRunning = 0; /**< store last successfully waterd plant RTC_DATA_ATTR long lastWaterValue = 0; /**< to calculate the used water per plant */ RTC_DATA_ATTR int gBootCount = 0; +RTC_DATA_ATTR long rtcLastWateringPlant[MAX_PLANTS] = { 0 }; /****************************************************************************** * LOCAL VARIABLES @@ -93,6 +94,15 @@ long getCurrentTime() return tv_now.tv_sec; } +int getCurrentHour() +{ + struct tm info; + time_t now; + time(&now); + localtime_r(&now, &info); + return info.tm_hour; +} + void espDeepSleepFor(long seconds, bool activatePump = false) { if (mode3Active) @@ -211,7 +221,7 @@ void mode2MQTT() else { digitalWrite(OUTPUT_ENABLE_PUMP, HIGH); - //TODO setLastActivationForPump(lastPumpRunning, getCurrentTime()); + rtcLastWateringPlant[lastPumpRunning] = getCurrentTime(); mPlants[lastPumpRunning].activatePump(); } } @@ -396,7 +406,7 @@ void onHomieEvent(const HomieEvent &event) int determineNextPump() { - bool isLowLight = (mSolarVoltage > SOLAR_CHARGE_MIN_VOLTAGE || mSolarVoltage < SOLAR_CHARGE_MAX_VOLTAGE); + bool isLowLight = (mSolarVoltage < SOLAR_CHARGE_MIN_VOLTAGE); //FIXME instead of for, use sorted by last activation index to ensure equal runtime? @@ -404,35 +414,41 @@ int determineNextPump() for (int i = 0; i < MAX_PLANTS; i++) { Plant plant = mPlants[i]; - //TODO skip pump last used here! - //if (plant.isInCooldown(sinceLastActivation)) - //{ - // Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, sinceLastActivation, plant.getCooldownInSeconds()); - //continue; - //} - //skip as it is not low light + if (!plant.isPumpTriggerActive()) + { + Serial.printf("%d Skip deactivated pump\r\n", i); + continue; + } + if ((rtcLastWateringPlant[i] > 0) + && ((rtcLastWateringPlant[i] + plant.getCooldownInSeconds()) < getCurrentTime())) { + Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, rtcLastWateringPlant[i], plant.getCooldownInSeconds()); + continue; + } if (!isLowLight && plant.isAllowedOnlyAtLowLight()) { Serial.printf("%d No pump required: due to light\r\n", i); continue; } - if (plant.getCurrentMoisture() == MISSING_SENSOR && plant.isPumpTriggerActive()) + if (plant.getCurrentMoisture() == MISSING_SENSOR) { Serial.printf("%d No pump possible: missing sensor \r\n", i); continue; } if (plant.isPumpRequired()) { - Serial.printf("%d Requested pumping\r\n", i); - pumpToUse = i; - } - else if (plant.isPumpTriggerActive()) - { - Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture()); + if ((plant.getHoursStart() > getCurrentHour() && plant.getHoursEnd() < getCurrentHour()) || + (getCurrentTime() < 10000) /* no time from NTP received */) + { + Serial.printf("%d Requested pumping\r\n", i); + pumpToUse = i; + } else { + Serial.printf("%d ignored due to time boundary: %d to %d (current %d)\r\n", i, plant.getHoursStart(), plant.getHoursEnd(), getCurrentHour()); + } + continue; } else { - Serial.printf("%d No pump required: disabled pump trigger \r\n", i); + Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture()); } } return pumpToUse;