Plant object expanded with settings

This commit is contained in:
c3ma 2020-10-14 20:07:56 +02:00
parent 24b842f5a3
commit 1a4cddc366
5 changed files with 90 additions and 94 deletions

View File

@ -12,7 +12,8 @@
"istream": "cpp", "istream": "cpp",
"limits": "cpp", "limits": "cpp",
"streambuf": "cpp", "streambuf": "cpp",
"functional": "cpp" "functional": "cpp",
"string": "cpp"
} }
} }
} }

View File

@ -47,7 +47,8 @@
#define MIN_TIME_RUNNING 5UL /**< Amount of seconds the controller must stay awoken */ #define MIN_TIME_RUNNING 5UL /**< Amount of seconds the controller must stay awoken */
#define MAX_PLANTS 7 #define MAX_PLANTS 7
#define EMPTY_LIPO_MULTIPL 3 /**< Multiplier to increase time for sleeping when lipo is empty */ #define EMPTY_LIPO_MULTIPL 3 /**< Multiplier to increase time for sleeping when lipo is empty */
#define MINIMUM_LIPO_VOLT 3.3f /**< Minimum voltage of the Lipo, that must be present */ #define MINIMUM_LIPO_VOLT 3.6f /**< Minimum voltage of the Lipo, that must be present */
#define NO_LIPO_VOLT 2.0f /**< No Lipo connected */
#define MINIMUM_SOLAR_VOLT 4.0f /**< Minimum voltage of the sun, to detect daylight */ #define MINIMUM_SOLAR_VOLT 4.0f /**< Minimum voltage of the sun, to detect daylight */
#define HC_SR04 /**< Ultrasonic distance sensor to measure water level */ #define HC_SR04 /**< Ultrasonic distance sensor to measure water level */

View File

@ -25,9 +25,12 @@ private:
int mAnalogValue=0; /**< moist sensor values, used for a calculation */ int mAnalogValue=0; /**< moist sensor values, used for a calculation */
HomieNode *mPlant = NULL; HomieNode *mPlant = NULL;
HomieSetting<long> *mSensorTriggerLevel=NULL; HomieSetting<long> *mSensorDry;
HomieSetting<long> *mWateringTime=NULL; HomieSetting<long> *mSensorWet;
HomieSetting<long> *mWateringIdleTime=NULL; HomieSetting<long> *mPumpAllowedHourRangeStart;
HomieSetting<long> *mPumpAllowedHourRangeEnd;
HomieSetting<bool> *mPumpOnlyWhenLowLight;
HomieSetting<long> *mPumpCooldownInHours;
public: public:
@ -38,10 +41,7 @@ public:
* @param pinPump Pin of the Pump to use * @param pinPump Pin of the Pump to use
*/ */
Plant(int pinSensor, int pinPump, Plant(int pinSensor, int pinPump,
HomieNode *plant, int plantId);
HomieSetting<long> *sensorTriggerLevel,
HomieSetting<long> *wateringTime,
HomieSetting<long> *wateringIdleTime);
/** /**
* @brief Add a value, to be measured * @brief Add a value, to be measured
@ -81,7 +81,7 @@ public:
* @return false * @return false
*/ */
bool isPumpRequired() { bool isPumpRequired() {
return (this->mSensorTriggerLevel != NULL) && (this->mValue < this->mSensorTriggerLevel->get()); return (this->mSensorWet != NULL) && (this->mValue < this->mSensorWet->get());
} }
HomieInternals::SendingPromise& setProperty(const String& property) const { HomieInternals::SendingPromise& setProperty(const String& property) const {

View File

@ -12,17 +12,63 @@
#include "PlantCtrl.h" #include "PlantCtrl.h"
Plant::Plant(int pinSensor, int pinPump, Plant::Plant(int pinSensor, int pinPump,int plantId) {
HomieNode *plant, this->mPinSensor = pinSensor;
HomieSetting<long> *sensorTriggerLevel, this->mPinPump = pinPump;
HomieSetting<long> *wateringTime,
HomieSetting<long> *wateringIdleTime) { char plantIdChar = plantId+'0';
this->mPlant=plant;
this->mPinSensor = pinSensor; {
this->mPinPump = pinPump; char* name = "moistZdry";
this->mSensorTriggerLevel=sensorTriggerLevel; name[6]= plantIdChar;
this->mWateringTime=wateringTime; mSensorDry = new HomieSetting<long>(name, "Moist sensor dry threshold");
this->mWateringIdleTime=wateringIdleTime; mSensorDry->setDefaultValue(4095);
mSensorDry->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 4095) );
});
}
{
char* name = "moistZwet";
name[6]= plantIdChar;
mSensorWet = new HomieSetting<long>(name, "Moist sensor wet threshold");
mSensorWet->setDefaultValue(0);
mSensorWet->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 4095) );
});
}
{
char* name = "rangeZhourstart";
name[6]= plantIdChar;
mPumpAllowedHourRangeStart = new HomieSetting<long>(name, "Range pump allowed hour start");
mPumpAllowedHourRangeStart->setDefaultValue(8);
mPumpAllowedHourRangeStart->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 23) );
});
}
{
char* name = "rangeZhourend";
name[6]= plantIdChar;
mPumpAllowedHourRangeEnd = new HomieSetting<long>(name, "Range pump allowed hour end");
mPumpAllowedHourRangeEnd->setDefaultValue(20);
mPumpAllowedHourRangeEnd->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 23) );
});
}
{
char* name = "onlyWhenLowLightZ";
name[16]= plantIdChar;
mPumpOnlyWhenLowLight = new HomieSetting<bool>(name, "Enable the Pump only, when there is light but not enought to charge battery");
mPumpOnlyWhenLowLight->setDefaultValue(true);
}
{
char* name = "cooldownpumpZ";
name[12]= plantIdChar;
mPumpCooldownInHours = new HomieSetting<long>(name, "How long to wait until the pump is activated again");
mPumpCooldownInHours->setDefaultValue(20);
mPumpCooldownInHours->setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 1024) );
});
}
} }
void Plant::addSenseValue(int analog) { void Plant::addSenseValue(int analog) {

View File

@ -73,44 +73,22 @@ HomieNode stayAlive("stay", "alive", "alive");
HomieSetting<long> deepSleepTime("deepsleep", "time in milliseconds to sleep (0 deactivats it)"); HomieSetting<long> deepSleepTime("deepsleep", "time in milliseconds to sleep (0 deactivats it)");
HomieSetting<long> deepSleepNightTime("nightsleep", "time in milliseconds to sleep (0 usese same setting: deepsleep at night, too)"); HomieSetting<long> deepSleepNightTime("nightsleep", "time in milliseconds to sleep (0 usese same setting: deepsleep at night, too)");
HomieSetting<long> wateringDeepSleep("pumpdeepsleep", "time seconds to sleep, while a pump is running"); HomieSetting<long> wateringDeepSleep("pumpdeepsleep", "time seconds to sleep, while a pump is running");
HomieSetting<long> plantCnt("plants", "amout of plants to control (1 ... 7)");
#ifdef HC_SR04 HomieSetting<long> waterLevelMax("watermaxlevel", "distance at maximum water level");
HomieSetting<long> waterLevel("watermaxlevel", "Water maximum level in centimeter (50 cm default)"); HomieSetting<long> waterLevelMin("waterminlevel", "distance at minimum water level (pumps still covered)");
HomieSetting<long> waterMinPercent("watermin", "Minimum percentage of water, to activate the pumps (default 5%)"); HomieSetting<long> waterLevelWarn("waterlevelwarn", "warn if below this water level %");
#endif HomieSetting<long> waterLevelVol("waterVolume", "ml between minimum and maximum");
HomieSetting<long> plant0SensorTrigger("moist0", "Moist0 sensor dry ");
HomieSetting<long> plant1SensorTrigger("moist1", "Moist1 sensor value, when pump activates");
HomieSetting<long> plant2SensorTrigger("moist2", "Moist2 sensor value, when pump activates");
HomieSetting<long> plant3SensorTrigger("moist3", "Moist3 sensor value, when pump activates");
HomieSetting<long> plant4SensorTrigger("moist4", "Moist4 sensor value, when pump activates");
HomieSetting<long> plant5SensorTrigger("moist5", "Moist5 sensor value, when pump activates");
HomieSetting<long> plant6SensorTrigger("moist6", "Moist6 sensor value, when pump activates");
HomieSetting<long> wateringTime0("plant0MaxPumpTime", "time seconds Pump0 is running (60 is the default)");
HomieSetting<long> wateringTime1("plant1MaxPumpTime", "time seconds Pump1 is running (60 is the default)");
HomieSetting<long> wateringTime2("plant2MaxPumpTime", "time seconds Pump2 is running (60 is the default)");
HomieSetting<long> wateringTime3("plant3MaxPumpTime", "time seconds Pump3 is running (60 is the default)");
HomieSetting<long> wateringTime4("plant4MaxPumpTime", "time seconds Pump4 is running (60 is the default)");
HomieSetting<long> wateringTime5("plant5MaxPumpTime", "time seconds Pump5 is running (60 is the default)");
HomieSetting<long> wateringTime6("plant6MaxPumpTime", "time seconds Pump6 is running (60 is the default)");
HomieSetting<long> wateringIdleTime0("plant0MinPumpIdle", "time in seconds Pump0 will wait (60 is the default)");
HomieSetting<long> wateringIdleTime1("plant1MinPumpIdle", "time in seconds Pump1 will wait (60 is the default)");
HomieSetting<long> wateringIdleTime2("plant2MinPumpIdle", "time in seconds Pump2 will wait (60 is the default)");
HomieSetting<long> wateringIdleTime3("plant3MinPumpIdle", "time in seconds Pump3 will wait (60 is the default)");
HomieSetting<long> wateringIdleTime4("plant4MinPumpIdle", "time in seconds Pump4 will wait (60 is the default)");
HomieSetting<long> wateringIdleTime5("plant5MinPumpIdle", "time in seconds Pump5 will wait (60 is the default)");
HomieSetting<long> wateringIdleTime6("plant6MinPumpIdle", "time in seconds Pump6 will wait (60 is the default)");
Ds18B20 dallas(SENSOR_DS18B20); Ds18B20 dallas(SENSOR_DS18B20);
Plant mPlants[MAX_PLANTS] = { Plant mPlants[MAX_PLANTS] = {
Plant(SENSOR_PLANT0, OUTPUT_PUMP0, &plant0, &plant0SensorTrigger, &wateringTime0, &wateringIdleTime0), Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0),
Plant(SENSOR_PLANT1, OUTPUT_PUMP1, &plant1, &plant1SensorTrigger, &wateringTime1, &wateringIdleTime1), Plant(SENSOR_PLANT1, OUTPUT_PUMP1, 1),
Plant(SENSOR_PLANT2, OUTPUT_PUMP2, &plant2, &plant2SensorTrigger, &wateringTime2, &wateringIdleTime2), Plant(SENSOR_PLANT2, OUTPUT_PUMP2, 2),
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, &plant3, &plant3SensorTrigger, &wateringTime3, &wateringIdleTime3), Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3),
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, &plant4, &plant4SensorTrigger, &wateringTime4, &wateringIdleTime4), Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4),
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, &plant5, &plant5SensorTrigger, &wateringTime5, &wateringIdleTime5), Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5),
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, &plant6, &plant6SensorTrigger, &wateringTime6, &wateringIdleTime6) Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6)
}; };
void readAnalogValues() { void readAnalogValues() {
@ -134,8 +112,6 @@ void readAnalogValues() {
*/ */
void loopHandler() { void loopHandler() {
int waterLevelPercent = (100 * mWaterGone) / waterLevel.get();
/* Move from Setup to this position, because the Settings are only here available */ /* Move from Setup to this position, because the Settings are only here available */
if (!mLoopInited) { if (!mLoopInited) {
// Configure Deep Sleep: // Configure Deep Sleep:
@ -156,7 +132,7 @@ void loopHandler() {
plant6.setProperty("switch").send(String("OFF")); plant6.setProperty("switch").send(String("OFF"));
#endif #endif
for(int i=0; i < plantCnt.get() && i < MAX_PLANTS; i++) { for(int i=0; i < MAX_PLANTS; i++) {
mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS); mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS);
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 )); mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 ));
/* the last Plant, that was watered is stored in non volatile memory */ /* the last Plant, that was watered is stored in non volatile memory */
@ -170,14 +146,14 @@ void loopHandler() {
} }
} }
sensorWater.setProperty("remaining").send(String(waterLevelPercent)); sensorWater.setProperty("remaining").send(String(waterLevelMax.get() - mWaterGone ));
Serial << "Water : " << mWaterGone << " cm (" << waterLevelPercent << "%)" << endl; Serial << "Water : " << mWaterGone << " cm (" << String(waterLevelMax.get() - mWaterGone ) << "%)" << endl;
/* Check if a plant needs water */ /* Check if a plant needs water */
if (gCurrentPlant > 0) { if (gCurrentPlant > 0) {
int plntIdx = (gCurrentPlant-1); int plntIdx = (gCurrentPlant-1);
if (mPlants[plntIdx].isPumpRequired() && if (mPlants[plntIdx].isPumpRequired() &&
(waterLevelPercent > waterMinPercent.get()) && (mWaterGone > waterLevelMin.get()) &&
(digitalRead(mPlants[plntIdx].getPumpPin()) == LOW) ) { (digitalRead(mPlants[plntIdx].getPumpPin()) == LOW) ) {
Serial << "Plant" << plntIdx << " needs water" << endl; Serial << "Plant" << plntIdx << " needs water" << endl;
mPlants[plntIdx].setProperty("switch").send(String("ON")); mPlants[plntIdx].setProperty("switch").send(String("ON"));
@ -214,7 +190,7 @@ void loopHandler() {
} }
/* Main Loop functionality */ /* Main Loop functionality */
if (waterLevelPercent <= waterMinPercent.get()) { if (mWaterGone <= waterLevelMin.get()) {
/* let the ESP sleep qickly, as nothing must be done */ /* let the ESP sleep qickly, as nothing must be done */
if ((millis() >= (MIN_TIME_RUNNING * MS_TO_S)) && (deepSleepTime.get() > 0)) { if ((millis() >= (MIN_TIME_RUNNING * MS_TO_S)) && (deepSleepTime.get() > 0)) {
mDeepSleep = true; mDeepSleep = true;
@ -400,37 +376,6 @@ void setup() {
// Load the settings // Load the settings
deepSleepTime.setDefaultValue(0); deepSleepTime.setDefaultValue(0);
deepSleepNightTime.setDefaultValue(0); deepSleepNightTime.setDefaultValue(0);
wateringTime0.setDefaultValue(60);
wateringTime1.setDefaultValue(60);
wateringTime2.setDefaultValue(60);
wateringTime3.setDefaultValue(60);
wateringTime4.setDefaultValue(60);
wateringTime5.setDefaultValue(60);
wateringTime6.setDefaultValue(60);
plantCnt.setDefaultValue(0).setValidator([] (long candidate) {
return ((candidate >= 0) && (candidate <= 7) );
});
plant0SensorTrigger.setDefaultValue(4000);
plant1SensorTrigger.setDefaultValue(4000);
plant2SensorTrigger.setDefaultValue(4000);
plant3SensorTrigger.setDefaultValue(4000);
plant4SensorTrigger.setDefaultValue(4000);
plant5SensorTrigger.setDefaultValue(4000);
plant6SensorTrigger.setDefaultValue(4000);
wateringDeepSleep.setDefaultValue(60);
wateringIdleTime0.setDefaultValue(60);
wateringIdleTime1.setDefaultValue(60);
wateringIdleTime2.setDefaultValue(60);
wateringIdleTime3.setDefaultValue(60);
wateringIdleTime4.setDefaultValue(60);
wateringIdleTime5.setDefaultValue(60);
wateringIdleTime6.setDefaultValue(60);
#ifdef HC_SR04
waterLevel.setDefaultValue(50);
waterMinPercent.setDefaultValue(5);
#endif
if (mConfigured) { if (mConfigured) {
// Advertise topics // Advertise topics
@ -502,7 +447,7 @@ void setup() {
Homie.setup(); Homie.setup();
/* Intialize inputs and outputs */ /* Intialize inputs and outputs */
for(int i=0; i < plantCnt.get(); i++) { for(int i=0; i < MAX_PLANTS; i++) {
pinMode(mPlants[i].getPumpPin(), OUTPUT); pinMode(mPlants[i].getPumpPin(), OUTPUT);
pinMode(mPlants[i].getSensorPin(), ANALOG); pinMode(mPlants[i].getSensorPin(), ANALOG);
digitalWrite(mPlants[i].getPumpPin(), LOW); digitalWrite(mPlants[i].getPumpPin(), LOW);
@ -528,7 +473,10 @@ void setup() {
esp_sleep_enable_timer_wakeup(usSleepTime); esp_sleep_enable_timer_wakeup(usSleepTime);
} }
if (mConfigured && (ADC_5V_TO_3V3(lipoSenor) < MINIMUM_LIPO_VOLT) && (deepSleepTime.get()) ) { if (mConfigured &&
(ADC_5V_TO_3V3(lipoSenor) < MINIMUM_LIPO_VOLT) &&
(ADC_5V_TO_3V3(lipoSenor) > NO_LIPO_VOLT) &&
(deepSleepTime.get()) ) {
long sleepEmptyLipo = (deepSleepTime.get() * EMPTY_LIPO_MULTIPL); long sleepEmptyLipo = (deepSleepTime.get() * EMPTY_LIPO_MULTIPL);
Serial << "HOMIE | Change sleeping to " << sleepEmptyLipo << " ms as lipo is at " << ADC_5V_TO_3V3(lipoSenor) << "V" << endl; Serial << "HOMIE | Change sleeping to " << sleepEmptyLipo << " ms as lipo is at " << ADC_5V_TO_3V3(lipoSenor) << "V" << endl;
esp_sleep_enable_timer_wakeup(sleepEmptyLipo * 1000U); esp_sleep_enable_timer_wakeup(sleepEmptyLipo * 1000U);