diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 2b5ee06..21e96d3 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -11,7 +11,7 @@ #ifndef CONTROLLER_CONFIG_H #define CONTROLLER_CONFIG_H -#define FIRMWARE_VERSION "0.9.5" +#define FIRMWARE_VERSION "1.0.1" #define ADC_TO_VOLT(adc) ((adc) * 3.3 ) / 4095) #define ADC_TO_VOLT_WITH_MULTI(adc, multi) (((adc) * 3.3 * (multi)) / 4095) @@ -22,34 +22,36 @@ #define SENSOR_LIPO 34 /**< GPIO 34 (ADC1) */ #define SENSOR_SOLAR 35 /**< GPIO 35 (ADC1) */ -#define SENSOR_PLANT1 32 /**< GPIO 32 (ADC1) */ -#define SENSOR_PLANT2 33 /**< GPIO 33 (ADC1) */ -#define SENSOR_PLANT3 25 /**< GPIO 25 (ADC2) */ -#define SENSOR_PLANT4 26 /**< GPIO 26 (ADC2) */ -#define SENSOR_PLANT5 27 /**< GPIO 27 (ADC2) */ -#define SENSOR_PLANT6 14 /**< GPIO 14 (ADC2) */ +#define SENSOR_PLANT0 32 /**< GPIO 32 (ADC1) */ +#define SENSOR_PLANT1 33 /**< GPIO 33 (ADC1) */ +#define SENSOR_PLANT2 25 /**< GPIO 25 (ADC2) */ +#define SENSOR_PLANT3 25 /**< GPIO 26 (ADC2) */ +#define SENSOR_PLANT4 27 /**< GPIO 27 (ADC2) */ +#define SENSOR_PLANT5 14 /**< GPIO 14 (ADC2) */ +#define SENSOR_PLANT6 12 /**< GPIO 12 (ADC2) */ -#define OUTPUT_PUMP1 5 /**< GPIO 5 */ -#define OUTPUT_PUMP2 18 /**< GPIO 18 */ +#define OUTPUT_PUMP0 23 /**< GPIO 23 */ +#define OUTPUT_PUMP1 22 /**< GPIO 22 */ +#define OUTPUT_PUMP2 21 /**< GPIO 21 */ #define OUTPUT_PUMP3 19 /**< GPIO 19 */ -#define OUTPUT_PUMP4 21 /**< GPIO 21 */ -#define OUTPUT_PUMP5 22 /**< GPIO 22 */ -#define OUTPUT_PUMP6 23 /**< GPIO 23 */ +#define OUTPUT_PUMP4 18 /**< GPIO 18 */ +#define OUTPUT_PUMP5 29 /**< GPIO 29 */ +#define OUTPUT_PUMP6 15 /**< GPIO 15 */ -#define OUTPUT_SENSOR 4 /**< GPIO 4 */ -#define INPUT_WATER_LOW 2 /**< GPIO 2 */ -#define INPUT_WATER_EMPTY 15 /**< GPIO 15 */ -#define INPUT_WATER_OVERFLOW 12 /**< GPIO 12 */ +#define OUTPUT_SENSOR 16 /**< GPIO 16 - Enable Sensors */ +#define OUTPUT_PUMP 13 /**< GPIO 13 - Enable Pumps */ -#define SENSOR_DS18B20 13 /**< GPIO 13 */ +#define SENSOR_DS18B20 2 /**< GPIO 2 */ #define BUTTON 0 /**< GPIO 0 */ -#define MIN_TIME_RUNNING 10UL /**< Amount of seconds the controller must stay awoken */ -#define MAX_PLANTS 3 +#define MIN_TIME_RUNNING 5UL /**< Amount of seconds the controller must stay awoken */ +#define MAX_PLANTS 7 #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_SOLAR_VOLT 4.0f /**< Minimum voltage of the sun, to detect daylight */ #define HC_SR04 /**< Ultrasonic distance sensor to measure water level */ +#define SENSOR_SR04_ECHO 17 /**< GPIO 17 - Echo */ +#define SENSOR_SR04_TRIG 23 /**< GPIO 23 - Trigger */ #endif diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 81a254c..4c24dee 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -12,6 +12,8 @@ #include "ControllerConfiguration.h" #include "DS18B20.h" #include +#include "esp_sleep.h" + const unsigned long TEMPREADCYCLE = 30000; /**< Check temperature all half minutes */ @@ -34,16 +36,12 @@ int solarSensor = -1; int solarSensorValues = 0; int mWaterAtEmptyLevel = 0; -#ifndef HC_SR04 -int mWaterLow = 0; -#else -int mWaterGone = -1; /**< Amount of centimeter, where no water is seen */ -#endif -int mOverflow = 0; +int mWaterGone = -1; /**< Amount of centimeter, where no water is seen */ int readCounter = 0; int mButtonClicks = 0; +RTC_DATA_ATTR int gBootCount = 0; #if (MAX_PLANTS >= 1) HomieNode plant1("plant1", "Plant 1", "Plant"); @@ -63,6 +61,9 @@ HomieNode plant5("plant5", "Plant 5", "Plant"); #if (MAX_PLANTS >= 6) HomieNode plant6("plant6", "Plant 6", "Plant"); #endif +#if (MAX_PLANTS >= 7) +HomieNode plant6("plant7", "Plant 7", "Plant"); +#endif HomieNode sensorLipo("lipo", "Battery Status", "Lipo"); HomieNode sensorSolar("solar", "Solar Status", "Solarpanel"); @@ -71,52 +72,56 @@ HomieNode sensorTemp("temperature", "Temperature", "temperature"); HomieSetting deepSleepTime("deepsleep", "time in milliseconds to sleep (0 deactivats it)"); HomieSetting deepSleepNightTime("nightsleep", "time in milliseconds to sleep (0 usese same setting: deepsleep at night, too)"); -HomieSetting wateringTime("watering", "time seconds the pump is running (60 is the default)"); -HomieSetting plantCnt("plants", "amout of plants to control (1 ... 6)"); +HomieSetting wateringDeepSleep("pumpdeepsleep", "time seconds to sleep, while a pump is running"); +HomieSetting plantCnt("plants", "amout of plants to control (1 ... 7)"); #ifdef HC_SR04 HomieSetting waterLevel("watermaxlevel", "Water maximum level in centimeter (50 cm default)"); #endif - - -#if (MAX_PLANTS >= 1) +HomieSetting plant0SensorTrigger("moist0", "Moist0 sensor value, when pump activates"); HomieSetting plant1SensorTrigger("moist1", "Moist1 sensor value, when pump activates"); -#endif -#if (MAX_PLANTS >= 2) HomieSetting plant2SensorTrigger("moist2", "Moist2 sensor value, when pump activates"); -#endif -#if (MAX_PLANTS >= 3) HomieSetting plant3SensorTrigger("moist3", "Moist3 sensor value, when pump activates"); -#endif -#if (MAX_PLANTS >= 4) HomieSetting plant4SensorTrigger("moist4", "Moist4 sensor value, when pump activates"); -#endif -#if (MAX_PLANTS >= 5) HomieSetting plant5SensorTrigger("moist5", "Moist5 sensor value, when pump activates"); -#endif -#if (MAX_PLANTS >= 6) HomieSetting plant6SensorTrigger("moist6", "Moist6 sensor value, when pump activates"); -#endif +HomieSetting wateringTime0("plant0MaxPumpTime", "time seconds Pump0 is running (60 is the default)"); +HomieSetting wateringTime1("plant1MaxPumpTime", "time seconds Pump1 is running (60 is the default)"); +HomieSetting wateringTime2("plant2MaxPumpTime", "time seconds Pump2 is running (60 is the default)"); +HomieSetting wateringTime3("plant3MaxPumpTime", "time seconds Pump3 is running (60 is the default)"); +HomieSetting wateringTime4("plant4MaxPumpTime", "time seconds Pump4 is running (60 is the default)"); +HomieSetting wateringTime5("plant5MaxPumpTime", "time seconds Pump5 is running (60 is the default)"); +HomieSetting wateringTime5("plant6MaxPumpTime", "time seconds Pump6 is running (60 is the default)"); +HomieSetting wateringIdleTime0("plant0MinPumpIdle", "time in seconds Pump0 will wait (60 is the default)"); +HomieSetting wateringIdleTime1("plant1MinPumpIdle", "time in seconds Pump1 will wait (60 is the default)"); +HomieSetting wateringIdleTime2("plant2MinPumpIdle", "time in seconds Pump2 will wait (60 is the default)"); +HomieSetting wateringIdleTime3("plant3MinPumpIdle", "time in seconds Pump3 will wait (60 is the default)"); +HomieSetting wateringIdleTime4("plant4MinPumpIdle", "time in seconds Pump4 will wait (60 is the default)"); +HomieSetting wateringIdleTime5("plant5MinPumpIdle", "time in seconds Pump5 will wait (60 is the default)"); +HomieSetting wateringIdleTime6("plant6MinPumpIdle", "time in seconds Pump6 will wait (60 is the default)"); Ds18B20 dallas(SENSOR_DS18B20); Plant mPlants[MAX_PLANTS] = { #if (MAX_PLANTS >= 1) - Plant(SENSOR_PLANT1, OUTPUT_PUMP1), + Plant(SENSOR_PLANT0, OUTPUT_PUMP0), #endif #if (MAX_PLANTS >= 2) - Plant(SENSOR_PLANT2, OUTPUT_PUMP2), + Plant(SENSOR_PLANT1, OUTPUT_PUMP1), #endif #if (MAX_PLANTS >= 3) - Plant(SENSOR_PLANT3, OUTPUT_PUMP3), + Plant(SENSOR_PLANT2, OUTPUT_PUMP2), #endif #if (MAX_PLANTS >= 4) - Plant(SENSOR_PLANT4, OUTPUT_PUMP4), + Plant(SENSOR_PLANT3, OUTPUT_PUMP3), #endif #if (MAX_PLANTS >= 5) - Plant(SENSOR_PLANT5, OUTPUT_PUMP5), + Plant(SENSOR_PLANT4, OUTPUT_PUMP4), #endif #if (MAX_PLANTS >= 6) + Plant(SENSOR_PLANT5, OUTPUT_PUMP5), +#endif +#if (MAX_PLANTS >= 7) Plant(SENSOR_PLANT6, OUTPUT_PUMP6) #endif }; @@ -161,6 +166,9 @@ void loopHandler() { plant5.setProperty("switch").send(String("OFF")); plant6.setProperty("switch").send(String("OFF")); #endif +#if (MAX_PLANTS >= 7) + plant7.setProperty("switch").send(String("OFF")); +#endif for(int i=0; i < plantCnt.get(); i++) { mPlants[i].calculateSensorValue(AMOUNT_SENOR_QUERYS); @@ -195,26 +203,11 @@ void loopHandler() { #endif } -#ifndef HC_SR04 - if (SOLAR_VOLT(solarSensor) > SOLAR4SENSORS) { - if (mWaterLow && mWaterAtEmptyLevel) { - sensorWater.setProperty("remaining").send("50"); - } else if (!mWaterLow && mWaterAtEmptyLevel) { - sensorWater.setProperty("remaining").send("10"); - } else if (!mWaterLow && !mWaterAtEmptyLevel) { - sensorWater.setProperty("remaining").send("0"); - } else if (!mWaterLow && !mWaterAtEmptyLevel) { - sensorWater.setProperty("remaining").send("-1"); - } - } else { - Serial << "Sun not strong enough for sensors (" << String(SOLAR_VOLT(solarSensor)) << "V )" << endl; - } -#else mWaterAtEmptyLevel = (mWaterGone <= waterLevel.get()); int waterLevelPercent = (100 * mWaterGone) / waterLevel.get(); sensorWater.setProperty("remaining").send(String(waterLevelPercent)); Serial << "Water : " << mWaterGone << " cm (" << waterLevelPercent << "%)" << endl; -#endif + mPumpIsRunning=false; /* Check if a plant needs water */ if (mPlants[i].isPumpRequired(boundary4MoistSensor) && @@ -385,11 +378,6 @@ void readSensors() { /* activate all sensors */ pinMode(OUTPUT_SENSOR, OUTPUT); digitalWrite(OUTPUT_SENSOR, HIGH); - /* Use Pump 4 to activate and deactivate the Sensors */ -#if (MAX_PLANTS < 4) - pinMode(OUTPUT_PUMP4, OUTPUT); - digitalWrite(OUTPUT_PUMP4, HIGH); -#endif delay(100); /* wait before reading something */ @@ -399,11 +387,7 @@ void readSensors() { } } -#ifndef HC_SR04 - mWaterAtEmptyLevel = digitalRead(INPUT_WATER_EMPTY); - mWaterLow = digitalRead(INPUT_WATER_LOW); - mOverflow = digitalRead(INPUT_WATER_OVERFLOW); -#else +#ifdef HC_SR04 /* Use the Ultrasonic sensor to measure waterLevel */ /* deactivate all sensors and measure the pulse */ @@ -419,9 +403,6 @@ void readSensors() { #endif /* deactivate the sensors */ digitalWrite(OUTPUT_SENSOR, LOW); -#if (MAX_PLANTS < 4) - digitalWrite(OUTPUT_PUMP4, LOW); -#endif } /** @@ -504,6 +485,10 @@ void setup() { plant6.advertise("moist").setName("Percent") .setDatatype("number") .setUnit("%"); + plant7.advertise("moist").setName("Percent") + .setDatatype("number") + .setUnit("%"); + #endif sensorTemp.advertise("control") .setName("Temperature") @@ -569,6 +554,12 @@ void setup() { mDeepSleep = true; } + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_PERIPH, ESP_PD_OPTION_OFF); + esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON); + 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); + + /* Read the temperature sensors once, as first time 85 degree is returned */ Serial << "DS18B20 | sensors: " << String(dallas.readDevices()) << endl; delay(200);