diff --git a/esp32/include/HomieTypes.h b/esp32/include/HomieTypes.h index 60f60bd..56c2e77 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -13,6 +13,8 @@ #include +#define DEACTIVATED_PLANT 5000 + typedef struct PlantSettings_t { HomieSetting* pSensorDry; HomieSetting* pSensorWet; diff --git a/esp32/include/PlantCtrl.h b/esp32/include/PlantCtrl.h index ea02ddf..464e0ee 100644 --- a/esp32/include/PlantCtrl.h +++ b/esp32/include/PlantCtrl.h @@ -24,9 +24,9 @@ private: int mAnalogValue=0; /**< moist sensor values, used for a calculation */ HomieNode* mPlant = NULL; + PlantSettings_t* mSetting; public: - PlantSettings_t* mSetting; /** * @brief Construct a new Plant object @@ -85,6 +85,10 @@ public: } void init(void); + + long getSettingSensorDry() { + return this->mSetting->pSensorDry->get(); + } }; #endif diff --git a/esp32/src/PlantCtrl.cpp b/esp32/src/PlantCtrl.cpp index ef1ca71..b3eabdd 100644 --- a/esp32/src/PlantCtrl.cpp +++ b/esp32/src/PlantCtrl.cpp @@ -20,9 +20,9 @@ Plant::Plant(int pinSensor, int pinPump,int plantId, HomieNode* plant, PlantSett } void Plant::init(void) { - this->mSetting->pSensorDry->setDefaultValue(4095); + this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT); this->mSetting->pSensorDry->setValidator([] (long candidate) { - return ((candidate >= 0) && (candidate <= 4095) ); + return (((candidate >= 0) && (candidate <= 4095) ) || candidate == DEACTIVATED_PLANT); }); this->mSetting->pSensorWet->setDefaultValue(0); this->mSetting->pSensorWet->setValidator([] (long candidate) { diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 4da930c..a4c3df0 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -24,7 +24,18 @@ const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minut #define TEMP_INIT_VALUE -999.0f #define TEMP_MAX_VALUE 85.0f +/********************* non volatile enable after deepsleep *******************************/ + RTC_DATA_ATTR bool coldBoot = true; +RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */ +RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /**= 4) + plant2.setProperty("switch").send(String("OFF")); plant3.setProperty("switch").send(String("OFF")); plant4.setProperty("switch").send(String("OFF")); plant5.setProperty("switch").send(String("OFF")); -#endif -#if (MAX_PLANTS >= 7) plant6.setProperty("switch").send(String("OFF")); -#endif for(int i=0; i < MAX_PLANTS; i++) { mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS); @@ -186,6 +192,15 @@ void loopHandler() { digitalWrite(OUTPUT_PUMP, HIGH); digitalWrite(mPlants[plntIdx].getPumpPin(), HIGH); } + + rtcDeepSleepTime = deepSleepTime.get(); + rtcMoistureTrigger0 = mPlants[0].getSettingSensorDry(); + rtcMoistureTrigger1 = mPlants[1].getSettingSensorDry(); + rtcMoistureTrigger2 = mPlants[2].getSettingSensorDry(); + rtcMoistureTrigger3 = mPlants[3].getSettingSensorDry(); + rtcMoistureTrigger4 = mPlants[4].getSettingSensorDry(); + rtcMoistureTrigger5 = mPlants[5].getSettingSensorDry(); + rtcMoistureTrigger6 = mPlants[6].getSettingSensorDry(); } mLoopInited = true; @@ -414,7 +429,7 @@ void systemInit(){ // Mode 3 stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler); } - + Homie.setup(); } @@ -422,6 +437,51 @@ void systemInit(){ bool mode1(){ Serial.println("Init mode 1"); readSensors(); + + if ((rtcDeepSleepTime == 0) || + (rtcMoistureTrigger0 == 0) || + (rtcMoistureTrigger1 == 0) || + (rtcMoistureTrigger2 == 0) || + (rtcMoistureTrigger3 == 0) || + (rtcMoistureTrigger4 == 0) || + (rtcMoistureTrigger5 == 0) || + (rtcMoistureTrigger6 == 0) + ) + { + Serial.println("Missing RTC information"); + return true; + } + + if ((rtcMoistureTrigger0 != DEACTIVATED_PLANT) && (mPlants[0].getSensorValue() < rtcMoistureTrigger0) ) { + Serial.println("Moisture of plant 0"); + return true; + } + if ((rtcMoistureTrigger1 != DEACTIVATED_PLANT) && (mPlants[1].getSensorValue() < rtcMoistureTrigger1) ) { + Serial.println("Moisture of plant 1"); + return true; + } + if ((rtcMoistureTrigger2 != DEACTIVATED_PLANT) && (mPlants[2].getSensorValue() < rtcMoistureTrigger2) ) { + Serial.println("Moisture of plant 2"); + return true; + } + if ((rtcMoistureTrigger3 != DEACTIVATED_PLANT) && (mPlants[3].getSensorValue() < rtcMoistureTrigger3) ) { + Serial.println("Moisture of plant 3"); + return true; + } + if ((rtcMoistureTrigger4 != DEACTIVATED_PLANT) && (mPlants[4].getSensorValue() < rtcMoistureTrigger4) ) { + Serial.println("Moisture of plant 4"); + return true; + } + if ((rtcMoistureTrigger5 != DEACTIVATED_PLANT) && (mPlants[5].getSensorValue() < rtcMoistureTrigger5) ) { + Serial.println("Moisture of plant 5"); + return true; + } + if ((rtcMoistureTrigger6 != DEACTIVATED_PLANT) && (mPlants[6].getSensorValue() < rtcMoistureTrigger6) ) { + Serial.println("Moisture of plant 6"); + return true; + } + //TODO read sensor values for each plant + //TODO evaluate if something is to do return false; }