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
#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

View File

@ -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);
}

View File

@ -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");
@ -149,6 +156,9 @@ void mode2MQTT(){
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;
}