added raw moisture, improved pct calc , formating

This commit is contained in:
Empire 2020-11-04 21:57:40 +01:00
parent fd28ffcfaf
commit 503c2b73b9
10 changed files with 555 additions and 409 deletions

View File

@ -11,55 +11,57 @@
#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.7"
#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)
#define MOIST_SENSOR_MAX_ADC 85 * 4095 / 100
#define MOIST_SENSOR_MIN_ADC 25 * 4095 / 100
#define SOLAR_VOLT(adc) ADC_TO_VOLT_WITH_MULTI(adc, 4.0306) /**< 100k and 33k voltage dividor */ #define SOLAR_VOLT(adc) ADC_TO_VOLT_WITH_MULTI(adc, 4.0306) /**< 100k and 33k voltage dividor */
#define ADC_5V_TO_3V3(adc) ADC_TO_VOLT_WITH_MULTI(adc, 1.7) /**< 33k and 47k8 voltage dividor */ #define ADC_5V_TO_3V3(adc) ADC_TO_VOLT_WITH_MULTI(adc, 1.7) /**< 33k and 47k8 voltage dividor */
#define MS_TO_S 1000 #define MS_TO_S 1000
#define SENSOR_LIPO 34 /**< GPIO 34 (ADC1) */ #define SENSOR_LIPO 34 /**< GPIO 34 (ADC1) */
#define SENSOR_SOLAR 35 /**< GPIO 35 (ADC1) */ #define SENSOR_SOLAR 35 /**< GPIO 35 (ADC1) */
#define SENSOR_PLANT0 32 /**< GPIO 32 (ADC1) */ #define SENSOR_PLANT0 32 /**< GPIO 32 (ADC1) */
#define SENSOR_PLANT1 33 /**< GPIO 33 (ADC1) */ #define SENSOR_PLANT1 33 /**< GPIO 33 (ADC1) */
#define SENSOR_PLANT2 25 /**< GPIO 25 (ADC2) */ #define SENSOR_PLANT2 25 /**< GPIO 25 (ADC2) */
#define SENSOR_PLANT3 26 /**< GPIO 26 (ADC2) */ #define SENSOR_PLANT3 26 /**< GPIO 26 (ADC2) */
#define SENSOR_PLANT4 27 /**< GPIO 27 (ADC2) */ #define SENSOR_PLANT4 27 /**< GPIO 27 (ADC2) */
#define SENSOR_PLANT5 14 /**< GPIO 14 (ADC2) */ #define SENSOR_PLANT5 14 /**< GPIO 14 (ADC2) */
#define SENSOR_PLANT6 12 /**< GPIO 12 (ADC2) */ #define SENSOR_PLANT6 12 /**< GPIO 12 (ADC2) */
#define OUTPUT_PUMP0 23 /**< GPIO 23 */ #define OUTPUT_PUMP0 23 /**< GPIO 23 */
#define OUTPUT_PUMP1 22 /**< GPIO 22 */ #define OUTPUT_PUMP1 22 /**< GPIO 22 */
#define OUTPUT_PUMP2 21 /**< GPIO 21 */ #define OUTPUT_PUMP2 21 /**< GPIO 21 */
#define OUTPUT_PUMP3 19 /**< GPIO 19 */ #define OUTPUT_PUMP3 19 /**< GPIO 19 */
#define OUTPUT_PUMP4 18 /**< GPIO 18 */ #define OUTPUT_PUMP4 18 /**< GPIO 18 */
#define OUTPUT_PUMP5 5 /**< GPIO 5 */ #define OUTPUT_PUMP5 5 /**< GPIO 5 */
#define OUTPUT_PUMP6 15 /**< GPIO 15 */ #define OUTPUT_PUMP6 15 /**< GPIO 15 */
#define OUTPUT_SENSOR 16 /**< GPIO 16 - Enable Sensors */ #define OUTPUT_SENSOR 16 /**< GPIO 16 - Enable Sensors */
#define OUTPUT_PUMP 13 /**< GPIO 13 - Enable Pumps */ #define OUTPUT_PUMP 13 /**< GPIO 13 - Enable Pumps */
#define SENSOR_DS18B20 2 /**< GPIO 2 */ #define SENSOR_DS18B20 2 /**< GPIO 2 */
#define BUTTON 0 /**< GPIO 0 */ #define BUTTON 0 /**< GPIO 0 */
#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 MINIMUM_LIPO_VOLT 3.6f /**< 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 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 SOLAR_CHARGE_MIN_VOLTAGE 7 #define SOLAR_CHARGE_MIN_VOLTAGE 7
#define SOLAR_CHARGE_MAX_VOLTAGE 9 #define SOLAR_CHARGE_MAX_VOLTAGE 9
#define HC_SR04 /**< Ultrasonic distance sensor to measure water level */ #define HC_SR04 /**< Ultrasonic distance sensor to measure water level */
#define SENSOR_SR04_ECHO 17 /**< GPIO 17 - Echo */ #define SENSOR_SR04_ECHO 17 /**< GPIO 17 - Echo */
#define SENSOR_SR04_TRIG 23 /**< GPIO 23 - Trigger */ #define SENSOR_SR04_TRIG 23 /**< GPIO 23 - Trigger */
#define MAX_CONFIG_SETTING_ITEMS 50 /**< Parameter, that can be configured in Homie */ #define MAX_CONFIG_SETTING_ITEMS 50 /**< Parameter, that can be configured in Homie */
#define PANIK_MODE_DEEPSLEEP (60*60*5U) /**< 5 hours in usecond */ #define PANIK_MODE_DEEPSLEEP (60 * 60 * 5U) /**< 5 hours in usecond */
#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 1337

View File

@ -21,32 +21,36 @@
#include <OneWire.h> #include <OneWire.h>
class Ds18B20 { class Ds18B20
private: {
OneWire* mDs; private:
int foundDevices; OneWire *mDs;
public: int foundDevices;
Ds18B20(int pin) {
this->mDs = new OneWire(pin);
}
~Ds18B20() { public:
delete this->mDs; Ds18B20(int pin)
} {
/** this->mDs = new OneWire(pin);
}
~Ds18B20()
{
delete this->mDs;
}
/**
* @brief read amount sensots * @brief read amount sensots
* check for available of DS18B20 sensors * check for available of DS18B20 sensors
* @return amount of sensors * @return amount of sensors
*/ */
int readDevices(void); int readDevices(void);
/** /**
* @brief Read all temperatures in celsius * @brief Read all temperatures in celsius
* *
* @param pTemperatures array of float valuies * @param pTemperatures array of float valuies
* @param maxTemperatures size of the given array * @param maxTemperatures size of the given array
* @return int amount of read temperature values * @return int amount of read temperature values
*/ */
int readAllTemperatures(float* pTemperatures, int maxTemperatures); int readAllTemperatures(float *pTemperatures, int maxTemperatures);
}; };
#endif #endif

View File

@ -45,17 +45,17 @@ HomieSetting<long> waterLevelMax("watermaxlevel", "distance (mm) at maximum wate
HomieSetting<long> waterLevelMin("waterminlevel", "distance (mm) at minimum water level (pumps still covered)"); HomieSetting<long> waterLevelMin("waterminlevel", "distance (mm) at minimum water level (pumps still covered)");
HomieSetting<long> waterLevelWarn("waterlevelwarn", "warn (mm) if below this water level %"); HomieSetting<long> waterLevelWarn("waterlevelwarn", "warn (mm) if below this water level %");
HomieSetting<long> waterLevelVol("waterVolume", "(ml) between minimum and maximum"); HomieSetting<long> waterLevelVol("waterVolume", "(ml) between minimum and maximum");
HomieSetting<const char *>ntpServer("ntpServer", "NTP server (pool.ntp.org as default)"); HomieSetting<const char *> ntpServer("ntpServer", "NTP server (pool.ntp.org as default)");
/** Plant specific ones */ /** Plant specific ones */
#define GENERATE_PLANT(plant, strplant) \ #define GENERATE_PLANT(plant, strplant) \
HomieSetting<long> mSensorDry##plant = HomieSetting<long>("moistdry" strplant, "Plant " strplant "- Moist sensor dry threshold"); \ HomieSetting<long> mSensorDry##plant = HomieSetting<long>("moistdry" strplant, "Plant " strplant "- Moist sensor dry threshold"); \
HomieSetting<long> mPumpAllowedHourRangeStart##plant = HomieSetting<long>("rangehourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \ HomieSetting<long> mPumpAllowedHourRangeStart##plant = HomieSetting<long>("rangehourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \
HomieSetting<long> mPumpAllowedHourRangeEnd##plant = HomieSetting<long>("rangehourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \ HomieSetting<long> mPumpAllowedHourRangeEnd##plant = HomieSetting<long>("rangehourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \
HomieSetting<bool> mPumpOnlyWhenLowLight##plant = HomieSetting<bool>("onlyWhenLowLightZ" strplant, "Plant" strplant " - Enable the Pump only, when there is light but not enought to charge battery"); \ HomieSetting<bool> mPumpOnlyWhenLowLight##plant = HomieSetting<bool>("onlyWhenLowLightZ" strplant, "Plant" strplant " - Enable the Pump only, when there is light but not enought to charge battery"); \
HomieSetting<long> mPumpCooldownInHours##plant = HomieSetting<long>("cooldownpump" strplant, "Plant" strplant " - How long to wait until the pump is activated again (minutes)"); \ HomieSetting<long> mPumpCooldownInHours##plant = HomieSetting<long>("cooldownpump" strplant, "Plant" strplant " - How long to wait until the pump is activated again (minutes)"); \
PlantSettings_t mSetting##plant = { &mSensorDry##plant, &mPumpAllowedHourRangeStart##plant, &mPumpAllowedHourRangeEnd##plant, &mPumpOnlyWhenLowLight##plant, &mPumpCooldownInHours##plant }; PlantSettings_t mSetting##plant = {&mSensorDry##plant, &mPumpAllowedHourRangeStart##plant, &mPumpAllowedHourRangeEnd##plant, &mPumpOnlyWhenLowLight##plant, &mPumpCooldownInHours##plant};
GENERATE_PLANT(0, "0"); GENERATE_PLANT(0, "0");
GENERATE_PLANT(1, "1"); GENERATE_PLANT(1, "1");
@ -65,6 +65,4 @@ GENERATE_PLANT(4, "4");
GENERATE_PLANT(5, "5"); GENERATE_PLANT(5, "5");
GENERATE_PLANT(6, "6"); GENERATE_PLANT(6, "6");
#endif /* HOMIE_PLANT_CONFIG_H */ #endif /* HOMIE_PLANT_CONFIG_H */

View File

@ -13,14 +13,15 @@
#include <Homie.h> #include <Homie.h>
#define DEACTIVATED_PLANT 5000 #define DEACTIVATED_PLANT 5000
typedef struct PlantSettings_t { typedef struct PlantSettings_t
HomieSetting<long>* pSensorDry; {
HomieSetting<long>* pPumpAllowedHourRangeStart; HomieSetting<long> *pSensorDry;
HomieSetting<long>* pPumpAllowedHourRangeEnd; HomieSetting<long> *pPumpAllowedHourRangeStart;
HomieSetting<bool>* pPumpOnlyWhenLowLight; HomieSetting<long> *pPumpAllowedHourRangeEnd;
HomieSetting<long>* pPumpCooldownInHours; HomieSetting<bool> *pPumpOnlyWhenLowLight;
HomieSetting<long> *pPumpCooldownInHours;
} PlantSettings_t; } PlantSettings_t;
#endif #endif

View File

@ -15,17 +15,18 @@
#include "HomieTypes.h" #include "HomieTypes.h"
#include "RunningMedian.h" #include "RunningMedian.h"
class Plant { class Plant
{
private: private:
RunningMedian moistureRaw = RunningMedian(5); RunningMedian moistureRaw = RunningMedian(5);
HomieNode* mPlant = NULL; HomieNode *mPlant = NULL;
int mPinSensor=0; /**< Pin of the moist sensor */ int mPinSensor = 0; /**< Pin of the moist sensor */
int mPinPump=0; /**< Pin of the pump */ int mPinPump = 0; /**< Pin of the pump */
bool mConnected = false; bool mConnected = false;
public: public:
PlantSettings_t* mSetting; PlantSettings_t *mSetting;
/** /**
* @brief Construct a new Plant object * @brief Construct a new Plant object
* *
@ -33,9 +34,9 @@ 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,
int plantId, int plantId,
HomieNode* plant, HomieNode *plant,
PlantSettings_t* setting); PlantSettings_t *setting);
void postMQTTconnection(void); void postMQTTconnection(void);
@ -59,31 +60,39 @@ public:
* @return true * @return true
* @return false * @return false
*/ */
bool isPumpRequired() { bool isPumpRequired()
{
bool isDry = getCurrentMoisture() > getSettingsMoisture(); bool isDry = getCurrentMoisture() > getSettingsMoisture();
bool isActive = isPumpTriggerActive(); bool isActive = isPumpTriggerActive();
return isDry && isActive; return isDry && isActive;
} }
bool isPumpTriggerActive(){ bool isPumpTriggerActive()
{
return this->mSetting->pSensorDry->get() != DEACTIVATED_PLANT; return this->mSetting->pSensorDry->get() != DEACTIVATED_PLANT;
} }
float getCurrentMoisture(){ float getCurrentMoisture()
{
return this->moistureRaw.getMedian(); return this->moistureRaw.getMedian();
} }
long getSettingsMoisture(){ long getSettingsMoisture()
if(this->mSetting->pSensorDry != NULL){ {
if (this->mSetting->pSensorDry != NULL)
{
return this->mSetting->pSensorDry->get(); return this->mSetting->pSensorDry->get();
} else { }
else
{
return DEACTIVATED_PLANT; return DEACTIVATED_PLANT;
} }
} }
HomieInternals::SendingPromise& setProperty(const String& property) const { HomieInternals::SendingPromise &setProperty(const String &property) const
{
return mPlant->setProperty(property); return mPlant->setProperty(property);
} }
bool switchHandler(const HomieRange& range, const String& value); bool switchHandler(const HomieRange &range, const String &value);
void init(void); void init(void);
@ -91,16 +100,19 @@ public:
* @brief determine, if the plant was recently casted * @brief determine, if the plant was recently casted
* @param sinceLastActivation timestamp of last time * @param sinceLastActivation timestamp of last time
*/ */
bool isInCooldown(long sinceLastActivation) { bool isInCooldown(long sinceLastActivation)
{
/* if the time difference is greater than one month, we know these are initial values */ /* if the time difference is greater than one month, we know these are initial values */
if (sinceLastActivation > (60 * 60 * 24 * 30)) { if (sinceLastActivation > (60 * 60 * 24 * 30))
{
return false; return false;
} }
return (this->mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600); return (this->mSetting->pPumpCooldownInHours->get() > sinceLastActivation / 3600);
} }
bool isAllowedOnlyAtLowLight(void) { bool isAllowedOnlyAtLowLight(void)
{
return this->mSetting->pPumpOnlyWhenLowLight->get(); return this->mSetting->pPumpOnlyWhenLowLight->get();
} }
}; };

View File

@ -17,13 +17,11 @@
// not tested ==> use at own risk :) // not tested ==> use at own risk :)
// #define RUNNING_MEDIAN_USE_MALLOC // #define RUNNING_MEDIAN_USE_MALLOC
// should at least be 5 to be practical, // should at least be 5 to be practical,
// odd sizes results in a 'real' middle element and will be a bit faster. // odd sizes results in a 'real' middle element and will be a bit faster.
// even sizes takes the average of the two middle elements as median // even sizes takes the average of the two middle elements as median
#define MEDIAN_MIN_SIZE 5 #define MEDIAN_MIN_SIZE 5
#define MEDIAN_MAX_SIZE 19 #define MEDIAN_MAX_SIZE 19
class RunningMedian class RunningMedian
{ {
@ -45,7 +43,7 @@ public:
float getAverage(uint8_t nMedian); float getAverage(uint8_t nMedian);
float getHighest() { return getSortedElement(_cnt - 1); }; float getHighest() { return getSortedElement(_cnt - 1); };
float getLowest() { return getSortedElement(0); }; float getLowest() { return getSortedElement(0); };
// get n'th element from the values in time order // get n'th element from the values in time order
float getElement(const uint8_t n); float getElement(const uint8_t n);
@ -58,7 +56,6 @@ public:
// returns current used elements, getCount() <= getSize() // returns current used elements, getCount() <= getSize()
uint8_t getCount() { return _cnt; }; uint8_t getCount() { return _cnt; };
protected: protected:
boolean _sorted; boolean _sorted;
uint8_t _size; uint8_t _size;
@ -66,8 +63,8 @@ protected:
uint8_t _idx; uint8_t _idx;
#ifdef RUNNING_MEDIAN_USE_MALLOC #ifdef RUNNING_MEDIAN_USE_MALLOC
float * _ar; float *_ar;
uint8_t * _p; uint8_t *_p;
#else #else
float _ar[MEDIAN_MAX_SIZE]; float _ar[MEDIAN_MAX_SIZE];
uint8_t _p[MEDIAN_MAX_SIZE]; uint8_t _p[MEDIAN_MAX_SIZE];

View File

@ -11,40 +11,45 @@
#include "DS18B20.h" #include "DS18B20.h"
#define STARTCONV 0x44 #define STARTCONV 0x44
#define READSCRATCH 0xBE // Read EEPROM #define READSCRATCH 0xBE // Read EEPROM
#define TEMP_LSB 0 #define TEMP_LSB 0
#define TEMP_MSB 1 #define TEMP_MSB 1
#define SCRATCHPADSIZE 9 #define SCRATCHPADSIZE 9
#define OFFSET_CRC8 8 /**< 9th byte has the CRC of the complete data */ #define OFFSET_CRC8 8 /**< 9th byte has the CRC of the complete data */
//Printf debugging //Printf debugging
//#define DS_DEBUG //#define DS_DEBUG
int Ds18B20::readDevices() { int Ds18B20::readDevices()
{
byte addr[8]; byte addr[8];
int amount = -1; int amount = -1;
while (this->mDs->search(addr)) { while (this->mDs->search(addr))
{
amount++; amount++;
} }
this->mDs->reset_search(); this->mDs->reset_search();
return amount; return amount;
} }
int Ds18B20::readAllTemperatures(float* pTemperatures, int maxTemperatures) { int Ds18B20::readAllTemperatures(float *pTemperatures, int maxTemperatures)
{
byte addr[8]; byte addr[8];
uint8_t scratchPad[SCRATCHPADSIZE]; uint8_t scratchPad[SCRATCHPADSIZE];
int currentTemp = 0; int currentTemp = 0;
while (this->mDs->search(addr)) { while (this->mDs->search(addr))
{
this->mDs->reset(); this->mDs->reset();
this->mDs->select(addr); this->mDs->select(addr);
this->mDs->write(STARTCONV); this->mDs->write(STARTCONV);
} }
delay(750); delay(750);
while (this->mDs->search(addr)) { while (this->mDs->search(addr))
{
this->mDs->reset(); this->mDs->reset();
this->mDs->select(addr); this->mDs->select(addr);
this->mDs->write(READSCRATCH); this->mDs->write(READSCRATCH);
@ -62,23 +67,27 @@ int Ds18B20::readAllTemperatures(float* pTemperatures, int maxTemperatures) {
// byte 7: DS18S20: COUNT_PER_C // byte 7: DS18S20: COUNT_PER_C
// DS18B20 & DS1822: store for crc // DS18B20 & DS1822: store for crc
// byte 8: SCRATCHPAD_CRC // byte 8: SCRATCHPAD_CRC
for (uint8_t i = 0; i < 9; i++) { for (uint8_t i = 0; i < 9; i++)
{
scratchPad[i] = this->mDs->read(); scratchPad[i] = this->mDs->read();
} }
uint8_t crc8 = this->mDs->crc8(scratchPad, 8); uint8_t crc8 = this->mDs->crc8(scratchPad, 8);
/* Only work an valid data */ /* Only work an valid data */
if (crc8 == scratchPad[OFFSET_CRC8]) { if (crc8 == scratchPad[OFFSET_CRC8])
int16_t fpTemperature = (((int16_t) scratchPad[TEMP_MSB]) << 11) {
| (((int16_t) scratchPad[TEMP_LSB]) << 3); int16_t fpTemperature = (((int16_t)scratchPad[TEMP_MSB]) << 11) | (((int16_t)scratchPad[TEMP_LSB]) << 3);
float celsius = (float) fpTemperature * 0.0078125; float celsius = (float)fpTemperature * 0.0078125;
#ifdef DS_DEBUG #ifdef DS_DEBUG
Serial.printf("\r\nTemp%d %f °C (Raw: %d, %x =? %x)\r\n", (currentTemp+1), celsius, fpTemperature, crc8, scratchPad[8]); Serial.printf("\r\nTemp%d %f °C (Raw: %d, %x =? %x)\r\n", (currentTemp + 1), celsius, fpTemperature, crc8, scratchPad[8]);
#endif #endif
/* check, if the buffer as some space for our data */ /* check, if the buffer as some space for our data */
if (currentTemp < maxTemperatures) { if (currentTemp < maxTemperatures)
{
pTemperatures[currentTemp] = celsius; pTemperatures[currentTemp] = celsius;
} else { }
else
{
return -1; return -1;
} }
} }

View File

@ -12,31 +12,33 @@
#include "PlantCtrl.h" #include "PlantCtrl.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)
{
this->mPinSensor = pinSensor; this->mPinSensor = pinSensor;
this->mPinPump = pinPump; this->mPinPump = pinPump;
this->mPlant = plant; this->mPlant = plant;
this->mSetting = setting; this->mSetting = setting;
} }
void Plant::init(void) { void Plant::init(void)
{
/* Initialize Home Settings validator */ /* Initialize Home Settings validator */
this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT); this->mSetting->pSensorDry->setDefaultValue(DEACTIVATED_PLANT);
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);
}); });
this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00 this->mSetting->pPumpAllowedHourRangeStart->setDefaultValue(8); // start at 8:00
this->mSetting->pPumpAllowedHourRangeStart->setValidator([] (long candidate) { this->mSetting->pPumpAllowedHourRangeStart->setValidator([](long candidate) {
return ((candidate >= 0) && (candidate <= 23) ); return ((candidate >= 0) && (candidate <= 23));
}); });
this->mSetting->pPumpAllowedHourRangeEnd->setDefaultValue(20); // stop pumps at 20:00 this->mSetting->pPumpAllowedHourRangeEnd->setDefaultValue(20); // stop pumps at 20:00
this->mSetting->pPumpAllowedHourRangeEnd->setValidator([] (long candidate) { this->mSetting->pPumpAllowedHourRangeEnd->setValidator([](long candidate) {
return ((candidate >= 0) && (candidate <= 23) ); return ((candidate >= 0) && (candidate <= 23));
}); });
this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(true); this->mSetting->pPumpOnlyWhenLowLight->setDefaultValue(true);
this->mSetting->pPumpCooldownInHours->setDefaultValue(20); // minutes this->mSetting->pPumpCooldownInHours->setDefaultValue(20); // minutes
this->mSetting->pPumpCooldownInHours->setValidator([] (long candidate) { this->mSetting->pPumpCooldownInHours->setValidator([](long candidate) {
return ((candidate >= 0) && (candidate <= 1024) ); return ((candidate >= 0) && (candidate <= 1024));
}); });
/* Initialize Hardware */ /* Initialize Hardware */
@ -45,43 +47,47 @@ void Plant::init(void) {
digitalWrite(this->mPinPump, LOW); digitalWrite(this->mPinPump, LOW);
} }
void Plant::addSenseValue(void) { void Plant::addSenseValue(void)
this->moistureRaw.add( analogRead(this->mPinSensor) ); {
this->moistureRaw.add(analogRead(this->mPinSensor));
} }
void Plant::postMQTTconnection(void) { void Plant::postMQTTconnection(void)
{
const String OFF = String("OFF"); const String OFF = String("OFF");
this->mConnected=true; this->mConnected = true;
this->mPlant->setProperty("switch").send(OFF); this->mPlant->setProperty("switch").send(OFF);
} }
void Plant::deactivatePump(void) { void Plant::deactivatePump(void)
{
digitalWrite(this->mPinPump, LOW); digitalWrite(this->mPinPump, LOW);
if (this->mConnected) { if (this->mConnected)
{
const String OFF = String("OFF"); const String OFF = String("OFF");
this->mPlant->setProperty("switch").send(OFF); this->mPlant->setProperty("switch").send(OFF);
} }
} }
void Plant::activatePump(void) { void Plant::activatePump(void)
{
digitalWrite(this->mPinPump, HIGH); digitalWrite(this->mPinPump, HIGH);
if (this->mConnected) { if (this->mConnected)
{
const String OFF = String("ON"); const String OFF = String("ON");
this->mPlant->setProperty("switch").send(OFF); this->mPlant->setProperty("switch").send(OFF);
} }
} }
void Plant::advertise(void) { void Plant::advertise(void)
{
// Advertise topics // Advertise topics
this->mPlant->advertise("switch").setName("Pump 1") this->mPlant->advertise("switch").setName("Pump 1").setDatatype("boolean");
.setDatatype("boolean");
//FIXME add .settable(this->switchHandler) //FIXME add .settable(this->switchHandler)
this->mPlant->advertise("moist").setName("Percent") this->mPlant->advertise("moist").setName("Percent").setDatatype("number").setUnit("%");
.setDatatype("number") this->mPlant->advertise("moistraw").setName("adc").setDatatype("number").setUnit("3.3/4096V");
.setUnit("%");
} }
/* FIXME /* FIXME
bool Plant::switchHandler(const HomieRange& range, const String& value) { bool Plant::switchHandler(const HomieRange& range, const String& value) {
if (range.isRange) return false; // only one switch is present if (range.isRange) return false; // only one switch is present

View File

@ -31,8 +31,8 @@ RunningMedian::RunningMedian(const uint8_t size)
_size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE); _size = constrain(size, MEDIAN_MIN_SIZE, MEDIAN_MAX_SIZE);
#ifdef RUNNING_MEDIAN_USE_MALLOC #ifdef RUNNING_MEDIAN_USE_MALLOC
_ar = (float *) malloc(_size * sizeof(float)); _ar = (float *)malloc(_size * sizeof(float));
_p = (uint8_t *) malloc(_size * sizeof(uint8_t)); _p = (uint8_t *)malloc(_size * sizeof(uint8_t));
#endif #endif
clear(); clear();
@ -63,18 +63,22 @@ void RunningMedian::clear()
void RunningMedian::add(float value) void RunningMedian::add(float value)
{ {
_ar[_idx++] = value; _ar[_idx++] = value;
if (_idx >= _size) _idx = 0; // wrap around if (_idx >= _size)
if (_cnt < _size) _cnt++; _idx = 0; // wrap around
if (_cnt < _size)
_cnt++;
_sorted = false; _sorted = false;
} }
float RunningMedian::getMedian() float RunningMedian::getMedian()
{ {
if (_cnt == 0) return NAN; if (_cnt == 0)
return NAN;
if (_sorted == false) sort(); if (_sorted == false)
sort();
if (_cnt & 0x01) // is it odd sized? if (_cnt & 0x01) // is it odd sized?
{ {
return _ar[_p[_cnt / 2]]; return _ar[_p[_cnt / 2]];
} }
@ -83,7 +87,8 @@ float RunningMedian::getMedian()
float RunningMedian::getAverage() float RunningMedian::getAverage()
{ {
if (_cnt == 0) return NAN; if (_cnt == 0)
return NAN;
float sum = 0; float sum = 0;
for (uint8_t i = 0; i < _cnt; i++) for (uint8_t i = 0; i < _cnt; i++)
@ -95,13 +100,16 @@ float RunningMedian::getAverage()
float RunningMedian::getAverage(uint8_t nMedians) float RunningMedian::getAverage(uint8_t nMedians)
{ {
if ((_cnt == 0) || (nMedians == 0)) return NAN; if ((_cnt == 0) || (nMedians == 0))
return NAN;
if (_cnt < nMedians) nMedians = _cnt; // when filling the array for first time if (_cnt < nMedians)
nMedians = _cnt; // when filling the array for first time
uint8_t start = ((_cnt - nMedians) / 2); uint8_t start = ((_cnt - nMedians) / 2);
uint8_t stop = start + nMedians; uint8_t stop = start + nMedians;
if (_sorted == false) sort(); if (_sorted == false)
sort();
float sum = 0; float sum = 0;
for (uint8_t i = start; i < stop; i++) for (uint8_t i = start; i < stop; i++)
@ -113,7 +121,8 @@ float RunningMedian::getAverage(uint8_t nMedians)
float RunningMedian::getElement(const uint8_t n) float RunningMedian::getElement(const uint8_t n)
{ {
if ((_cnt == 0) || (n >= _cnt)) return NAN; if ((_cnt == 0) || (n >= _cnt))
return NAN;
uint8_t pos = _idx + n; uint8_t pos = _idx + n;
if (pos >= _cnt) // faster than % if (pos >= _cnt) // faster than %
@ -125,18 +134,21 @@ float RunningMedian::getElement(const uint8_t n)
float RunningMedian::getSortedElement(const uint8_t n) float RunningMedian::getSortedElement(const uint8_t n)
{ {
if ((_cnt == 0) || (n >= _cnt)) return NAN; if ((_cnt == 0) || (n >= _cnt))
return NAN;
if (_sorted == false) sort(); if (_sorted == false)
sort();
return _ar[_p[n]]; return _ar[_p[n]];
} }
// n can be max <= half the (filled) size // n can be max <= half the (filled) size
float RunningMedian::predict(const uint8_t n) float RunningMedian::predict(const uint8_t n)
{ {
if ((_cnt == 0) || (n >= _cnt / 2)) return NAN; if ((_cnt == 0) || (n >= _cnt / 2))
return NAN;
float med = getMedian(); // takes care of sorting ! float med = getMedian(); // takes care of sorting !
if (_cnt & 0x01) if (_cnt & 0x01)
{ {
return max(med - _ar[_p[_cnt / 2 - n]], _ar[_p[_cnt / 2 + n]] - med); return max(med - _ar[_p[_cnt / 2 - n]], _ar[_p[_cnt / 2 + n]] - med);
@ -162,7 +174,8 @@ void RunningMedian::sort()
flag = false; flag = false;
} }
} }
if (flag) break; if (flag)
break;
} }
_sorted = true; _sorted = true;
} }

View File

@ -16,52 +16,48 @@
#include "time.h" #include "time.h"
#include "esp_sleep.h" #include "esp_sleep.h"
#include "RunningMedian.h" #include "RunningMedian.h"
#include <arduino-timer.h>
#include <stdint.h> #include <stdint.h>
const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */ const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */
#define AMOUNT_SENOR_QUERYS 8 #define AMOUNT_SENOR_QUERYS 8
#define SENSOR_QUERY_SHIFTS 3 #define SENSOR_QUERY_SHIFTS 3
#define SOLAR4SENSORS 6.0f #define SOLAR4SENSORS 6.0f
#define TEMP_INIT_VALUE -999.0f #define TEMP_INIT_VALUE -999.0f
#define TEMP_MAX_VALUE 85.0f #define TEMP_MAX_VALUE 85.0f
#define HalfHour 60 #define HalfHour 60
/********************* non volatile enable after deepsleep *******************************/ /********************* non volatile enable after deepsleep *******************************/
RTC_DATA_ATTR long gotoMode2AfterThisTimestamp = 0; 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 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 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 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 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 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 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 int lastPumpRunning = 0; RTC_DATA_ATTR int lastPumpRunning = 0;
RTC_DATA_ATTR long lastWaterValue = 0; RTC_DATA_ATTR long lastWaterValue = 0;
bool warmBoot = true; bool warmBoot = true;
bool volatile mode3Active = false; /**< Controller must not sleep */ bool volatile mode3Active = false; /**< Controller must not sleep */
bool volatile mDeepsleep = false; bool volatile mDeepsleep = false;
int plantSensor1 = 0; int plantSensor1 = 0;
int mWaterGone = -1; /**< Amount of centimeter, where no water is seen */ int mWaterGone = -1; /**< Amount of centimeter, where no water is seen */
int readCounter = 0; int readCounter = 0;
bool mConfigured = false; bool mConfigured = false;
auto wait4sleep = timer_create_default(); // create a timer with default settings
RTC_DATA_ATTR int gBootCount = 0; RTC_DATA_ATTR int gBootCount = 0;
RTC_DATA_ATTR int gCurrentPlant = 0; /**< Value Range: 1 ... 7 (0: no plant needs water) */ RTC_DATA_ATTR int gCurrentPlant = 0; /**< Value Range: 1 ... 7 (0: no plant needs water) */
@ -71,52 +67,61 @@ RunningMedian waterRawSensor = RunningMedian(5);
RunningMedian temp1 = RunningMedian(5); RunningMedian temp1 = RunningMedian(5);
RunningMedian temp2 = RunningMedian(5); RunningMedian temp2 = RunningMedian(5);
Ds18B20 dallas(SENSOR_DS18B20); Ds18B20 dallas(SENSOR_DS18B20);
Plant mPlants[MAX_PLANTS] = { Plant mPlants[MAX_PLANTS] = {
Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0), Plant(SENSOR_PLANT0, OUTPUT_PUMP0, 0, &plant0, &mSetting0),
Plant(SENSOR_PLANT1, OUTPUT_PUMP1, 1, &plant1, &mSetting1), Plant(SENSOR_PLANT1, OUTPUT_PUMP1, 1, &plant1, &mSetting1),
Plant(SENSOR_PLANT2, OUTPUT_PUMP2, 2, &plant2, &mSetting2), Plant(SENSOR_PLANT2, OUTPUT_PUMP2, 2, &plant2, &mSetting2),
Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3, &plant3, &mSetting3), Plant(SENSOR_PLANT3, OUTPUT_PUMP3, 3, &plant3, &mSetting3),
Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4, &plant4, &mSetting4), Plant(SENSOR_PLANT4, OUTPUT_PUMP4, 4, &plant4, &mSetting4),
Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5, &plant5, &mSetting5), Plant(SENSOR_PLANT5, OUTPUT_PUMP5, 5, &plant5, &mSetting5),
Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6) Plant(SENSOR_PLANT6, OUTPUT_PUMP6, 6, &plant6, &mSetting6)};
};
float getBatteryVoltage(){ float getBatteryVoltage()
{
return ADC_5V_TO_3V3(lipoRawSensor.getAverage()); return ADC_5V_TO_3V3(lipoRawSensor.getAverage());
} }
float getSolarVoltage(){ float getSolarVoltage()
{
return SOLAR_VOLT(solarRawSensor.getAverage()); return SOLAR_VOLT(solarRawSensor.getAverage());
} }
void setMoistureTrigger(int plantId, long value){ void setMoistureTrigger(int plantId, long value)
if(plantId == 0){ {
if (plantId == 0)
{
rtcMoistureTrigger0 = value; rtcMoistureTrigger0 = value;
} }
if(plantId == 1){ if (plantId == 1)
{
rtcMoistureTrigger1 = value; rtcMoistureTrigger1 = value;
} }
if(plantId == 2){ if (plantId == 2)
{
rtcMoistureTrigger2 = value; rtcMoistureTrigger2 = value;
} }
if(plantId == 3){ if (plantId == 3)
{
rtcMoistureTrigger3 = value; rtcMoistureTrigger3 = value;
} }
if(plantId == 4){ if (plantId == 4)
{
rtcMoistureTrigger4 = value; rtcMoistureTrigger4 = value;
} }
if(plantId == 5){ if (plantId == 5)
{
rtcMoistureTrigger5 = value; rtcMoistureTrigger5 = value;
} }
if(plantId == 6){ if (plantId == 6)
{
rtcMoistureTrigger6 = value; rtcMoistureTrigger6 = value;
} }
} }
void readSystemSensors() { void readSystemSensors()
{
lipoRawSensor.add(analogRead(SENSOR_LIPO)); lipoRawSensor.add(analogRead(SENSOR_LIPO));
solarRawSensor.add(analogRead(SENSOR_SOLAR)); solarRawSensor.add(analogRead(SENSOR_SOLAR));
} }
@ -124,214 +129,265 @@ void readSystemSensors() {
int determineNextPump(); int determineNextPump();
void setLastActivationForPump(int pumpId, long time); void setLastActivationForPump(int pumpId, long time);
long getCurrentTime()
long getCurrentTime(){ {
struct timeval tv_now; struct timeval tv_now;
gettimeofday(&tv_now, NULL); gettimeofday(&tv_now, NULL);
return tv_now.tv_sec; return tv_now.tv_sec;
} }
void espDeepSleepFor(long seconds, bool activatePump = false){ void espDeepSleepFor(long seconds, bool activatePump = false)
if(mode3Active){ {
if (mode3Active)
{
Serial << "abort deepsleep, mode3Active" << endl; Serial << "abort deepsleep, mode3Active" << endl;
return; return;
} }
for(int i = 0;i<10;i++){ for (int i = 0; i < 10; i++)
{
long cTime = getCurrentTime(); long cTime = getCurrentTime();
if(cTime < 100000){ if (cTime < 100000)
{
Serial << "Wait for ntp" << endl; Serial << "Wait for ntp" << endl;
delay(100); delay(100);
} else { }
else
{
break; break;
} }
} }
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,ESP_PD_OPTION_ON); esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_ON);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
if (activatePump) { if (activatePump)
{
gpio_deep_sleep_hold_en(); gpio_deep_sleep_hold_en();
gpio_hold_en(GPIO_NUM_13); //pump pwr gpio_hold_en(GPIO_NUM_13); //pump pwr
} else { }
else
{
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);
for (int i=0; i < MAX_PLANTS; i++) { for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].deactivatePump(); mPlants[i].deactivatePump();
} }
} }
//gpio_hold_en(GPIO_NUM_23); //p0 //gpio_hold_en(GPIO_NUM_23); //p0
//FIXME fix for outher outputs //FIXME fix for outher outputs
Serial.print("Trying to sleep for "); Serial.print("Trying to sleep for ");
Serial.print(seconds); Serial.print(seconds);
Serial.println(" seconds"); Serial.println(" seconds");
esp_sleep_enable_timer_wakeup( (seconds * 1000U * 1000U) ); esp_sleep_enable_timer_wakeup((seconds * 1000U * 1000U));
mDeepsleep=true; mDeepsleep = true;
} }
void mode2MQTT()
{
void mode2MQTT(){
readSystemSensors(); readSystemSensors();
digitalWrite(OUTPUT_PUMP, LOW); digitalWrite(OUTPUT_PUMP, LOW);
for (int i=0; i < MAX_PLANTS; i++) { for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].deactivatePump(); mPlants[i].deactivatePump();
} }
if (deepSleepTime.get()) { if (deepSleepTime.get())
{
Serial << "deepsleep time is configured to " << deepSleepTime.get() << endl; Serial << "deepsleep time is configured to " << deepSleepTime.get() << endl;
} }
/* Publish default values */ /* Publish default values */
if(lastPumpRunning != -1){ if (lastPumpRunning != -1)
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++) { for (int i = 0; i < MAX_PLANTS; i++)
mPlants[i].setProperty("moist").send(String(100 * mPlants[i].getSensorValue() / 4095 )); {
long pct = 100 - map(mPlants[i].getSensorValue(), MOIST_SENSOR_MIN_ADC, MOIST_SENSOR_MAX_ADC, 0, 100);
if (pct < 0)
{
pct = 0;
}
if (pct > 100)
{
pct = 100;
}
mPlants[i].setProperty("moist").send(String(pct));
mPlants[i].setProperty("moistraw").send(String(mPlants[i].getSensorValue()));
} }
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;
sensorLipo.setProperty("percent").send( String(100 * lipoRawSensor.getAverage() / 4095) ); sensorLipo.setProperty("percent").send(String(100 * lipoRawSensor.getAverage() / 4095));
sensorLipo.setProperty("volt").send( String(getBatteryVoltage()) ); sensorLipo.setProperty("volt").send(String(getBatteryVoltage()));
sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage() ) / 4095)); sensorSolar.setProperty("percent").send(String((100 * solarRawSensor.getAverage()) / 4095));
sensorSolar.setProperty("volt").send( String(getSolarVoltage()) ); sensorSolar.setProperty("volt").send(String(getSolarVoltage()));
float t1 = temp1.getMedian(); float t1 = temp1.getMedian();
if (t1 != NAN) { if (t1 != NAN)
sensorTemp.setProperty("control").send( String(t1)); {
sensorTemp.setProperty("control").send(String(t1));
} }
float t2 = temp2.getMedian(); float t2 = temp2.getMedian();
if (t2 != NAN) { if (t2 != NAN)
sensorTemp.setProperty("temp").send( String(t2)); {
sensorTemp.setProperty("temp").send(String(t2));
} }
//give mqtt time, use via publish callback instead? //give mqtt time, use via publish callback instead?
delay(100); delay(100);
bool lipoTempWarning = t1!=85 && t2!=85 && abs(t1 - t2) > 10; bool lipoTempWarning = t1 != 85 && t2 != 85 && abs(t1 - t2) > 10;
if(lipoTempWarning){ if (lipoTempWarning)
{
Serial.println("Lipo temp incorrect, panic mode deepsleep TODO"); Serial.println("Lipo temp incorrect, panic mode deepsleep TODO");
//espDeepSleepFor(PANIK_MODE_DEEPSLEEP); //espDeepSleepFor(PANIK_MODE_DEEPSLEEP);
//return; //return;
} }
for(int i=0; i < MAX_PLANTS; i++) { for (int i = 0; i < MAX_PLANTS; i++)
{
setMoistureTrigger(i, mPlants[i].mSetting->pSensorDry->get()); setMoistureTrigger(i, mPlants[i].mSetting->pSensorDry->get());
} }
bool hasWater = true; //FIXMEmWaterGone > waterLevelMin.get();
bool hasWater = true;//FIXMEmWaterGone > waterLevelMin.get();
//FIXME no water warning message //FIXME no water warning message
lastPumpRunning = determineNextPump(); lastPumpRunning = determineNextPump();
if(lastPumpRunning != -1 && !hasWater){ if (lastPumpRunning != -1 && !hasWater)
{
Serial.println("Want to pump but no water"); Serial.println("Want to pump but no water");
} }
if(lastPumpRunning != -1 && hasWater){ if (lastPumpRunning != -1 && hasWater)
{
digitalWrite(OUTPUT_PUMP, HIGH); digitalWrite(OUTPUT_PUMP, HIGH);
setLastActivationForPump(lastPumpRunning, getCurrentTime()); setLastActivationForPump(lastPumpRunning, getCurrentTime());
mPlants[lastPumpRunning].activatePump(); mPlants[lastPumpRunning].activatePump();
} }
if(lastPumpRunning == -1 || !hasWater){ if (lastPumpRunning == -1 || !hasWater)
if(getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE){ {
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepNightTime.get(); if (getSolarVoltage() < SOLAR_CHARGE_MIN_VOLTAGE)
{
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepNightTime.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 { }
gotoMode2AfterThisTimestamp = getCurrentTime()+deepSleepTime.get(); else
{
gotoMode2AfterThisTimestamp = getCurrentTime() + deepSleepTime.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();
} }
}
}else { else
{
gotoMode2AfterThisTimestamp = 0; gotoMode2AfterThisTimestamp = 0;
Serial.println("Running pump, watering deepsleep"); Serial.println("Running pump, watering deepsleep");
espDeepSleepFor(wateringDeepSleep.get(), true); espDeepSleepFor(wateringDeepSleep.get(), true);
} }
} }
long getMoistureTrigger(int plantId){ long getMoistureTrigger(int plantId)
if(plantId == 0){ {
if (plantId == 0)
{
return rtcMoistureTrigger0; return rtcMoistureTrigger0;
} }
if(plantId == 1){ if (plantId == 1)
{
return rtcMoistureTrigger1; return rtcMoistureTrigger1;
} }
if(plantId == 2){ if (plantId == 2)
{
return rtcMoistureTrigger2; return rtcMoistureTrigger2;
} }
if(plantId == 3){ if (plantId == 3)
{
return rtcMoistureTrigger3; return rtcMoistureTrigger3;
} }
if(plantId == 4){ if (plantId == 4)
{
return rtcMoistureTrigger4; return rtcMoistureTrigger4;
} }
if(plantId == 5){ if (plantId == 5)
{
return rtcMoistureTrigger5; return rtcMoistureTrigger5;
} }
if(plantId == 6){ if (plantId == 6)
{
return rtcMoistureTrigger6; return rtcMoistureTrigger6;
} }
return -1; return -1;
} }
void setLastActivationForPump(int plantId, long value)
{
void setLastActivationForPump(int plantId, long value){ if (plantId == 0)
if(plantId == 0){ {
rtcLastActive0 = value; rtcLastActive0 = value;
} }
if(plantId == 1){ if (plantId == 1)
{
rtcLastActive1 = value; rtcLastActive1 = value;
} }
if(plantId == 2){ if (plantId == 2)
{
rtcLastActive2 = value; rtcLastActive2 = value;
} }
if(plantId == 3){ if (plantId == 3)
{
rtcLastActive3 = value; rtcLastActive3 = value;
} }
if(plantId == 4){ if (plantId == 4)
{
rtcLastActive4 = value; rtcLastActive4 = value;
} }
if(plantId == 5){ if (plantId == 5)
{
rtcLastActive5 = value; rtcLastActive5 = value;
} }
if(plantId == 6){ if (plantId == 6)
{
rtcLastActive6 = value; rtcLastActive6 = value;
} }
} }
long getLastActivationForPump(int plantId){ long getLastActivationForPump(int plantId)
if(plantId == 0){ {
if (plantId == 0)
{
return rtcLastActive0; return rtcLastActive0;
} }
if(plantId == 1){ if (plantId == 1)
{
return rtcLastActive1; return rtcLastActive1;
} }
if(plantId == 2){ if (plantId == 2)
{
return rtcLastActive2; return rtcLastActive2;
} }
if(plantId == 3){ if (plantId == 3)
{
return rtcLastActive3; return rtcLastActive3;
} }
if(plantId == 4){ if (plantId == 4)
{
return rtcLastActive4; return rtcLastActive4;
} }
if(plantId == 5){ if (plantId == 5)
{
return rtcLastActive5; return rtcLastActive5;
} }
if(plantId == 6){ if (plantId == 6)
{
return rtcLastActive6; return rtcLastActive6;
} }
return -1; return -1;
@ -341,7 +397,8 @@ 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() { void readSensors()
{
Serial << "Read Sensors" << endl; Serial << "Read Sensors" << endl;
readSystemSensors(); readSystemSensors();
@ -352,8 +409,10 @@ void readSensors() {
delay(100); delay(100);
/* wait before reading something */ /* wait before reading something */
for (int readCnt=0;readCnt < AMOUNT_SENOR_QUERYS; readCnt++) { for (int readCnt = 0; readCnt < AMOUNT_SENOR_QUERYS; readCnt++)
for(int i=0; i < MAX_PLANTS; i++) { {
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].addSenseValue(); mPlants[i].addSenseValue();
} }
} }
@ -363,21 +422,21 @@ void readSensors() {
Serial << "DS18B20" << String(dallas.readDevices()) << endl; Serial << "DS18B20" << String(dallas.readDevices()) << endl;
delay(200); delay(200);
/* Required to read the temperature once */ /* Required to read the temperature once */
float temp[2] = {TEMP_MAX_VALUE, TEMP_MAX_VALUE}; float temp[2] = {TEMP_MAX_VALUE, TEMP_MAX_VALUE};
float* pFloat = temp; float *pFloat = temp;
int sensors = dallas.readAllTemperatures(pFloat, 2); int sensors = dallas.readAllTemperatures(pFloat, 2);
if (sensors > 0) { if (sensors > 0)
{
Serial << "t1: " << String(temp[0]) << endl; Serial << "t1: " << String(temp[0]) << endl;
temp1.add(temp[0]); temp1.add(temp[0]);
} }
if (sensors > 1) { if (sensors > 1)
{
Serial << "t2: " << String(temp[1]) << endl; Serial << "t2: " << String(temp[1]) << endl;
temp2.add(temp[1]); temp2.add(temp[1]);
} }
/* Use the Ultrasonic sensor to measure waterLevel */ /* Use the Ultrasonic sensor to measure waterLevel */
digitalWrite(SENSOR_SR04_TRIG, LOW); digitalWrite(SENSOR_SR04_TRIG, LOW);
@ -386,89 +445,100 @@ void readSensors() {
delayMicroseconds(10); delayMicroseconds(10);
digitalWrite(SENSOR_SR04_TRIG, LOW); digitalWrite(SENSOR_SR04_TRIG, LOW);
float duration = pulseIn(SENSOR_SR04_ECHO, HIGH); float duration = pulseIn(SENSOR_SR04_ECHO, HIGH);
waterRawSensor.add((duration*.343)/2); waterRawSensor.add((duration * .343) / 2);
/* deactivate the sensors */ /* deactivate the sensors */
digitalWrite(OUTPUT_SENSOR, LOW); digitalWrite(OUTPUT_SENSOR, LOW);
} }
//Homie.getMqttClient().disconnect(); //Homie.getMqttClient().disconnect();
void onHomieEvent(const HomieEvent& event) { void onHomieEvent(const HomieEvent &event)
switch(event.type) { {
case HomieEventType::SENDING_STATISTICS: switch (event.type)
Homie.getLogger() << "My statistics" << endl; {
break; case HomieEventType::SENDING_STATISTICS:
case HomieEventType::MQTT_READY: Homie.getLogger() << "My statistics" << endl;
Serial.printf("NTP Setup with server %s\r\n", ntpServer.get()); break;
configTime(0, 0, ntpServer.get()); case HomieEventType::MQTT_READY:
//wait for rtc sync? Serial.printf("NTP Setup with server %s\r\n", ntpServer.get());
rtcDeepSleepTime = deepSleepTime.get(); configTime(0, 0, ntpServer.get());
Serial << "Setup plants" << endl; //wait for rtc sync?
for(int i=0; i < MAX_PLANTS; i++) { rtcDeepSleepTime = deepSleepTime.get();
mPlants[i].postMQTTconnection(); Serial << "Setup plants" << endl;
} for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].postMQTTconnection();
}
mode2MQTT(); mode2MQTT();
break; break;
case HomieEventType::READY_TO_SLEEP: case HomieEventType::READY_TO_SLEEP:
Homie.getLogger() << "rtsleep" << endl; Homie.getLogger() << "rtsleep" << endl;
esp_deep_sleep_start(); esp_deep_sleep_start();
break; break;
case HomieEventType::OTA_STARTED: case HomieEventType::OTA_STARTED:
Homie.getLogger() << "OTA started" << endl; Homie.getLogger() << "OTA started" << endl;
digitalWrite(OUTPUT_SENSOR, HIGH); digitalWrite(OUTPUT_SENSOR, HIGH);
digitalWrite(OUTPUT_PUMP, HIGH); digitalWrite(OUTPUT_PUMP, HIGH);
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();
for (int i=0; i < MAX_PLANTS; i++) { for (int i = 0; i < MAX_PLANTS; i++)
mPlants[i].deactivatePump(); {
} mPlants[i].deactivatePump();
mode3Active=true; }
break; mode3Active = true;
case HomieEventType::OTA_SUCCESSFUL: break;
case HomieEventType::OTA_SUCCESSFUL:
Homie.getLogger() << "OTA successfull" << endl; Homie.getLogger() << "OTA successfull" << endl;
digitalWrite(OUTPUT_SENSOR, LOW); digitalWrite(OUTPUT_SENSOR, LOW);
digitalWrite(OUTPUT_PUMP, LOW); digitalWrite(OUTPUT_PUMP, LOW);
ESP.restart(); ESP.restart();
break; break;
default: default:
break; break;
} }
} }
int determineNextPump(){ int determineNextPump()
{
float solarValue = getSolarVoltage(); float solarValue = getSolarVoltage();
bool isLowLight =(solarValue > SOLAR_CHARGE_MIN_VOLTAGE || 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?
int pumpToUse = -1; int pumpToUse = -1;
for(int i=0; i < MAX_PLANTS; i++) { for (int i = 0; i < MAX_PLANTS; i++)
{
Plant plant = mPlants[i]; Plant plant = mPlants[i];
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(plant.isInCooldown(sinceLastActivation) ){ if (plant.isInCooldown(sinceLastActivation))
Serial.printf("%d Skipping due to cooldown\r\n", i); {
Serial.printf("%d Skipping due to cooldown %ld \r\n", i, sinceLastActivation);
setMoistureTrigger(i, DEACTIVATED_PLANT); setMoistureTrigger(i, DEACTIVATED_PLANT);
continue; continue;
} }
//skip as it is not low light //skip as it is not low light
if(!isLowLight && plant.isAllowedOnlyAtLowLight()){ if (!isLowLight && plant.isAllowedOnlyAtLowLight())
{
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.isPumpRequired()){ if (plant.isPumpRequired())
{
Serial.printf("%d Requested pumping\r\n", i); Serial.printf("%d Requested pumping\r\n", i);
pumpToUse = i; pumpToUse = i;
} }
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: disabled trigger %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 trigger\r\n", i);
} }
} }
return pumpToUse; return pumpToUse;
} }
@ -481,22 +551,28 @@ int determineNextPump(){
* @return true when the command was parsed and executed succuessfully * @return true when the command was parsed and executed succuessfully
* @return false on errors when parsing the request * @return false on errors when parsing the request
*/ */
bool aliveHandler(const HomieRange& range, const String& value) { bool aliveHandler(const HomieRange &range, const String &value)
if (range.isRange) return false; // only one controller is present {
if (value.equals("ON") || value.equals("On") || value.equals("1")) { if (range.isRange)
mode3Active=true; return false; // only one controller is present
} else { if (value.equals("ON") || value.equals("On") || value.equals("1"))
mode3Active=false; {
mode3Active = true;
}
else
{
mode3Active = false;
} }
return true; return true;
} }
void homieLoop(){ void homieLoop()
{
} }
void systemInit(){ void systemInit()
{
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
Homie_setFirmware("PlantControl", FIRMWARE_VERSION); Homie_setFirmware("PlantControl", FIRMWARE_VERSION);
@ -509,10 +585,10 @@ void systemInit(){
wateringDeepSleep.setDefaultValue(5); wateringDeepSleep.setDefaultValue(5);
ntpServer.setDefaultValue("pool.ntp.org"); ntpServer.setDefaultValue("pool.ntp.org");
/* waterLevelMax 1000 */ /* 100cm in mm */ /* waterLevelMax 1000 */ /* 100cm in mm */
waterLevelMin.setDefaultValue(50); /* 5cm in mm */ waterLevelMin.setDefaultValue(50); /* 5cm in mm */
waterLevelWarn.setDefaultValue(500); /* 50cm in mm */ waterLevelWarn.setDefaultValue(500); /* 50cm in mm */
waterLevelVol.setDefaultValue(5000); /* 5l in ml */ waterLevelVol.setDefaultValue(5000); /* 5l in ml */
Homie.setLoopFunction(homieLoop); Homie.setLoopFunction(homieLoop);
Homie.onEvent(onHomieEvent); Homie.onEvent(onHomieEvent);
@ -520,63 +596,70 @@ void systemInit(){
Homie.setup(); Homie.setup();
mConfigured = Homie.isConfigured(); mConfigured = Homie.isConfigured();
if (mConfigured) { if (mConfigured)
for(int i=0; i < MAX_PLANTS; i++) { {
for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].advertise(); mPlants[i].advertise();
} }
sensorTemp.advertise("control") sensorTemp.advertise("control")
.setName("Temperature") .setName("Temperature")
.setDatatype("number") .setDatatype("number")
.setUnit("°C"); .setUnit("°C");
sensorTemp.advertise("temp") sensorTemp.advertise("temp")
.setName("Temperature") .setName("Temperature")
.setDatatype("number") .setDatatype("number")
.setUnit("°C"); .setUnit("°C");
sensorLipo.advertise("percent") sensorLipo.advertise("percent")
.setName("Percent") .setName("Percent")
.setDatatype("number") .setDatatype("number")
.setUnit("%"); .setUnit("%");
sensorLipo.advertise("volt") sensorLipo.advertise("volt")
.setName("Volt") .setName("Volt")
.setDatatype("number") .setDatatype("number")
.setUnit("V"); .setUnit("V");
sensorSolar.advertise("percent") sensorSolar.advertise("percent")
.setName("Percent") .setName("Percent")
.setDatatype("number") .setDatatype("number")
.setUnit("%"); .setUnit("%");
sensorSolar.advertise("volt") sensorSolar.advertise("volt")
.setName("Volt") .setName("Volt")
.setDatatype("number") .setDatatype("number")
.setUnit("V"); .setUnit("V");
sensorWater.advertise("remaining").setDatatype("number").setUnit("%"); sensorWater.advertise("remaining").setDatatype("number").setUnit("%");
} }
stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler); stayAlive.advertise("alive").setName("Alive").setDatatype("number").settable(aliveHandler);
} }
bool mode1()
bool mode1(){ {
Serial.println("==== Mode 1 ===="); Serial.println("==== Mode 1 ====");
Serial << getCurrentTime() << " curtime" << endl; Serial << getCurrentTime() << " curtime" << endl;
readSensors(); readSensors();
//queue sensor values for //queue sensor values for
if (rtcDeepSleepTime == 0){ if (rtcDeepSleepTime == 0)
{
Serial.println("1 missing rtc value, going to mode2"); Serial.println("1 missing rtc value, going to mode2");
return true; return true;
} }
for(int i = 0; i < MAX_PLANTS; i++){ for (int i = 0; i < MAX_PLANTS; i++)
long trigger =getMoistureTrigger(i); {
if (trigger == 0) { long trigger = getMoistureTrigger(i);
if (trigger == 0)
{
Serial << "Missing rtc trigger " << i << endl; Serial << "Missing rtc trigger " << i << endl;
return true; return true;
} }
if(trigger == DEACTIVATED_PLANT){ if (trigger == DEACTIVATED_PLANT)
{
continue; continue;
} }
if(mPlants[i].getSensorValue() <= trigger){ if (mPlants[i].getSensorValue() <= trigger)
{
Serial << "plant dry starting mode 2" << i << endl; Serial << "plant dry starting mode 2" << i << endl;
return true; return true;
} }
@ -585,26 +668,32 @@ bool mode1(){
//check how long it was already in mode1 if to long goto mode2 //check how long it was already in mode1 if to long goto mode2
long cTime = getCurrentTime(); long cTime = getCurrentTime();
if(cTime < 100000){ if (cTime < 100000)
{
Serial.println("Starting mode 2 due to missing ntp"); Serial.println("Starting mode 2 due to missing ntp");
//missing ntp time boot to mode3 //missing ntp time boot to mode3
return true; return true;
} }
if(gotoMode2AfterThisTimestamp < cTime){ if (gotoMode2AfterThisTimestamp < cTime)
{
Serial.println("Starting mode 2 after specified mode1 time"); Serial.println("Starting mode 2 after specified mode1 time");
return true; return true;
} else { }
else
{
Serial << "Mode2 Timer " << gotoMode2AfterThisTimestamp << " curtime " << cTime << endl; Serial << "Mode2 Timer " << gotoMode2AfterThisTimestamp << " curtime " << cTime << endl;
} }
return false; return false;
} }
void mode2(){ void mode2()
{
Serial.println("==== Mode 2 ===="); Serial.println("==== Mode 2 ====");
systemInit(); systemInit();
/* Jump into Mode 3, if not configured */ /* Jump into Mode 3, if not configured */
if (!mConfigured) { if (!mConfigured)
{
Serial.println("==== Mode 3 ===="); Serial.println("==== Mode 3 ====");
mode3Active = true; mode3Active = true;
} }
@ -614,12 +703,15 @@ void mode2(){
* @brief Startup function * @brief Startup function
* Is called once, the controller is started * Is called once, the controller is started
*/ */
void setup() { void setup()
{
Serial.begin(115200); Serial.begin(115200);
Serial.setTimeout(1000); // Set timeout of 1 second Serial.setTimeout(1000); // Set timeout of 1 second
Serial << endl << endl; Serial << endl
<< endl;
/* Intialize Plant */ /* Intialize Plant */
for(int i=0; i < MAX_PLANTS; i++) { for (int i = 0; i < MAX_PLANTS; i++)
{
mPlants[i].init(); mPlants[i].init();
} }
@ -635,7 +727,8 @@ void setup() {
/* Disable Wifi and bluetooth */ /* Disable Wifi and bluetooth */
WiFi.mode(WIFI_OFF); WiFi.mode(WIFI_OFF);
if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS) { if (HomieInternals::MAX_CONFIG_SETTING_SIZE < MAX_CONFIG_SETTING_ITEMS)
{
//increase the config settings to 50 and the json to 3000 //increase the config settings to 50 and the json to 3000
Serial << "Limits.hpp" << endl; Serial << "Limits.hpp" << endl;
} }
@ -644,19 +737,23 @@ void setup() {
//Panik mode, the Lipo is empty, sleep a long long time: //Panik mode, the Lipo is empty, sleep a long long time:
if ((getBatteryVoltage() < MINIMUM_LIPO_VOLT) && if ((getBatteryVoltage() < MINIMUM_LIPO_VOLT) &&
(getBatteryVoltage() > NO_LIPO_VOLT)) { (getBatteryVoltage() > NO_LIPO_VOLT))
{
Serial << PANIK_MODE_DEEPSLEEP << " s lipo " << getBatteryVoltage() << "V" << endl; Serial << PANIK_MODE_DEEPSLEEP << " s lipo " << getBatteryVoltage() << "V" << endl;
esp_sleep_enable_timer_wakeup(PANIK_MODE_DEEPSLEEP_US); esp_sleep_enable_timer_wakeup(PANIK_MODE_DEEPSLEEP_US);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_FAST_MEM, ESP_PD_OPTION_OFF);
esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL,ESP_PD_OPTION_ON); esp_sleep_pd_config(ESP_PD_DOMAIN_XTAL, ESP_PD_OPTION_ON);
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF); esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_OFF);
esp_deep_sleep_start(); esp_deep_sleep_start();
} }
if(mode1()){ if (mode1())
{
mode2(); mode2();
} else { }
else
{
Serial.println("nop"); Serial.println("nop");
espDeepSleepFor(rtcDeepSleepTime); espDeepSleepFor(rtcDeepSleepTime);
} }
@ -667,26 +764,33 @@ void setup() {
* Executs the Homie base functionallity or triggers sleeping, if requested. * Executs the Homie base functionallity or triggers sleeping, if requested.
*/ */
long nextBlink = 0; long nextBlink = 0;
void loop() { void loop()
if (!mDeepsleep || mode3Active) { {
if (!mDeepsleep || mode3Active)
{
Homie.loop(); Homie.loop();
} else { }
else
{
Serial << "Bye" << endl; Serial << "Bye" << endl;
Serial.flush(); Serial.flush();
esp_deep_sleep_start(); esp_deep_sleep_start();
} }
if(millis() > 30000 && !mode3Active){ if (millis() > 30000 && !mode3Active)
Serial << (millis()/ 1000) << "not terminated watchdog putting to sleep" << endl; {
Serial << (millis() / 1000) << "not terminated watchdog putting to sleep" << endl;
Serial.flush(); Serial.flush();
espDeepSleepFor(rtcDeepSleepTime); espDeepSleepFor(rtcDeepSleepTime);
} }
/* Toggel Senor LED to visualize mode 3 */ /* Toggel Senor LED to visualize mode 3 */
if(mode3Active){ if (mode3Active)
if (nextBlink < millis()) { {
if (nextBlink < millis())
{
nextBlink = millis() + 500; nextBlink = millis() + 500;
digitalWrite(OUTPUT_SENSOR, ! digitalRead(OUTPUT_SENSOR)); digitalWrite(OUTPUT_SENSOR, !digitalRead(OUTPUT_SENSOR));
} }
} }
} }