diff --git a/esp32/include/PlantCtrl.h b/esp32/include/PlantCtrl.h index 41f735a..a955797 100644 --- a/esp32/include/PlantCtrl.h +++ b/esp32/include/PlantCtrl.h @@ -13,13 +13,12 @@ #define PLANT_CTRL_H #include "HomieTypes.h" +#include "RunningMedian.h" class Plant { private: - int mValue = 0; /**< Value of the moist sensor */ - - int mAnalogValue=0; /**< moist sensor values, used for a calculation */ + RunningMedian moistureRaw = RunningMedian(5); HomieNode* mPlant = NULL; public: @@ -45,14 +44,6 @@ public: */ void addSenseValue(int analogValue); - /** - * @brief Calculate the value based on the information - * @see amountMeasurePoints - * Internal memory, used by addSenseValue will be resetted - * @return int analog value - */ - void calculateSensorValue(int amountMeasurePoints); - /** * @brief Get the Sensor Pin of the analog measuring * @@ -67,7 +58,7 @@ public: */ int getPumpPin() { return mPinPump; } - int getSensorValue() { return mValue; } + int getSensorValue() { return moistureRaw.getMedian(); } /** * @brief Check if a plant is too dry and needs some water. @@ -76,7 +67,7 @@ public: * @return false */ bool isPumpRequired() { - return (this->mSetting->pSensorDry != NULL) && (this->mValue < this->mSetting->pSensorDry->get()); + return (this->mSetting->pSensorDry != NULL) && (this->moistureRaw.getMedian() < this->mSetting->pSensorDry->get()); } HomieInternals::SendingPromise& setProperty(const String& property) const { @@ -84,10 +75,6 @@ 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 7290ae8..e7cf64d 100644 --- a/esp32/src/PlantCtrl.cpp +++ b/esp32/src/PlantCtrl.cpp @@ -20,7 +20,7 @@ Plant::Plant(int pinSensor, int pinPump,int plantId, HomieNode* plant, PlantSett } void Plant::init(void) { - this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT); + this->mSetting->pSensorDry->setDefaultValue(4095); this->mSetting->pSensorDry->setValidator([] (long candidate) { return (((candidate >= 0) && (candidate <= 4095) ) || candidate == DEACTIVATED_PLANT); }); @@ -41,10 +41,5 @@ void Plant::init(void) { } void Plant::addSenseValue(int analog) { - this->mAnalogValue += analog; -} - -void Plant::calculateSensorValue(int amountMeasurePoints) { - this->mValue = this->mAnalogValue / amountMeasurePoints; - this->mAnalogValue = 0; -} + this->moistureRaw.add(analog); +} \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index cfcbc78..c420665 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -121,6 +121,13 @@ bool prepareSleep(void *) { } void espDeepSleepFor(long seconds){ + delay(1500); + gpio_deep_sleep_hold_en(); + gpio_hold_en(GPIO_NUM_13); //pump pwr + //gpio_hold_en(GPIO_NUM_23); //p0 + //FIXME fix for outher outputs + + Serial.print("Going to sleep for "); Serial.print(seconds); Serial.println(" seconds"); @@ -140,15 +147,18 @@ void mode2MQTT(){ digitalWrite(mPlants[i].mPinPump, LOW); } - if (deepSleepTime.get()) { - Serial << "sleeping for " << deepSleepTime.get() << endl; - } - /* Publish default values */ + if (deepSleepTime.get()) { + Serial << "sleeping for " << deepSleepTime.get() << endl; + } + /* Publish default values */ if(lastPumpRunning != -1){ long waterDiff = mWaterGone-lastWaterValue; //TODO attribute used water in ml to plantid } + for(int i=0; i < MAX_PLANTS; i++) { + mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 )); + } sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - mWaterGone )); Serial << "W : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl; lastWaterValue = mWaterGone; @@ -181,11 +191,14 @@ void mode2MQTT(){ return; } - - bool hasWater = mWaterGone > waterLevelMin.get(); + bool hasWater = true;//FIXMEmWaterGone > waterLevelMin.get(); //FIXME no water warning message lastPumpRunning = determineNextPump(); + if(lastPumpRunning != -1 && !hasWater){ + Serial.println("Want to pump but no water"); + } if(lastPumpRunning != -1 && hasWater){ + digitalWrite(OUTPUT_PUMP, HIGH); setLastActivationForPump(lastPumpRunning, getCurrentTime()); digitalWrite(mPlants[lastPumpRunning].mPinPump, HIGH); } @@ -385,30 +398,29 @@ void onHomieEvent(const HomieEvent& event) { int determineNextPump(){ float solarValue = getSolarVoltage(); - bool isLowLight =(ADC_5V_TO_3V3(solarValue) > SOLAR_CHARGE_MIN_VOLTAGE || ADC_5V_TO_3V3(solarValue) < SOLAR_CHARGE_MAX_VOLTAGE); - - - + bool isLowLight =(solarValue > SOLAR_CHARGE_MIN_VOLTAGE || solarValue < SOLAR_CHARGE_MAX_VOLTAGE); //FIXME instead of for, use sorted by last activation index to ensure equal runtime? for(int i=0; i < MAX_PLANTS; i++) { - mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS); - mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 )); long lastActivation = getLastActivationForPump(i); long sinceLastActivation = getCurrentTime()-lastActivation; //this pump is in cooldown skip it and disable low power mode trigger for it if(mPlants[i].mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600){ - setMoistureTrigger(i, DEACTIVATED_PLANT); - continue; + Serial.println("Skipping due to cooldown"); + //setMoistureTrigger(i, DEACTIVATED_PLANT); + //continue; } //skip as it is not low light if(!isLowLight && mPlants[i].mSetting->pPumpOnlyWhenLowLight->get()){ + Serial.println("Skipping due to light"); continue; } if(mPlants->isPumpRequired()){ + Serial.println("Requested pumpin"); return i; } + Serial.println("No pump required"); } return -1; }