settings gpio mode before deepsleep

This commit is contained in:
c3ma 2020-10-21 20:46:09 +02:00
parent 1485539a8b
commit 9b5bf90da9
3 changed files with 33 additions and 39 deletions

View File

@ -13,13 +13,12 @@
#define PLANT_CTRL_H #define PLANT_CTRL_H
#include "HomieTypes.h" #include "HomieTypes.h"
#include "RunningMedian.h"
class Plant { class Plant {
private: private:
int mValue = 0; /**< Value of the moist sensor */ RunningMedian moistureRaw = RunningMedian(5);
int mAnalogValue=0; /**< moist sensor values, used for a calculation */
HomieNode* mPlant = NULL; HomieNode* mPlant = NULL;
public: public:
@ -45,14 +44,6 @@ public:
*/ */
void addSenseValue(int analogValue); 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 * @brief Get the Sensor Pin of the analog measuring
* *
@ -67,7 +58,7 @@ public:
*/ */
int getPumpPin() { return mPinPump; } 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. * @brief Check if a plant is too dry and needs some water.
@ -76,7 +67,7 @@ public:
* @return false * @return false
*/ */
bool isPumpRequired() { 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 { HomieInternals::SendingPromise& setProperty(const String& property) const {
@ -84,10 +75,6 @@ public:
} }
void init(void); void init(void);
long getSettingSensorDry() {
return this->mSetting->pSensorDry->get();
}
}; };
#endif #endif

View File

@ -20,7 +20,7 @@ Plant::Plant(int pinSensor, int pinPump,int plantId, HomieNode* plant, PlantSett
} }
void Plant::init(void) { void Plant::init(void) {
this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT); this->mSetting->pSensorDry->setDefaultValue(4095);
this->mSetting->pSensorDry->setValidator([] (long candidate) { this->mSetting->pSensorDry->setValidator([] (long candidate) {
return (((candidate >= 0) && (candidate <= 4095) ) || candidate == DEACTIVATED_PLANT); return (((candidate >= 0) && (candidate <= 4095) ) || candidate == DEACTIVATED_PLANT);
}); });
@ -41,10 +41,5 @@ void Plant::init(void) {
} }
void Plant::addSenseValue(int analog) { void Plant::addSenseValue(int analog) {
this->mAnalogValue += analog; this->moistureRaw.add(analog);
}
void Plant::calculateSensorValue(int amountMeasurePoints) {
this->mValue = this->mAnalogValue / amountMeasurePoints;
this->mAnalogValue = 0;
} }

View File

@ -121,6 +121,13 @@ bool prepareSleep(void *) {
} }
void espDeepSleepFor(long seconds){ 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("Going to sleep for ");
Serial.print(seconds); Serial.print(seconds);
Serial.println(" seconds"); Serial.println(" seconds");
@ -149,6 +156,9 @@ void mode2MQTT(){
long waterDiff = mWaterGone-lastWaterValue; long waterDiff = mWaterGone-lastWaterValue;
//TODO attribute used water in ml to plantid //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 )); sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - mWaterGone ));
Serial << "W : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl; Serial << "W : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl;
lastWaterValue = mWaterGone; lastWaterValue = mWaterGone;
@ -181,11 +191,14 @@ void mode2MQTT(){
return; return;
} }
bool hasWater = true;//FIXMEmWaterGone > waterLevelMin.get();
bool hasWater = mWaterGone > waterLevelMin.get();
//FIXME no water warning message //FIXME no water warning message
lastPumpRunning = determineNextPump(); lastPumpRunning = determineNextPump();
if(lastPumpRunning != -1 && !hasWater){
Serial.println("Want to pump but no water");
}
if(lastPumpRunning != -1 && hasWater){ if(lastPumpRunning != -1 && hasWater){
digitalWrite(OUTPUT_PUMP, HIGH);
setLastActivationForPump(lastPumpRunning, getCurrentTime()); setLastActivationForPump(lastPumpRunning, getCurrentTime());
digitalWrite(mPlants[lastPumpRunning].mPinPump, HIGH); digitalWrite(mPlants[lastPumpRunning].mPinPump, HIGH);
} }
@ -385,30 +398,29 @@ void onHomieEvent(const HomieEvent& event) {
int determineNextPump(){ int determineNextPump(){
float solarValue = getSolarVoltage(); 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? //FIXME instead of for, use sorted by last activation index to ensure equal runtime?
for(int i=0; i < MAX_PLANTS; i++) { 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 lastActivation = getLastActivationForPump(i);
long sinceLastActivation = getCurrentTime()-lastActivation; long sinceLastActivation = getCurrentTime()-lastActivation;
//this pump is in cooldown skip it and disable low power mode trigger for it //this pump is in cooldown skip it and disable low power mode trigger for it
if(mPlants[i].mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600){ if(mPlants[i].mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600){
setMoistureTrigger(i, DEACTIVATED_PLANT); Serial.println("Skipping due to cooldown");
continue; //setMoistureTrigger(i, DEACTIVATED_PLANT);
//continue;
} }
//skip as it is not low light //skip as it is not low light
if(!isLowLight && mPlants[i].mSetting->pPumpOnlyWhenLowLight->get()){ if(!isLowLight && mPlants[i].mSetting->pPumpOnlyWhenLowLight->get()){
Serial.println("Skipping due to light");
continue; continue;
} }
if(mPlants->isPumpRequired()){ if(mPlants->isPumpRequired()){
Serial.println("Requested pumpin");
return i; return i;
} }
Serial.println("No pump required");
} }
return -1; return -1;
} }