From a14f599f5a9ed5c8a9af00e59bd92c9d6d6fc21f Mon Sep 17 00:00:00 2001 From: Empire Date: Wed, 4 Nov 2020 23:28:08 +0100 Subject: [PATCH] missing sensor detection, delta trigger moisture working --- esp32/include/ControllerConfiguration.h | 4 +- esp32/include/HomieConfiguration.h | 1 + esp32/include/HomieTypes.h | 1 + esp32/include/PlantCtrl.h | 13 ++- esp32/src/PlantCtrl.cpp | 8 +- esp32/src/main.cpp | 130 +++++++++++++++++++++--- 6 files changed, 137 insertions(+), 20 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 43ef4f0..86eac83 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -11,7 +11,7 @@ #ifndef CONTROLLER_CONFIG_H #define CONTROLLER_CONFIG_H -#define FIRMWARE_VERSION "1.0.7" +#define FIRMWARE_VERSION "1.0.9" #define ADC_TO_VOLT(adc) ((adc) * 3.3 ) / 4095) #define ADC_TO_VOLT_WITH_MULTI(adc, multi) (((adc)*3.3 * (multi)) / 4095) @@ -64,7 +64,7 @@ #define PANIK_MODE_DEEPSLEEP_US (PANIK_MODE_DEEPSLEEP * 1000 * 1000) #define TEMPERATURE_DELTA_TRIGGER_IN_C 1 -#define MOIST_DELTA_TRIGGER_ADC 1337 +#define MOIST_DELTA_TRIGGER_ADC 10 #define SOLAR_DELTA_VOLT_ADC 3 #define LIPO_DELTA_VOLT_ADC 0.2 diff --git a/esp32/include/HomieConfiguration.h b/esp32/include/HomieConfiguration.h index f300a76..f8671de 100644 --- a/esp32/include/HomieConfiguration.h +++ b/esp32/include/HomieConfiguration.h @@ -37,6 +37,7 @@ HomieNode stayAlive("stay", "alive", "alive"); /** *********************************** Settings ******************************* */ +HomieSetting maxTimeBetweenMQTTUpdates("mqttSleep", "time in seconds to start into mode2"); HomieSetting deepSleepTime("deepsleep", "time in seconds to sleep (0 deactivats it)"); HomieSetting deepSleepNightTime("nightsleep", "time in seconds to sleep (0 uses same setting: deepsleep at night, too)"); HomieSetting wateringDeepSleep("pumpdeepsleep", "time seconds to sleep, while a pump is running"); diff --git a/esp32/include/HomieTypes.h b/esp32/include/HomieTypes.h index 2c915c5..fffc43b 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -14,6 +14,7 @@ #include #define DEACTIVATED_PLANT 5000 +#define MISSING_SENSOR 5001 typedef struct PlantSettings_t { diff --git a/esp32/include/PlantCtrl.h b/esp32/include/PlantCtrl.h index 6902d38..d00c3f5 100644 --- a/esp32/include/PlantCtrl.h +++ b/esp32/include/PlantCtrl.h @@ -48,8 +48,6 @@ public: */ void addSenseValue(void); - int getSensorValue() { return moistureRaw.getMedian(); } - void deactivatePump(void); void activatePump(void); @@ -73,7 +71,10 @@ public: } float getCurrentMoisture() - { + { + if(moistureRaw.getCount()==0){ + return MISSING_SENSOR; + } return this->moistureRaw.getMedian(); } long getSettingsMoisture() @@ -108,7 +109,11 @@ public: return false; } - return (this->mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600); + return (getCooldownInSeconds() > sinceLastActivation); + } + + long getCooldownInSeconds(){ + return this->mSetting->pPumpCooldownInHours->get()*60*60; } bool isAllowedOnlyAtLowLight(void) diff --git a/esp32/src/PlantCtrl.cpp b/esp32/src/PlantCtrl.cpp index 0660dff..046db81 100644 --- a/esp32/src/PlantCtrl.cpp +++ b/esp32/src/PlantCtrl.cpp @@ -11,6 +11,7 @@ */ #include "PlantCtrl.h" +#include "ControllerConfiguration.h" Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting) { @@ -48,8 +49,11 @@ void Plant::init(void) } void Plant::addSenseValue(void) -{ - this->moistureRaw.add(analogRead(this->mPinSensor)); +{ + int raw = analogRead(this->mPinSensor); + if(raw < MOIST_SENSOR_MAX_ADC && raw > MOIST_SENSOR_MIN_ADC){ + this->moistureRaw.add(raw); + } } void Plant::postMQTTconnection(void) diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index aed1cfb..ce27091 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -33,18 +33,25 @@ RTC_DATA_ATTR long gotoMode2AfterThisTimestamp = 0; RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */ RTC_DATA_ATTR long rtcLastActive0 = 0; RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /** MOIST_DELTA_TRIGGER_ADC); + setLastMoisture(i, current); + if (tmp) + { + triggerMoistStart = true; + Serial.printf("Mode2 start due to moist delta in plant %d with %ld \r\n", i, delta); + } + } Serial << "DS18B20" << endl; /* Read the temperature sensors once, as first time 85 degree is returned */ @@ -456,6 +548,7 @@ void readSensors() waterRawSensor.add((duration * .343) / 2); /* deactivate the sensors */ digitalWrite(OUTPUT_SENSOR, LOW); + return triggerMoistStart; } //Homie.getMqttClient().disconnect(); @@ -523,7 +616,7 @@ int determineNextPump() //this pump is in cooldown skip it and disable low power mode trigger for it if (plant.isInCooldown(sinceLastActivation)) { - Serial.printf("%d Skipping due to cooldown %ld \r\n", i, sinceLastActivation); + Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, sinceLastActivation, plant.getCooldownInSeconds()); setMoistureTrigger(i, DEACTIVATED_PLANT); continue; } @@ -533,7 +626,10 @@ int determineNextPump() Serial.printf("%d No pump required: due to light\r\n", i); continue; } - + if(plant.getCurrentMoisture() == MISSING_SENSOR && plant.isPumpTriggerActive()){ + Serial.printf("%d No pump possible: missing sensor \r\n", i); + continue; + } if (plant.isPumpRequired()) { Serial.printf("%d Requested pumping\r\n", i); @@ -541,11 +637,11 @@ int determineNextPump() } else if (plant.isPumpTriggerActive()) { - Serial.printf("%d No pump required: disabled trigger %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture()); + Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture()); } else { - Serial.printf("%d No pump required: disabled trigger\r\n", i); + Serial.printf("%d No pump required: disabled pump trigger \r\n", i); } } return pumpToUse; @@ -588,6 +684,7 @@ void systemInit() // Set default values //in seconds + maxTimeBetweenMQTTUpdates.setDefaultValue(120); deepSleepTime.setDefaultValue(60); deepSleepNightTime.setDefaultValue(600); wateringDeepSleep.setDefaultValue(5); @@ -646,9 +743,13 @@ bool mode1() Serial.println("==== Mode 1 ===="); Serial << getCurrentTime() << " curtime" << endl; - readSensors(); + bool deltaTrigger = readSensors(); //queue sensor values for + if(deltaTrigger){ + Serial.println("1 delta triggered, going to mode2"); + return true; + } if (rtcDeepSleepTime == 0) { Serial.println("1 missing rtc value, going to mode2"); @@ -666,9 +767,14 @@ bool mode1() { continue; } - if (mPlants[i].getSensorValue() <= trigger) + long raw = mPlants[i].getCurrentMoisture(); + if (raw == MISSING_SENSOR) { - Serial << "plant dry starting mode 2" << i << endl; + continue; + } + if (raw > trigger) + { + Serial << "plant " << i << " dry " << raw << " / " << trigger << " starting mode 2" << endl; return true; } }