missing sensor detection, delta trigger moisture working

This commit is contained in:
Empire 2020-11-04 23:28:08 +01:00
parent 1dcf4df740
commit a14f599f5a
6 changed files with 137 additions and 20 deletions

View File

@ -11,7 +11,7 @@
#ifndef CONTROLLER_CONFIG_H #ifndef CONTROLLER_CONFIG_H
#define CONTROLLER_CONFIG_H #define CONTROLLER_CONFIG_H
#define FIRMWARE_VERSION "1.0.7" #define FIRMWARE_VERSION "1.0.9"
#define ADC_TO_VOLT(adc) ((adc) * 3.3 ) / 4095) #define ADC_TO_VOLT(adc) ((adc) * 3.3 ) / 4095)
#define ADC_TO_VOLT_WITH_MULTI(adc, multi) (((adc)*3.3 * (multi)) / 4095) #define ADC_TO_VOLT_WITH_MULTI(adc, multi) (((adc)*3.3 * (multi)) / 4095)
@ -64,7 +64,7 @@
#define PANIK_MODE_DEEPSLEEP_US (PANIK_MODE_DEEPSLEEP * 1000 * 1000) #define PANIK_MODE_DEEPSLEEP_US (PANIK_MODE_DEEPSLEEP * 1000 * 1000)
#define TEMPERATURE_DELTA_TRIGGER_IN_C 1 #define TEMPERATURE_DELTA_TRIGGER_IN_C 1
#define MOIST_DELTA_TRIGGER_ADC 1337 #define MOIST_DELTA_TRIGGER_ADC 10
#define SOLAR_DELTA_VOLT_ADC 3 #define SOLAR_DELTA_VOLT_ADC 3
#define LIPO_DELTA_VOLT_ADC 0.2 #define LIPO_DELTA_VOLT_ADC 0.2

View File

@ -37,6 +37,7 @@ HomieNode stayAlive("stay", "alive", "alive");
/** /**
*********************************** Settings ******************************* *********************************** Settings *******************************
*/ */
HomieSetting<long> maxTimeBetweenMQTTUpdates("mqttSleep", "time in seconds to start into mode2");
HomieSetting<long> deepSleepTime("deepsleep", "time in seconds to sleep (0 deactivats it)"); HomieSetting<long> deepSleepTime("deepsleep", "time in seconds to sleep (0 deactivats it)");
HomieSetting<long> deepSleepNightTime("nightsleep", "time in seconds to sleep (0 uses same setting: deepsleep at night, too)"); HomieSetting<long> deepSleepNightTime("nightsleep", "time in seconds to sleep (0 uses 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");

View File

@ -14,6 +14,7 @@
#include <Homie.h> #include <Homie.h>
#define DEACTIVATED_PLANT 5000 #define DEACTIVATED_PLANT 5000
#define MISSING_SENSOR 5001
typedef struct PlantSettings_t typedef struct PlantSettings_t
{ {

View File

@ -48,8 +48,6 @@ public:
*/ */
void addSenseValue(void); void addSenseValue(void);
int getSensorValue() { return moistureRaw.getMedian(); }
void deactivatePump(void); void deactivatePump(void);
void activatePump(void); void activatePump(void);
@ -73,7 +71,10 @@ public:
} }
float getCurrentMoisture() float getCurrentMoisture()
{ {
if(moistureRaw.getCount()==0){
return MISSING_SENSOR;
}
return this->moistureRaw.getMedian(); return this->moistureRaw.getMedian();
} }
long getSettingsMoisture() long getSettingsMoisture()
@ -108,7 +109,11 @@ public:
return false; return false;
} }
return (this->mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600); return (getCooldownInSeconds() > sinceLastActivation);
}
long getCooldownInSeconds(){
return this->mSetting->pPumpCooldownInHours->get()*60*60;
} }
bool isAllowedOnlyAtLowLight(void) bool isAllowedOnlyAtLowLight(void)

View File

@ -11,6 +11,7 @@
*/ */
#include "PlantCtrl.h" #include "PlantCtrl.h"
#include "ControllerConfiguration.h"
Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting) Plant::Plant(int pinSensor, int pinPump, int plantId, HomieNode *plant, PlantSettings_t *setting)
{ {
@ -48,8 +49,11 @@ void Plant::init(void)
} }
void Plant::addSenseValue(void) void Plant::addSenseValue(void)
{ {
this->moistureRaw.add(analogRead(this->mPinSensor)); int raw = analogRead(this->mPinSensor);
if(raw < MOIST_SENSOR_MAX_ADC && raw > MOIST_SENSOR_MIN_ADC){
this->moistureRaw.add(raw);
}
} }
void Plant::postMQTTconnection(void) void Plant::postMQTTconnection(void)

View File

@ -33,18 +33,25 @@ RTC_DATA_ATTR long gotoMode2AfterThisTimestamp = 0;
RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */ RTC_DATA_ATTR long rtcDeepSleepTime = 0; /**< Time, when the microcontroller shall be up again */
RTC_DATA_ATTR long rtcLastActive0 = 0; RTC_DATA_ATTR long rtcLastActive0 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /**<Level for the moisture sensor */ RTC_DATA_ATTR long rtcMoistureTrigger0 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcMoisture0 = 0;
RTC_DATA_ATTR long rtcLastActive1 = 0; RTC_DATA_ATTR long rtcLastActive1 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger1 = 0; /**<Level for the moisture sensor */ RTC_DATA_ATTR long rtcMoistureTrigger1 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcMoisture1 = 0;
RTC_DATA_ATTR long rtcLastActive2 = 0; RTC_DATA_ATTR long rtcLastActive2 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger2 = 0; /**<Level for the moisture sensor */ RTC_DATA_ATTR long rtcMoistureTrigger2 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcMoisture2 = 0;
RTC_DATA_ATTR long rtcLastActive3 = 0; RTC_DATA_ATTR long rtcLastActive3 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger3 = 0; /**<Level for the moisture sensor */ RTC_DATA_ATTR long rtcMoistureTrigger3 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcMoisture3 = 0;
RTC_DATA_ATTR long rtcLastActive4 = 0; RTC_DATA_ATTR long rtcLastActive4 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger4 = 0; /**<Level for the moisture sensor */ RTC_DATA_ATTR long rtcMoistureTrigger4 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcMoisture4 = 0;
RTC_DATA_ATTR long rtcLastActive5 = 0; RTC_DATA_ATTR long rtcLastActive5 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger5 = 0; /**<Level for the moisture sensor */ RTC_DATA_ATTR long rtcMoistureTrigger5 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcMoisture5 = 0;
RTC_DATA_ATTR long rtcLastActive6 = 0; RTC_DATA_ATTR long rtcLastActive6 = 0;
RTC_DATA_ATTR long rtcMoistureTrigger6 = 0; /**<Level for the moisture sensor */ RTC_DATA_ATTR long rtcMoistureTrigger6 = 0; /**<Level for the moisture sensor */
RTC_DATA_ATTR long rtcMoisture6 = 0;
RTC_DATA_ATTR int lastPumpRunning = 0; RTC_DATA_ATTR int lastPumpRunning = 0;
RTC_DATA_ATTR long lastWaterValue = 0; RTC_DATA_ATTR long lastWaterValue = 0;
@ -120,6 +127,71 @@ void setMoistureTrigger(int plantId, long value)
} }
} }
void setLastMoisture(int plantId, long value)
{
if (plantId == 0)
{
rtcMoisture0 = value;
}
if (plantId == 1)
{
rtcMoisture1 = value;
}
if (plantId == 2)
{
rtcMoisture2 = value;
}
if (plantId == 3)
{
rtcMoisture3 = value;
}
if (plantId == 4)
{
rtcMoisture4 = value;
}
if (plantId == 5)
{
rtcMoisture5 = value;
}
if (plantId == 6)
{
rtcMoisture6 = value;
}
}
long getLastMoisture(int plantId)
{
if (plantId == 0)
{
return rtcMoisture0;
}
if (plantId == 1)
{
return rtcMoisture1;
}
if (plantId == 2)
{
return rtcMoisture2;
}
if (plantId == 3)
{
return rtcMoisture3;
}
if (plantId == 4)
{
return rtcMoisture4;
}
if (plantId == 5)
{
return rtcMoisture5;
}
if (plantId == 6)
{
return rtcMoisture6;
}
return -1;
}
void readSystemSensors() void readSystemSensors()
{ {
lipoRawSensor.add(analogRead(SENSOR_LIPO)); lipoRawSensor.add(analogRead(SENSOR_LIPO));
@ -171,6 +243,7 @@ void espDeepSleepFor(long seconds, bool activatePump = false)
gpio_hold_dis(GPIO_NUM_13); //pump pwr gpio_hold_dis(GPIO_NUM_13); //pump pwr
gpio_deep_sleep_hold_dis(); gpio_deep_sleep_hold_dis();
digitalWrite(OUTPUT_PUMP, LOW); digitalWrite(OUTPUT_PUMP, LOW);
digitalWrite(OUTPUT_SENSOR, LOW);
for (int i = 0; i < MAX_PLANTS; i++) for (int i = 0; i < MAX_PLANTS; i++)
{ {
mPlants[i].deactivatePump(); mPlants[i].deactivatePump();
@ -209,7 +282,12 @@ void mode2MQTT()
} }
for (int i = 0; i < MAX_PLANTS; i++) for (int i = 0; i < MAX_PLANTS; i++)
{ {
long pct = 100 - map(mPlants[i].getSensorValue(), MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100); long raw = mPlants[i].getCurrentMoisture();
long pct = 100 - map(raw, MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100);
if (raw == MISSING_SENSOR)
{
pct = 0;
}
if (pct < 0) if (pct < 0)
{ {
pct = 0; pct = 0;
@ -218,8 +296,9 @@ void mode2MQTT()
{ {
pct = 100; pct = 100;
} }
mPlants[i].setProperty("moist").send(String(pct)); mPlants[i].setProperty("moist").send(String(pct));
mPlants[i].setProperty("moistraw").send(String(mPlants[i].getSensorValue())); mPlants[i].setProperty("moistraw").send(String(raw));
} }
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;
@ -281,14 +360,14 @@ void mode2MQTT()
{ {
if (getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE) if (getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE)
{ {
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepNightTime.get(); gotoMode2AfterThisTimestamp = getCurrentTime() + maxTimeBetweenMQTTUpdates.get();
Serial.println("No pumps to activate and low light, deepSleepNight"); Serial.println("No pumps to activate and low light, deepSleepNight");
espDeepSleepFor(deepSleepNightTime.get()); espDeepSleepFor(deepSleepNightTime.get());
rtcDeepSleepTime = deepSleepNightTime.get(); rtcDeepSleepTime = deepSleepNightTime.get();
} }
else else
{ {
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepTime.get(); gotoMode2AfterThisTimestamp = getCurrentTime() + maxTimeBetweenMQTTUpdates.get();
Serial.println("No pumps to activate, deepSleep"); Serial.println("No pumps to activate, deepSleep");
espDeepSleepFor(deepSleepTime.get()); espDeepSleepFor(deepSleepTime.get());
rtcDeepSleepTime = deepSleepTime.get(); rtcDeepSleepTime = deepSleepTime.get();
@ -404,7 +483,7 @@ long getLastActivationForPump(int plantId)
* @brief Sensors, that are connected to GPIOs, mandatory for WIFI. * @brief Sensors, that are connected to GPIOs, mandatory for WIFI.
* These sensors (ADC2) can only be read when no Wifi is used. * These sensors (ADC2) can only be read when no Wifi is used.
*/ */
void readSensors() bool readSensors()
{ {
Serial << "Read Sensors" << endl; Serial << "Read Sensors" << endl;
@ -424,6 +503,19 @@ void readSensors()
} }
delay(10); delay(10);
} }
bool triggerMoistStart = false;
for (int i = 0; i < MAX_PLANTS; i++)
{
long current = mPlants[i].getCurrentMoisture();
long delta = abs(getLastMoisture(i) - current);
bool tmp = (delta > MOIST_DELTA_TRIGGER_ADC);
setLastMoisture(i, current);
if (tmp)
{
triggerMoistStart = true;
Serial.printf("Mode2 start due to moist delta in plant %d with %ld \r\n", i, delta);
}
}
Serial << "DS18B20" << endl; Serial << "DS18B20" << endl;
/* Read the temperature sensors once, as first time 85 degree is returned */ /* Read the temperature sensors once, as first time 85 degree is returned */
@ -456,6 +548,7 @@ void readSensors()
waterRawSensor.add((duration * .343) / 2); waterRawSensor.add((duration * .343) / 2);
/* deactivate the sensors */ /* deactivate the sensors */
digitalWrite(OUTPUT_SENSOR, LOW); digitalWrite(OUTPUT_SENSOR, LOW);
return triggerMoistStart;
} }
//Homie.getMqttClient().disconnect(); //Homie.getMqttClient().disconnect();
@ -523,7 +616,7 @@ int determineNextPump()
//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 (plant.isInCooldown(sinceLastActivation)) if (plant.isInCooldown(sinceLastActivation))
{ {
Serial.printf("%d Skipping due to cooldown %ld \r\n", i, sinceLastActivation); Serial.printf("%d Skipping due to cooldown %ld / %ld \r\n", i, sinceLastActivation, plant.getCooldownInSeconds());
setMoistureTrigger(i, DEACTIVATED_PLANT); setMoistureTrigger(i, DEACTIVATED_PLANT);
continue; continue;
} }
@ -533,7 +626,10 @@ int determineNextPump()
Serial.printf("%d No pump required: due to light\r\n", i); Serial.printf("%d No pump required: due to light\r\n", i);
continue; continue;
} }
if(plant.getCurrentMoisture() == MISSING_SENSOR && plant.isPumpTriggerActive()){
Serial.printf("%d No pump possible: missing sensor \r\n", i);
continue;
}
if (plant.isPumpRequired()) if (plant.isPumpRequired())
{ {
Serial.printf("%d Requested pumping\r\n", i); Serial.printf("%d Requested pumping\r\n", i);
@ -541,11 +637,11 @@ int determineNextPump()
} }
else if (plant.isPumpTriggerActive()) else if (plant.isPumpTriggerActive())
{ {
Serial.printf("%d No pump required: disabled trigger %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture()); Serial.printf("%d No pump required: moisture acceptable %f / %ld\r\n", i, plant.getCurrentMoisture(), plant.getSettingsMoisture());
} }
else else
{ {
Serial.printf("%d No pump required: disabled trigger\r\n", i); Serial.printf("%d No pump required: disabled pump trigger \r\n", i);
} }
} }
return pumpToUse; return pumpToUse;
@ -588,6 +684,7 @@ void systemInit()
// Set default values // Set default values
//in seconds //in seconds
maxTimeBetweenMQTTUpdates.setDefaultValue(120);
deepSleepTime.setDefaultValue(60); deepSleepTime.setDefaultValue(60);
deepSleepNightTime.setDefaultValue(600); deepSleepNightTime.setDefaultValue(600);
wateringDeepSleep.setDefaultValue(5); wateringDeepSleep.setDefaultValue(5);
@ -646,9 +743,13 @@ bool mode1()
Serial.println("==== Mode 1 ===="); Serial.println("==== Mode 1 ====");
Serial << getCurrentTime() << " curtime" << endl; Serial << getCurrentTime() << " curtime" << endl;
readSensors(); bool deltaTrigger = readSensors();
//queue sensor values for //queue sensor values for
if(deltaTrigger){
Serial.println("1 delta triggered, going to mode2");
return true;
}
if (rtcDeepSleepTime == 0) if (rtcDeepSleepTime == 0)
{ {
Serial.println("1 missing rtc value, going to mode2"); Serial.println("1 missing rtc value, going to mode2");
@ -666,9 +767,14 @@ bool mode1()
{ {
continue; continue;
} }
if (mPlants[i].getSensorValue() <= trigger) long raw = mPlants[i].getCurrentMoisture();
if (raw == MISSING_SENSOR)
{ {
Serial << "plant dry starting mode 2" << i << endl; continue;
}
if (raw > trigger)
{
Serial << "plant " << i << " dry " << raw << " / " << trigger << " starting mode 2" << endl;
return true; return true;
} }
} }