use only seconds for time units, improve self test debug

This commit is contained in:
Your Name 2021-10-27 01:42:01 +02:00
parent e70e467e9b
commit 7d80d444cf
5 changed files with 44 additions and 27 deletions

View File

@ -26,7 +26,7 @@ typedef struct PlantSettings_t
HomieSetting<long> *pPumpAllowedHourRangeStart; HomieSetting<long> *pPumpAllowedHourRangeStart;
HomieSetting<long> *pPumpAllowedHourRangeEnd; HomieSetting<long> *pPumpAllowedHourRangeEnd;
HomieSetting<bool> *pPumpOnlyWhenLowLight; HomieSetting<bool> *pPumpOnlyWhenLowLight;
HomieSetting<long> *pPumpCooldownInMinutes; HomieSetting<long> *pPumpCooldownInSeconds;
HomieSetting<long> *pPumpDuration; HomieSetting<long> *pPumpDuration;
HomieSetting<long> *pPumpPowerLevel; HomieSetting<long> *pPumpPowerLevel;
} PlantSettings_t; } PlantSettings_t;

View File

@ -30,4 +30,5 @@
#define LOG_DEBUG_CODE 1001 #define LOG_DEBUG_CODE 1001
#define LOG_SLEEP_NIGHT 100 #define LOG_SLEEP_NIGHT 100
#define LOG_SLEEP_DAY 101 #define LOG_SLEEP_DAY 101
#define LOG_SLEEP_CYCLE 102
#define LOG_MISSING_PUMP -4 #define LOG_MISSING_PUMP -4

View File

@ -122,7 +122,7 @@ public:
void init(void); void init(void);
long getCooldownInSeconds() { long getCooldownInSeconds() {
return this->mSetting->pPumpCooldownInMinutes->get()*60; return this->mSetting->pPumpCooldownInSeconds->get();
} }
/** /**

View File

@ -46,9 +46,9 @@ void Plant::init(void)
return ((candidate >= 0) && (candidate <= 23)); return ((candidate >= 0) && (candidate <= 23));
}); });
this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(true); this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(true);
this->mSetting->pPumpCooldownInMinutes->setDefaultValue(20); // minutes this->mSetting->pPumpCooldownInSeconds->setDefaultValue(60*60); // 1 hour
this->mSetting->pPumpCooldownInMinutes->setValidator([](long candidate) { this->mSetting->pPumpCooldownInSeconds->setValidator([](long candidate) {
return ((candidate >= 0) && (candidate <= 60*24*7)); return (candidate >= 0);
}); });
this->mSetting->pPumpDuration->setDefaultValue(5); this->mSetting->pPumpDuration->setDefaultValue(5);
@ -71,9 +71,7 @@ void Plant::init(void)
Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG)); Serial.println("Set GPIO mode " + String(mPinSensor) + "=" + String(ANALOG));
Serial.flush(); Serial.flush();
pinMode(this->mPinSensor, INPUT); pinMode(this->mPinSensor, INPUT);
Serial.println("Set GPIO " + String(mPinPump) + "=" + String(LOW));
Serial.flush();
digitalWrite(this->mPinPump, LOW);
pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId); pcnt_unit_t unit = (pcnt_unit_t) (PCNT_UNIT_0 + this->mPlantId);
pcnt_config_t pcnt_config = { }; // Instancia PCNT config pcnt_config_t pcnt_config = { }; // Instancia PCNT config

View File

@ -103,7 +103,7 @@ Plant mPlants[MAX_PLANTS] = {
* LOCAL FUNCTIONS * LOCAL FUNCTIONS
******************************************************************************/ ******************************************************************************/
void espDeepSleep() void espDeepSleep(bool afterPump = false)
{ {
if (mDownloadMode) if (mDownloadMode)
{ {
@ -144,15 +144,23 @@ void espDeepSleep()
long secondsToSleep = -1; long secondsToSleep = -1;
if (mSolarVoltage < SOLAR_CHARGE_MIN_VOLTAGE) if (afterPump)
{ {
log(LOG_LEVEL_INFO, String(String(mSolarVoltage) + "V! Low light -> deepSleepNight"), LOG_SLEEP_NIGHT); log(LOG_LEVEL_INFO, "AfterPump Cycle Resume", LOG_SLEEP_CYCLE);
secondsToSleep = deepSleepNightTime.get(); secondsToSleep = 1;
} }
else else
{ {
log(LOG_LEVEL_INFO, "Sunny -> deepSleep", LOG_SLEEP_DAY); if (mSolarVoltage < SOLAR_CHARGE_MIN_VOLTAGE)
secondsToSleep = deepSleepTime.get(); {
log(LOG_LEVEL_INFO, String(String(mSolarVoltage) + "V! Low light -> deepSleepNight"), LOG_SLEEP_NIGHT);
secondsToSleep = deepSleepNightTime.get();
}
else
{
log(LOG_LEVEL_INFO, "Sunny -> deepSleep", LOG_SLEEP_DAY);
secondsToSleep = deepSleepTime.get();
}
} }
esp_sleep_enable_timer_wakeup((secondsToSleep * 1000U * 1000U)); esp_sleep_enable_timer_wakeup((secondsToSleep * 1000U * 1000U));
@ -409,13 +417,13 @@ int determineNextPump(bool isLowLight)
if (plant.isPumpRequired()) if (plant.isPumpRequired())
{ {
/* Handle e.g. start = 21, end = 8 */ /* Handle e.g. start = 21, end = 8 */
if ( plant.isHydroponic() || (((plant.getHoursStart() > plant.getHoursEnd()) && if (plant.isHydroponic() || (((plant.getHoursStart() > plant.getHoursEnd()) &&
(getCurrentHour() >= plant.getHoursStart() || getCurrentHour() <= plant.getHoursEnd())) || (getCurrentHour() >= plant.getHoursStart() || getCurrentHour() <= plant.getHoursEnd())) ||
/* Handle e.g. start = 8, end = 21 */ /* Handle e.g. start = 8, end = 21 */
((plant.getHoursStart() < plant.getHoursEnd()) && ((plant.getHoursStart() < plant.getHoursEnd()) &&
(getCurrentHour() >= plant.getHoursStart() && getCurrentHour() <= plant.getHoursEnd())) || (getCurrentHour() >= plant.getHoursStart() && getCurrentHour() <= plant.getHoursEnd())) ||
/* no time from NTP received */ /* no time from NTP received */
(getCurrentTime() < 10000))) (getCurrentTime() < 10000)))
{ {
if (wateralarm) if (wateralarm)
{ {
@ -426,7 +434,8 @@ int determineNextPump(bool isLowLight)
plant.publishState("active"); plant.publishState("active");
} }
if(!plant.isHydroponic()){ if (!plant.isHydroponic())
{
consecutiveWateringPlant[i]++; consecutiveWateringPlant[i]++;
} }
@ -538,7 +547,7 @@ bool switch7(const HomieRange &range, const String &value)
void initPumpLogic() void initPumpLogic()
{ {
//set targets //set targets
pumpTarget = millis() + (mPlants[pumpToRun].getPumpDuration() * 1000);
#ifdef FLOWMETER_PIN #ifdef FLOWMETER_PIN
pumpTargetMl = mPlants[pumpToRun].getPumpDuration(); pumpTargetMl = mPlants[pumpToRun].getPumpDuration();
@ -558,6 +567,9 @@ void initPumpLogic()
pcnt_counter_clear(unit); // Zera o contador PCNT pcnt_counter_clear(unit); // Zera o contador PCNT
pcnt_counter_resume(unit); pcnt_counter_resume(unit);
#else
pumpTarget = millis() + (mPlants[pumpToRun].getPumpDuration() * 1000);
log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) + " for " + String(mPlants[pumpToRun].getPumpDuration()) + "s", LOG_PUMP_STARTED_CODE);
#endif #endif
//enable power //enable power
WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0); WRITE_PERI_REG(RTC_CNTL_BROWN_OUT_REG, 0);
@ -574,7 +586,6 @@ void pumpActiveLoop()
if (!pumpStarted) if (!pumpStarted)
{ {
log(LOG_LEVEL_INFO, "Starting pump " + String(pumpToRun) , LOG_PUMP_STARTED_CODE );
initPumpLogic(); initPumpLogic();
pumpStarted = true; pumpStarted = true;
rtcLastWateringPlant[pumpToRun] = getCurrentTime(); rtcLastWateringPlant[pumpToRun] = getCurrentTime();
@ -604,7 +615,7 @@ void pumpActiveLoop()
mPlants[pumpToRun].setProperty("waterusage").send(String(pumped)); mPlants[pumpToRun].setProperty("waterusage").send(String(pumped));
} }
#else #else
if (millis() > pumpTarget*1000) if (millis() > pumpTarget)
{ {
targetReached = true; targetReached = true;
} }
@ -623,7 +634,7 @@ void pumpActiveLoop()
pumpStarted = false; pumpStarted = false;
//if runtime is larger than cooldown, else it would run continously //if runtime is larger than cooldown, else it would run continously
rtcLastWateringPlant[pumpToRun] = getCurrentTime(); rtcLastWateringPlant[pumpToRun] = getCurrentTime();
espDeepSleep(); espDeepSleep(true);
} }
} }
@ -819,12 +830,17 @@ void setup()
void selfTest() void selfTest()
{ {
if (pumpToRun >= 0 && pumpToRun < MAX_PLANTS) if (pumpToRun >= 0 && pumpToRun < MAX_PLANTS)
{ {
Serial << "self test mode pump deactivate " << pumpToRun << endl;
Serial.flush();
mPlants[pumpToRun].deactivatePump(); mPlants[pumpToRun].deactivatePump();
} }
if (pumpToRun >= MAX_PLANTS) if (pumpToRun >= MAX_PLANTS)
{ {
Serial << "self test finished all pumps, proceed to initial wait mode " << pumpToRun << endl;
Serial.flush();
digitalWrite(OUTPUT_ENABLE_PUMP, LOW); digitalWrite(OUTPUT_ENABLE_PUMP, LOW);
nextBlink = millis() + 500; nextBlink = millis() + 500;
} }
@ -835,6 +851,8 @@ void selfTest()
} }
if (pumpToRun < MAX_PLANTS) if (pumpToRun < MAX_PLANTS)
{ {
Serial << "self test activating pump " << pumpToRun << endl;
Serial.flush();
mPlants[pumpToRun].activatePump(); mPlants[pumpToRun].activatePump();
} }
} }