Handle cooldown time for watering; add start and end time when pumping is allowed

This commit is contained in:
Ollo 2021-04-07 21:26:11 +02:00
parent 3932e82593
commit 47aba5387b
3 changed files with 52 additions and 33 deletions

View File

@ -91,7 +91,7 @@ HomieSetting<const char *> ntpServer("ntpServer", "NTP server (pool.ntp.org as d
HomieSetting<long> mSensorDry##plant = HomieSetting<long>("moistdry" strplant, "Plant " strplant "- Moist sensor dry threshold"); \ HomieSetting<long> mSensorDry##plant = HomieSetting<long>("moistdry" strplant, "Plant " strplant "- Moist sensor dry threshold"); \
HomieSetting<long> mPumpAllowedHourRangeStart##plant = HomieSetting<long>("rangehourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \ HomieSetting<long> mPumpAllowedHourRangeStart##plant = HomieSetting<long>("rangehourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \
HomieSetting<long> mPumpAllowedHourRangeEnd##plant = HomieSetting<long>("rangehourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \ HomieSetting<long> mPumpAllowedHourRangeEnd##plant = HomieSetting<long>("rangehourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \
HomieSetting<bool> mPumpOnlyWhenLowLight##plant = HomieSetting<bool>("onlyWhenLowLightZ" strplant, "Plant" strplant " - Enable the Pump only, when there is light but not enought to charge battery"); \ HomieSetting<bool> mPumpOnlyWhenLowLight##plant = HomieSetting<bool>("onlyWhenLowLightZ" strplant, "Plant" strplant " - Enable the Pump only, when there is no sunlight"); \
HomieSetting<long> mPumpCooldownInHours##plant = HomieSetting<long>("cooldownpump" strplant, "Plant" strplant " - How long to wait until the pump is activated again (minutes)"); \ HomieSetting<long> mPumpCooldownInHours##plant = HomieSetting<long>("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}; \ PlantSettings_t mSetting##plant = {&mSensorDry##plant, &mPumpAllowedHourRangeStart##plant, &mPumpAllowedHourRangeEnd##plant, &mPumpOnlyWhenLowLight##plant, &mPumpCooldownInHours##plant}; \
/**< Generate all settings for one plant \ /**< Generate all settings for one plant \

View File

@ -98,25 +98,28 @@ public:
void init(void); void init(void);
/** @fn bool isInCooldown(long sinceLastActivation) long getCooldownInSeconds() {
* @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; 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) bool isAllowedOnlyAtLowLight(void)
{ {
return this->mSetting->pPumpOnlyWhenLowLight->get(); return this->mSetting->pPumpOnlyWhenLowLight->get();

View File

@ -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 long lastWaterValue = 0; /**< to calculate the used water per plant */
RTC_DATA_ATTR int gBootCount = 0; RTC_DATA_ATTR int gBootCount = 0;
RTC_DATA_ATTR long rtcLastWateringPlant[MAX_PLANTS] = { 0 };
/****************************************************************************** /******************************************************************************
* LOCAL VARIABLES * LOCAL VARIABLES
@ -93,6 +94,15 @@ long getCurrentTime()
return tv_now.tv_sec; 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) void espDeepSleepFor(long seconds, bool activatePump = false)
{ {
if (mode3Active) if (mode3Active)
@ -211,7 +221,7 @@ void mode2MQTT()
else else
{ {
digitalWrite(OUTPUT_ENABLE_PUMP, HIGH); digitalWrite(OUTPUT_ENABLE_PUMP, HIGH);
//TODO setLastActivationForPump(lastPumpRunning, getCurrentTime()); rtcLastWateringPlant[lastPumpRunning] = getCurrentTime();
mPlants[lastPumpRunning].activatePump(); mPlants[lastPumpRunning].activatePump();
} }
} }
@ -396,7 +406,7 @@ void onHomieEvent(const HomieEvent &event)
int determineNextPump() 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? //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++) for (int i = 0; i < MAX_PLANTS; i++)
{ {
Plant plant = mPlants[i]; Plant plant = mPlants[i];
//TODO skip pump last used here! if (!plant.isPumpTriggerActive())
//if (plant.isInCooldown(sinceLastActivation)) {
//{ Serial.printf("%d Skip deactivated pump\r\n", i);
// Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, sinceLastActivation, plant.getCooldownInSeconds()); continue;
//continue; }
//} if ((rtcLastWateringPlant[i] > 0)
//skip as it is not low light && ((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()) if (!isLowLight && plant.isAllowedOnlyAtLowLight())
{ {
Serial.printf("%d No pump required: due to light\r\n", i); Serial.printf("%d No pump required: due to light\r\n", i);
continue; continue;
} }
if (plant.getCurrentMoisture() == MISSING_SENSOR && plant.isPumpTriggerActive()) if (plant.getCurrentMoisture() == MISSING_SENSOR)
{ {
Serial.printf("%d No pump possible: missing sensor \r\n", i); Serial.printf("%d No pump possible: missing sensor \r\n", i);
continue; continue;
} }
if (plant.isPumpRequired()) if (plant.isPumpRequired())
{
if ((plant.getHoursStart() > getCurrentHour() && plant.getHoursEnd() < getCurrentHour()) ||
(getCurrentTime() < 10000) /* no time from NTP received */)
{ {
Serial.printf("%d Requested pumping\r\n", i); Serial.printf("%d Requested pumping\r\n", i);
pumpToUse = 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());
} }
else if (plant.isPumpTriggerActive()) continue;
{
Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture());
} }
else 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; return pumpToUse;