From f38cb6b56488a4ba3ac373e2ea46c3a6df16edd6 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 2 Apr 2022 22:10:31 +0200 Subject: [PATCH 01/22] Testsetup for frequency on GPIO17 --- esp32/.vscode/extensions.json | 17 +++++++----- .../.vscode/extensions.json | 17 +++++++----- esp32test/Esp32DeepSleepTest/platformio.ini | 3 ++- esp32test/Esp32DeepSleepTest/src/main.cpp | 26 ++++++++++++------- 4 files changed, 38 insertions(+), 25 deletions(-) diff --git a/esp32/.vscode/extensions.json b/esp32/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/esp32/.vscode/extensions.json +++ b/esp32/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/esp32test/Esp32DeepSleepTest/.vscode/extensions.json b/esp32test/Esp32DeepSleepTest/.vscode/extensions.json index 0f0d740..080e70d 100644 --- a/esp32test/Esp32DeepSleepTest/.vscode/extensions.json +++ b/esp32test/Esp32DeepSleepTest/.vscode/extensions.json @@ -1,7 +1,10 @@ -{ - // See http://go.microsoft.com/fwlink/?LinkId=827846 - // for the documentation about the extensions.json format - "recommendations": [ - "platformio.platformio-ide" - ] -} +{ + // See http://go.microsoft.com/fwlink/?LinkId=827846 + // for the documentation about the extensions.json format + "recommendations": [ + "platformio.platformio-ide" + ], + "unwantedRecommendations": [ + "ms-vscode.cpptools-extension-pack" + ] +} diff --git a/esp32test/Esp32DeepSleepTest/platformio.ini b/esp32test/Esp32DeepSleepTest/platformio.ini index 46c5a12..c910f3b 100644 --- a/esp32test/Esp32DeepSleepTest/platformio.ini +++ b/esp32test/Esp32DeepSleepTest/platformio.ini @@ -14,4 +14,5 @@ board = esp32doit-devkit-v1 framework = arduino build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY lib_deps = OneWire - DallasTemperature \ No newline at end of file + DallasTemperature + \ No newline at end of file diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index a04df45..9d7d074 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -1,25 +1,33 @@ #include #include "driver/pcnt.h" -#define OUTPUT_SENSOR 14 /**< GPIO 16 - Enable Sensors */ -#define SENSOR_PLANT5 39 /**< SENSOR vn */ -#define SENSOR_PLANT6 36 /**< SENSOR VP */ +#define PWM_FREQ 50000 +#define PWM_BITS 8 + +#define OUTPUT_SENSOR 14 +#define SENSOR_PLANT 17 int16_t pulses = 0; int16_t pulses2 = 0; +int plantId = 0; + void setup() { Serial.begin(115200); pinMode(OUTPUT_SENSOR, OUTPUT); - pinMode(SENSOR_PLANT5, INPUT); + + ledcSetup(plantId, PWM_FREQ, PWM_BITS); + ledcAttachPin(SENSOR_PLANT, plantId); + ledcWrite(plantId, plantId); + pinMode(SENSOR_PLANT, INPUT); pcnt_config_t pcnt_config = { }; // Instancia PCNT config - - pcnt_config.pulse_gpio_num = SENSOR_PLANT5; // Configura GPIO para entrada dos pulsos + pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + plantId); + pcnt_config.pulse_gpio_num = SENSOR_PLANT; // Configura GPIO para entrada dos pulsos pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem - pcnt_config.unit = PCNT_UNIT_0; // Unidade de contagem PCNT - 0 + pcnt_config.unit = unit; // Unidade de contagem PCNT - 0 pcnt_config.channel = PCNT_CHANNEL_0; // Canal de contagem PCNT - 0 pcnt_config.counter_h_lim = INT16_MAX; // Limite maximo de contagem - 20000 pcnt_config.pos_mode = PCNT_COUNT_INC; // Incrementa contagem na subida do pulso @@ -34,11 +42,10 @@ void setup() { digitalWrite(OUTPUT_SENSOR, HIGH); - Serial.println("Start done"); + Serial.println("Nodemcu ESP32 Start done"); } void loop() { - pulses2 = pulseIn(SENSOR_PLANT5,HIGH); pcnt_counter_resume(PCNT_UNIT_0); delay(500); @@ -47,6 +54,5 @@ void loop() { pcnt_get_counter_value(PCNT_UNIT_0, &pulses); pcnt_counter_clear(PCNT_UNIT_0); - Serial.println(pulses2*2); Serial.println(pulses*2); } \ No newline at end of file From f18e4a7586bc0ea491289c8f9c121dd0d05f4d1c Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 25 Jun 2022 17:20:11 +0200 Subject: [PATCH 02/22] Show process in output (steps) --- esp32/host/upload-settings.sh | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/esp32/host/upload-settings.sh b/esp32/host/upload-settings.sh index e2c3580..b1ee62c 100755 --- a/esp32/host/upload-settings.sh +++ b/esp32/host/upload-settings.sh @@ -12,6 +12,8 @@ mqttHost=$1 mqttPrefix=$2 homieId=$3 +maxSteps=6 + settingsFile=settings.json if [ ! -f $settingsFile ]; then echo "$settingsFile missing" @@ -20,20 +22,20 @@ if [ ! -f $settingsFile ]; then fi mosquitto_pub -h $mqttHost -t "${mqttPrefix}${homieId}/stay/alive/set" -m "1" -r -echo "Waiting ..." +echo "(1 / $maxSteps) Waiting ..." mosquitto_sub -h $mqttHost -t "${mqttPrefix}${homieId}/#" -R -C 1 set -e -echo "Waiting 30 seconds ..." +echo "(2 / $maxSteps) Waiting 30 seconds ..." sleep 30 mosquitto_pub -h $mqttHost -t "${mqttPrefix}${homieId}/\$implementation/config/set" -f $settingsFile -echo "Waiting for reboot ..." +echo "(3 / $maxSteps) Waiting for reboot ..." sleep 1 mosquitto_sub -h $mqttHost -t "${mqttPrefix}${homieId}/#" -R -C 1 -echo "Alive" +echo "(4 / $maxSteps) Alive" sleep 20 -echo "Create Backup ..." +echo "(5 / $maxSteps) Create Backup ..." mosquitto_pub -h $mqttHost -t "${mqttPrefix}${homieId}/config/backup/set" -m "true" -r sleep 5 -echo "Shutdown ..." +echo "(6 / $maxSteps) Shutdown ..." mosquitto_pub -h $mqttHost -t "${mqttPrefix}${homieId}/stay/alive/set" -m "0" -r exit 0 From 71b91502790d61e6257ad0eb61a6ab19b9f31999 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 25 Jun 2022 17:43:23 +0200 Subject: [PATCH 03/22] Restore frequency boundaries (7a54065a5fce8c5fd448afce79aca0221c51c6a8) --- esp32/include/ControllerConfiguration.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 3d0ac5e..bff7d2e 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -94,7 +94,7 @@ #define FLOWMETER_PULSES_PER_ML 2.2 #endif -#define MOIST_SENSOR_MAX_FRQ 60000 // 60kHz (500Hz margin) +#define MOIST_SENSOR_MAX_FRQ 10000 // 10kHz (as never more then 3000 was measured) #define MOIST_SENSOR_MIN_FRQ 500 // 0.5kHz (500Hz margin) #define ANALOG_SENSOR_MAX_MV 1300 //successive approximation of good range From 5d24a51bd9460a7d1501aa2c0a529bc17bb3d8c0 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 21 Aug 2022 11:52:15 +0200 Subject: [PATCH 04/22] Plant states defined in header --- esp32/include/HomieTypes.h | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/esp32/include/HomieTypes.h b/esp32/include/HomieTypes.h index d39cd3b..861a105 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -48,6 +48,36 @@ static const char *SENSOR_STRING[] = { //plant uses cooldown and duration and workhours, moisture is measured but ignored #define TIMER_ONLY -4 +/** + * @brief State of plants + * + */ +#define PLANTSTATE_NUM_DEACTIVATED 0xF000 +#define PLANTSTATE_NUM_NO_SENSOR 0xE000 +#define PLANTSTATE_NUM_WET 0x0F00 +#define PLANTSTATE_NUM_SUNNY_ALARM 0x0021 +#define PLANTSTATE_NUM_ACTIVE_ALARM 0x0011 +#define PLANTSTATE_NUM_ACTIVE_SUPESSED 0x001F +#define PLANTSTATE_NUM_ACTIVE 0x0010 +#define PLANTSTATE_NUM_SUNNY 0x0020 +#define PLANTSTATE_NUM_COOLDOWN_ALARM 0x0031 +#define PLANTSTATE_NUM_COOLDOWN 0x0030 +#define PLANTSTATE_NUM_AFTERWORK_ALARM 0x0041 +#define PLANTSTATE_NUM_AFTERWORK 0x0040 + +#define PLANTSTATE_STR_DEACTIVATED "deactivated" +#define PLANTSTATE_STR_NO_SENSOR "nosensor" +#define PLANTSTATE_STR_WET "wet" +#define PLANTSTATE_STR_SUNNY_ALARM "sunny+alarm" +#define PLANTSTATE_STR_ACTIVE_ALARM "activate+alarm" +#define PLANTSTATE_STR_ACTIVE_SUPESSED "activate+supressed" +#define PLANTSTATE_STR_ACTIVE "activate" +#define PLANTSTATE_STR_SUNNY "sunny" +#define PLANTSTATE_STR_COOLDOWN_ALARM "cooldown+alarm" +#define PLANTSTATE_STR_COOLDOWN "cooldown" +#define PLANTSTATE_STR_AFTERWORK_ALARM "after-work+alarm" +#define PLANTSTATE_STR_AFTERWORK "after-work" + typedef struct PlantSettings_t { HomieSetting *pSensorDry; From c4441178536fd2a1710c883c7964110bdb4acb53 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 21 Aug 2022 11:59:50 +0200 Subject: [PATCH 05/22] code refactoring: use the defined states of header file --- esp32/include/HomieTypes.h | 6 +++--- esp32/include/PlantCtrl.h | 2 +- esp32/src/PlantCtrl.cpp | 4 ++-- esp32/src/main.cpp | 24 ++++++++++++------------ 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/esp32/include/HomieTypes.h b/esp32/include/HomieTypes.h index 861a105..95efe6a 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -69,9 +69,9 @@ static const char *SENSOR_STRING[] = { #define PLANTSTATE_STR_NO_SENSOR "nosensor" #define PLANTSTATE_STR_WET "wet" #define PLANTSTATE_STR_SUNNY_ALARM "sunny+alarm" -#define PLANTSTATE_STR_ACTIVE_ALARM "activate+alarm" -#define PLANTSTATE_STR_ACTIVE_SUPESSED "activate+supressed" -#define PLANTSTATE_STR_ACTIVE "activate" +#define PLANTSTATE_STR_ACTIVE_ALARM "active+alarm" +#define PLANTSTATE_STR_ACTIVE_SUPESSED "active+supressed" +#define PLANTSTATE_STR_ACTIVE "active" #define PLANTSTATE_STR_SUNNY "sunny" #define PLANTSTATE_STR_COOLDOWN_ALARM "cooldown+alarm" #define PLANTSTATE_STR_COOLDOWN "cooldown" diff --git a/esp32/include/PlantCtrl.h b/esp32/include/PlantCtrl.h index 332b1a7..dc4b2e8 100644 --- a/esp32/include/PlantCtrl.h +++ b/esp32/include/PlantCtrl.h @@ -197,7 +197,7 @@ public: return this->mSetting->pPumpOnlyWhenLowLight->get(); } - void publishState(String state); + void publishState(int stateNumber, String stateString); bool switchHandler(const HomieRange &range, const String &value); diff --git a/esp32/src/PlantCtrl.cpp b/esp32/src/PlantCtrl.cpp index 246d880..ca68bfa 100644 --- a/esp32/src/PlantCtrl.cpp +++ b/esp32/src/PlantCtrl.cpp @@ -209,11 +209,11 @@ void Plant::deactivatePump(void) } } -void Plant::publishState(String state) +void Plant::publishState(int stateNumber, String stateString) { if (this->mConnected) { - this->mPlant->setProperty("state").send(state); + this->mPlant->setProperty("state").send(stateString); } } diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 369cbc5..0df2994 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -429,7 +429,7 @@ int determineNextPump(bool isLowLight) Plant plant = mPlants[i]; if (!plant.isPumpTriggerActive()) { - plant.publishState("deactivated"); + plant.publishState(PLANTSTATE_NUM_DEACTIVATED, PLANTSTATE_STR_DEACTIVATED); log(LOG_LEVEL_DEBUG, String(String(i) + " Skip deactivated pump"), LOG_DEBUG_CODE); continue; } @@ -437,11 +437,11 @@ int determineNextPump(bool isLowLight) { if (wateralarm) { - plant.publishState("cooldown+alarm"); + plant.publishState(PLANTSTATE_NUM_COOLDOWN_ALARM, PLANTSTATE_STR_COOLDOWN_ALARM); } else { - plant.publishState("cooldown"); + plant.publishState(PLANTSTATE_NUM_COOLDOWN, PLANTSTATE_STR_COOLDOWN); } log(LOG_LEVEL_DEBUG, String(String(i) + " Skipping due to cooldown " + String(rtcLastWateringPlant[i] + plant.getCooldownInSeconds())), LOG_DEBUG_CODE); continue; @@ -450,11 +450,11 @@ int determineNextPump(bool isLowLight) { if (wateralarm) { - plant.publishState("sunny+alarm"); + plant.publishState(PLANTSTATE_NUM_SUNNY_ALARM, PLANTSTATE_STR_SUNNY_ALARM); } else { - plant.publishState("sunny"); + plant.publishState(PLANTSTATE_NUM_SUNNY, PLANTSTATE_STR_SUNNY); } log(LOG_LEVEL_DEBUG, String(String(i) + " No pump required: due to light"), LOG_DEBUG_CODE); @@ -464,7 +464,7 @@ int determineNextPump(bool isLowLight) { if (equalish(plant.getCurrentMoistureRaw(), MISSING_SENSOR)) { - plant.publishState("nosensor"); + plant.publishState(PLANTSTATE_NUM_NO_SENSOR, PLANTSTATE_STR_NO_SENSOR); log(LOG_LEVEL_ERROR, String(String(i) + " No pump possible: missing sensor"), LOG_MISSING_PUMP); continue; } @@ -483,17 +483,17 @@ int determineNextPump(bool isLowLight) { if (wateralarm) { - plant.publishState("active+alarm"); + plant.publishState(PLANTSTATE_NUM_ACTIVE_ALARM, PLANTSTATE_STR_ACTIVE_ALARM); } else { if (mDownloadMode) { - plant.publishState("active+supressed"); + plant.publishState(PLANTSTATE_NUM_ACTIVE_SUPESSED, PLANTSTATE_STR_ACTIVE_SUPESSED); } else { - plant.publishState("active"); + plant.publishState(PLANTSTATE_NUM_ACTIVE, PLANTSTATE_STR_ACTIVE); } } @@ -510,11 +510,11 @@ int determineNextPump(bool isLowLight) { if (wateralarm) { - plant.publishState("after-work+alarm"); + plant.publishState(PLANTSTATE_NUM_AFTERWORK_ALARM, PLANTSTATE_STR_AFTERWORK_ALARM); } else { - plant.publishState("after-work"); + plant.publishState(PLANTSTATE_NUM_AFTERWORK, PLANTSTATE_STR_AFTERWORK); } log(LOG_LEVEL_DEBUG, String(String(i) + " ignored due to time boundary: " + String(plant.getHoursStart()) + " to " + String(plant.getHoursEnd()) + " ( current " + String(getCurrentHour()) + " )"), LOG_DEBUG_CODE); } @@ -522,7 +522,7 @@ int determineNextPump(bool isLowLight) } else { - plant.publishState("wet"); + plant.publishState(PLANTSTATE_NUM_WET, PLANTSTATE_STR_WET); // plant was detected as wet, remove consecutive count consecutiveWateringPlant[i] = 0; } From 18b927bd59bd3b08336bd45222142e2e496ce077 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 21 Aug 2022 12:05:43 +0200 Subject: [PATCH 06/22] Publish state as number and string --- esp32/src/PlantCtrl.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/esp32/src/PlantCtrl.cpp b/esp32/src/PlantCtrl.cpp index ca68bfa..0030fb0 100644 --- a/esp32/src/PlantCtrl.cpp +++ b/esp32/src/PlantCtrl.cpp @@ -211,9 +211,14 @@ void Plant::deactivatePump(void) void Plant::publishState(int stateNumber, String stateString) { + String buffer; + StaticJsonDocument<200> doc; if (this->mConnected) { - this->mPlant->setProperty("state").send(stateString); + doc["number"] = stateNumber; + doc["message"] = stateString; + serializeJson(doc, buffer); + this->mPlant->setProperty("state").send(buffer.c_str()); } } From 79ffd8acfff6d9234b5f3149735e569a8b5e4189 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 21 Aug 2022 12:07:43 +0200 Subject: [PATCH 07/22] Version increased --- esp32/include/ControllerConfiguration.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index bff7d2e..acf4ddc 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -86,7 +86,7 @@ #define CONFIG_COMPILER_CXX_EXCEPTIONS -#define FIRMWARE_VERSION "2.4 HW0.10b" +#define FIRMWARE_VERSION "2.5 HW0.10b" #define TIMED_LIGHT_PIN CUSTOM1_PIN5 #define FLOWMETER_PIN CUSTOM1_PIN1 From 791d6f04b0694e5da342b9237bad57190e0ed921 Mon Sep 17 00:00:00 2001 From: Ollo Date: Sun, 21 Aug 2022 12:12:07 +0200 Subject: [PATCH 08/22] Tweaked number --- esp32/include/HomieTypes.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esp32/include/HomieTypes.h b/esp32/include/HomieTypes.h index 95efe6a..81d505c 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -54,11 +54,11 @@ static const char *SENSOR_STRING[] = { */ #define PLANTSTATE_NUM_DEACTIVATED 0xF000 #define PLANTSTATE_NUM_NO_SENSOR 0xE000 -#define PLANTSTATE_NUM_WET 0x0F00 +#define PLANTSTATE_NUM_WET 0x0100 #define PLANTSTATE_NUM_SUNNY_ALARM 0x0021 -#define PLANTSTATE_NUM_ACTIVE_ALARM 0x0011 +#define PLANTSTATE_NUM_ACTIVE_ALARM 0x0201 #define PLANTSTATE_NUM_ACTIVE_SUPESSED 0x001F -#define PLANTSTATE_NUM_ACTIVE 0x0010 +#define PLANTSTATE_NUM_ACTIVE 0x0200 #define PLANTSTATE_NUM_SUNNY 0x0020 #define PLANTSTATE_NUM_COOLDOWN_ALARM 0x0031 #define PLANTSTATE_NUM_COOLDOWN 0x0030 From 29e3aa08cb0792dbccc9872a08b7e938fa63b672 Mon Sep 17 00:00:00 2001 From: Ollo Date: Mon, 22 Aug 2022 18:08:19 +0200 Subject: [PATCH 09/22] Fixed unit description for cooldown --- esp32/include/HomieConfiguration.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/esp32/include/HomieConfiguration.h b/esp32/include/HomieConfiguration.h index 7bceab7..f133962 100644 --- a/esp32/include/HomieConfiguration.h +++ b/esp32/include/HomieConfiguration.h @@ -107,7 +107,7 @@ HomieSetting ntpServer("ntpServer", "NTP server (pool.ntp.org as d HomieSetting mPumpAllowedHourRangeStart##plant = HomieSetting("hourstart" strplant, "Plant" strplant " - Range pump allowed hour start (0-23)"); \ HomieSetting mPumpAllowedHourRangeEnd##plant = HomieSetting("hourend" strplant, "Plant" strplant " - Range pump allowed hour end (0-23)"); \ HomieSetting mPumpOnlyWhenLowLight##plant = HomieSetting("lowLight" strplant, "Plant" strplant " - Enable the Pump only, when there is no sunlight"); \ - HomieSetting mPumpCooldownInSeconds##plant = HomieSetting("delay" strplant, "Plant" strplant " - How long to wait until the pump is activated again (minutes)"); \ + HomieSetting mPumpCooldownInSeconds##plant = HomieSetting("delay" strplant, "Plant" strplant " - How long to wait until the pump is activated again (sec)"); \ HomieSetting pPumpDuration##plant = HomieSetting("pumpDuration" strplant, "Plant" strplant " - time seconds to water when pump is active"); \ HomieSetting pPumpMl##plant = HomieSetting("pumpAmount" strplant, "Plant" strplant " - ml (if using flowmeter) to water when pump is active"); \ HomieSetting pPowerLevel##plant = HomieSetting("powerLevel" strplant, "Plant" strplant " - pwm duty cycle in percent"); \ From e820301490122823c39eede773d76e67d2caac22 Mon Sep 17 00:00:00 2001 From: Ollo Date: Tue, 23 Aug 2022 20:26:11 +0200 Subject: [PATCH 10/22] Tweak state numbers --- esp32/include/HomieTypes.h | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/esp32/include/HomieTypes.h b/esp32/include/HomieTypes.h index 81d505c..14fd2a1 100644 --- a/esp32/include/HomieTypes.h +++ b/esp32/include/HomieTypes.h @@ -52,18 +52,18 @@ static const char *SENSOR_STRING[] = { * @brief State of plants * */ -#define PLANTSTATE_NUM_DEACTIVATED 0xF000 -#define PLANTSTATE_NUM_NO_SENSOR 0xE000 -#define PLANTSTATE_NUM_WET 0x0100 -#define PLANTSTATE_NUM_SUNNY_ALARM 0x0021 -#define PLANTSTATE_NUM_ACTIVE_ALARM 0x0201 -#define PLANTSTATE_NUM_ACTIVE_SUPESSED 0x001F -#define PLANTSTATE_NUM_ACTIVE 0x0200 -#define PLANTSTATE_NUM_SUNNY 0x0020 -#define PLANTSTATE_NUM_COOLDOWN_ALARM 0x0031 -#define PLANTSTATE_NUM_COOLDOWN 0x0030 -#define PLANTSTATE_NUM_AFTERWORK_ALARM 0x0041 -#define PLANTSTATE_NUM_AFTERWORK 0x0040 +#define PLANTSTATE_NUM_DEACTIVATED -1 +#define PLANTSTATE_NUM_NO_SENSOR -2 +#define PLANTSTATE_NUM_WET 0x00 +#define PLANTSTATE_NUM_SUNNY_ALARM 0x11 +#define PLANTSTATE_NUM_ACTIVE_ALARM 0x41 +#define PLANTSTATE_NUM_ACTIVE_SUPESSED -3 +#define PLANTSTATE_NUM_ACTIVE 0x40 +#define PLANTSTATE_NUM_SUNNY 0x10 +#define PLANTSTATE_NUM_COOLDOWN_ALARM 0x21 +#define PLANTSTATE_NUM_COOLDOWN 0x20 +#define PLANTSTATE_NUM_AFTERWORK_ALARM 0x31 +#define PLANTSTATE_NUM_AFTERWORK 0x30 #define PLANTSTATE_STR_DEACTIVATED "deactivated" #define PLANTSTATE_STR_NO_SENSOR "nosensor" From 195d3d3c1bc394f398244d34b0c1cdb32fd4160e Mon Sep 17 00:00:00 2001 From: Ollo Date: Thu, 25 Aug 2022 18:40:41 +0200 Subject: [PATCH 11/22] Ignore voltages above 100V --- esp32/include/ControllerConfiguration.h | 1 + esp32/include/LogDefines.h | 3 ++- esp32/src/main.cpp | 6 +++++- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index acf4ddc..268a237 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -110,6 +110,7 @@ #define MAX_PLANTS 7 #define SOLAR_CHARGE_MIN_VOLTAGE 7 /**< Sun is rising (morning detected) */ #define SOLAR_CHARGE_MAX_VOLTAGE 9 /**< Sun is shining (noon) */ +#define SOLAR_MAX_VOLTAGE_POSSIBLE 100 /**< higher values are treated as not connected sensor */ #define VOLT_MAX_BATT 4.2f #define MAX_CONFIG_SETTING_ITEMS 100 /**< Parameter, that can be configured in Homie */ diff --git a/esp32/include/LogDefines.h b/esp32/include/LogDefines.h index dc6ff3c..f007df2 100644 --- a/esp32/include/LogDefines.h +++ b/esp32/include/LogDefines.h @@ -37,4 +37,5 @@ #define LOG_SLEEP_DAY 101 #define LOG_SLEEP_CYCLE 102 #define LOG_MISSING_PUMP -4 -#define LOG_BOOT_ERROR_DETECTION 10000 \ No newline at end of file +#define LOG_BOOT_ERROR_DETECTION 10000 +#define LOG_SOLAR_CHARGER_MISSING 300 \ No newline at end of file diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 0df2994..696baab 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -1063,7 +1063,11 @@ void plantcontrol() sensorLipo.setProperty("ICA").send(String(battery.getICA())); sensorLipo.setProperty("DCA").send(String(battery.getDCA())); sensorLipo.setProperty("CCA").send(String(battery.getCCA())); - sensorSolar.setProperty("volt").send(String(mSolarVoltage)); + if (mSolarVoltage < SOLAR_MAX_VOLTAGE_POSSIBLE) { + sensorSolar.setProperty("volt").send(String(mSolarVoltage)); + } else { + log(LOG_LEVEL_INFO, String("Ignore unrealistc sun voltage" + String(mSolarVoltage) +"V"), LOG_SOLAR_CHARGER_MISSING); + } sensorTemp.setProperty(TEMPERATUR_SENSOR_CHIP).send(String(chipTemp)); } else From 1fad63a84dd765bca712e7c4517d837070238860 Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 10 Mar 2023 22:50:16 +0100 Subject: [PATCH 12/22] Start I2C sensor after power was activated --- esp32/include/ControllerConfiguration.h | 6 +++--- esp32/src/main.cpp | 12 +++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 1f74fef..fcaf5b6 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -75,8 +75,6 @@ #define CUSTOM1_PIN5 GPIO_NUM_2 /** mosfet controlled */ #define CUSTOM1_PIN7 GPIO_NUM_12 /** mosfet controlled */ -#define I2C1_SDA GPIO_NUM_34 /**< GPIO 34 - I2C */ -#define I2C1_SCL GPIO_NUM_35 /**< GPIO 35 - I2C */ /* @} */ /** \addtogroup Configuration @@ -116,7 +114,9 @@ #define MAX_CONFIG_SETTING_ITEMS 100 /**< Parameter, that can be configured in Homie */ #define MAX_JSON_CONFIG_FILE_SIZE_CUSTOM 2500 -#define TEMPERATUR_TIMEOUT 3000 /**< 3 Seconds timeout for the temperatur sensors */ +#define TEMPERATUR_TIMEOUT 3000 /**< 3 Seconds timeout for the temperatures sensors */ +#define WATERSENSOR_TIMEOUT 2000 /**< 2 Seconds timeout for the water distance sensor */ +#define WATERSENSOR_CYCLE 5 /**< 5 sensor measurement are performed */ #define DS18B20_RESOLUTION 9 /**< 9bit temperature resolution -> 0.5°C steps */ #define UTC_OFFSET_DE 3600 /* UTC offset in seconds for Germany */ diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index b16b317..f940a9e 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -329,11 +329,13 @@ void readPowerSwitchedSensors() } } - waterRawSensor.clear(); + Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); tankSensor.setTimeout(500); + tankSensor.setBus(&Wire); long start = millis(); bool distanceReady = false; - while (start + 500 > millis()) + delay(50); + while ((start + WATERSENSOR_TIMEOUT) > millis()) { if (tankSensor.init()) { @@ -347,12 +349,13 @@ void readPowerSwitchedSensors() } if (distanceReady) { + waterRawSensor.clear(); tankSensor.setSignalRateLimit(0.1); // increase laser pulse periods (defaults are 14 and 10 PCLKs) tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18); tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); tankSensor.setMeasurementTimingBudget(200000); - for (int readCnt = 0; readCnt < 5; readCnt++) + for (int readCnt = 0; readCnt < WATERSENSOR_CYCLE; readCnt++) { if (!tankSensor.timeoutOccurred()) { @@ -362,7 +365,7 @@ void readPowerSwitchedSensors() waterRawSensor.add(distance); } } - delay(10); + delay(50); } Serial << "Distance sensor " << waterRawSensor.getMedian() << " mm" << endl; } @@ -802,7 +805,6 @@ void safeSetup() { mPlants[i].initSensors(); } - Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL); readPowerSwitchedSensors(); Homie.setup(); From ab480041b29f3879fc8f04401d4ac0dd7a8f2dce Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 10 Mar 2023 23:37:50 +0100 Subject: [PATCH 13/22] Removed timeout --- esp32/include/ControllerConfiguration.h | 2 +- esp32/src/main.cpp | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index fcaf5b6..92af105 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -115,7 +115,7 @@ #define MAX_JSON_CONFIG_FILE_SIZE_CUSTOM 2500 #define TEMPERATUR_TIMEOUT 3000 /**< 3 Seconds timeout for the temperatures sensors */ -#define WATERSENSOR_TIMEOUT 2000 /**< 2 Seconds timeout for the water distance sensor */ +#define WATERSENSOR_TIMEOUT 3000 /**< 3 Seconds timeout for the water distance sensor */ #define WATERSENSOR_CYCLE 5 /**< 5 sensor measurement are performed */ #define DS18B20_RESOLUTION 9 /**< 9bit temperature resolution -> 0.5°C steps */ diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index f940a9e..890391d 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -329,12 +329,13 @@ void readPowerSwitchedSensors() } } - Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); - tankSensor.setTimeout(500); + Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL); + // Source: https://www.st.com/resource/en/datasheet/vl53l0x.pdf + tankSensor.setAddress(0x52); tankSensor.setBus(&Wire); + delay(50); long start = millis(); bool distanceReady = false; - delay(50); while ((start + WATERSENSOR_TIMEOUT) > millis()) { if (tankSensor.init()) @@ -344,7 +345,7 @@ void readPowerSwitchedSensors() } else { - delay(20); + delay(200); } } if (distanceReady) From a0c657a418cac449c31f6a2f0dae208eec9d2202 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 20:02:23 +0100 Subject: [PATCH 14/22] VL53L0X test started --- esp32test/Esp32DeepSleepTest/platformio.ini | 3 +- esp32test/Esp32DeepSleepTest/src/main.cpp | 79 ++++++++++----------- 2 files changed, 39 insertions(+), 43 deletions(-) diff --git a/esp32test/Esp32DeepSleepTest/platformio.ini b/esp32test/Esp32DeepSleepTest/platformio.ini index 777b595..1733f51 100644 --- a/esp32test/Esp32DeepSleepTest/platformio.ini +++ b/esp32test/Esp32DeepSleepTest/platformio.ini @@ -12,6 +12,5 @@ platform = espressif32 board = esp32doit-devkit-v1 framework = arduino -lib_deps = OneWire - DallasTemperature +lib_deps = pololu/VL53L0X@^1.3.1 build_flags = -DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY -DBOOTLOADER_LOG_LEVEL_VERBOSE -DLOG_DEFAULT_LEVEL_VERBOSE -DCORE_DEBUG_LEVEL=5 diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index 76edff7..bea0eec 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -1,61 +1,58 @@ #include -#include "driver/pcnt.h" +#include "driver/pcnt.h" +#include -#define PWM_FREQ 50000 -#define PWM_BITS 8 #define OUTPUT_SENSOR 14 #define SENSOR_PLANT 17 -int16_t pulses = 0; -int16_t pulses2 = 0; +VL53L0X tankSensor; -int plantId = 0; - -void setup() { -RTC_SLOW_ATTR uint8_t tick = 0; -RTC_SLOW_ATTR bool dir = true; void setup() { Serial.begin(115200); pinMode(OUTPUT_SENSOR, OUTPUT); - - ledcSetup(plantId, PWM_FREQ, PWM_BITS); - ledcAttachPin(SENSOR_PLANT, plantId); - ledcWrite(plantId, plantId); - pinMode(SENSOR_PLANT, INPUT); - - - pcnt_config_t pcnt_config = { }; // Instancia PCNT config - pcnt_unit_t unit = (pcnt_unit_t)(PCNT_UNIT_0 + plantId); - pcnt_config.pulse_gpio_num = SENSOR_PLANT; // Configura GPIO para entrada dos pulsos - pcnt_config.ctrl_gpio_num = PCNT_PIN_NOT_USED; // Configura GPIO para controle da contagem - pcnt_config.unit = unit; // Unidade de contagem PCNT - 0 - pcnt_config.channel = PCNT_CHANNEL_0; // Canal de contagem PCNT - 0 - pcnt_config.counter_h_lim = INT16_MAX; // Limite maximo de contagem - 20000 - pcnt_config.pos_mode = PCNT_COUNT_INC; // Incrementa contagem na subida do pulso - pcnt_config.neg_mode = PCNT_COUNT_DIS; // Incrementa contagem na descida do pulso - pcnt_config.lctrl_mode = PCNT_MODE_KEEP; // PCNT - modo lctrl desabilitado - pcnt_config.hctrl_mode = PCNT_MODE_KEEP; // PCNT - modo hctrl - se HIGH conta incrementando - pcnt_unit_config(&pcnt_config); // Configura o contador PCNT - - - pcnt_counter_pause(PCNT_UNIT_0); // Pausa o contador PCNT - pcnt_counter_clear(PCNT_UNIT_0); // Zera o contador PCNT - + tankSensor.setTimeout(500); digitalWrite(OUTPUT_SENSOR, HIGH); Serial.println("Nodemcu ESP32 Start done"); + + tankSensor.setTimeout(500); + long start = millis(); + bool distanceReady = false; + while (start + 500 > millis()) + { + if (tankSensor.init()) + { + distanceReady = true; + break; + } + else + { + delay(20); + } + } + + if (distanceReady) + { + Serial.println("Sensor init done"); + tankSensor.setSignalRateLimit(0.1); + // increase laser pulse periods (defaults are 14 and 10 PCLKs) + tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18); + tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); + tankSensor.setMeasurementTimingBudget(200000); + } else { + Serial.println("Sensor init failed"); + } } void loop() { - pcnt_counter_resume(PCNT_UNIT_0); - delay(500); + delay(500); - pcnt_counter_pause(PCNT_UNIT_0); - pcnt_get_counter_value(PCNT_UNIT_0, &pulses); - pcnt_counter_clear(PCNT_UNIT_0); - - Serial.println(pulses*2); + + if (!tankSensor.timeoutOccurred()) + { + uint16_t distance = tankSensor.readRangeSingleMillimeters(); + } } From 3a2ddb92e360ea09063da8b96e7824cd29bac027 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 20:06:35 +0100 Subject: [PATCH 15/22] Set onewire bus --- esp32test/Esp32DeepSleepTest/src/main.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index bea0eec..fa2de84 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -2,6 +2,9 @@ #include "driver/pcnt.h" #include +#define SENSOR_TANK_SDA GPIO_NUM_16 /**< GPIO 16 - echo feedback of water sensor */ +#define SENSOR_TANK_SCL GPIO_NUM_17 /**< GPIO 17 - trigger for water sensor */ + #define OUTPUT_SENSOR 14 #define SENSOR_PLANT 17 @@ -18,6 +21,10 @@ void setup() Serial.println("Nodemcu ESP32 Start done"); tankSensor.setTimeout(500); + Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); + tankSensor.setTimeout(500); + tankSensor.setBus(&Wire); + delay(20); long start = millis(); bool distanceReady = false; while (start + 500 > millis()) From 939a5fe21a6e05ae3810d43715e3735465d33b48 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 20:21:10 +0100 Subject: [PATCH 16/22] Print distance --- esp32test/Esp32DeepSleepTest/src/main.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index fa2de84..85f24e4 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -61,5 +61,9 @@ void loop() { if (!tankSensor.timeoutOccurred()) { uint16_t distance = tankSensor.readRangeSingleMillimeters(); + Serial.print("Distance"); + Serial.println(distance); + } else { + Serial.println("Timeout"); } } From 66f61a69db69fce897e21f56fbf27f27eab7ccdb Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 20:50:25 +0100 Subject: [PATCH 17/22] Reset sensor on bad values --- esp32test/Esp32DeepSleepTest/src/main.cpp | 39 ++++++++++++++++------- 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index 85f24e4..69588b9 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -11,16 +11,7 @@ VL53L0X tankSensor; -void setup() -{ - Serial.begin(115200); - pinMode(OUTPUT_SENSOR, OUTPUT); - tankSensor.setTimeout(500); - - digitalWrite(OUTPUT_SENSOR, HIGH); - Serial.println("Nodemcu ESP32 Start done"); - - tankSensor.setTimeout(500); +void initializeTanksensor() { Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); tankSensor.setTimeout(500); tankSensor.setBus(&Wire); @@ -48,11 +39,23 @@ void setup() tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18); tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); tankSensor.setMeasurementTimingBudget(200000); + tankSensor.startContinuous(); } else { Serial.println("Sensor init failed"); } } +void setup() +{ + Serial.begin(115200); + pinMode(OUTPUT_SENSOR, OUTPUT); + + digitalWrite(OUTPUT_SENSOR, HIGH); + Serial.println("Nodemcu ESP32 Start done"); + + initializeTanksensor(); +} + void loop() { delay(500); @@ -61,9 +64,21 @@ void loop() { if (!tankSensor.timeoutOccurred()) { uint16_t distance = tankSensor.readRangeSingleMillimeters(); - Serial.print("Distance"); - Serial.println(distance); + if (distance == 8191) { + Serial.println("Reset due to 8.191 meter"); + tankSensor.stopContinuous(); + Wire.end(); + delay(100); + initializeTanksensor(); + } else { + Serial.print("Distance"); + Serial.println(distance); + } } else { Serial.println("Timeout"); + tankSensor.stopContinuous(); + Wire.end(); + delay(100); + initializeTanksensor(); } } From 9eb16637dc24aab8b008ea1b5bd49b08eef41276 Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 21:01:24 +0100 Subject: [PATCH 18/22] Reset as distance is > 8 meter --- esp32test/Esp32DeepSleepTest/src/main.cpp | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index 69588b9..f7f600f 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -13,9 +13,9 @@ VL53L0X tankSensor; void initializeTanksensor() { Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); - tankSensor.setTimeout(500); tankSensor.setBus(&Wire); - delay(20); + delay(100); + tankSensor.setTimeout(500); long start = millis(); bool distanceReady = false; while (start + 500 > millis()) @@ -64,11 +64,10 @@ void loop() { if (!tankSensor.timeoutOccurred()) { uint16_t distance = tankSensor.readRangeSingleMillimeters(); - if (distance == 8191) { - Serial.println("Reset due to 8.191 meter"); - tankSensor.stopContinuous(); + if (distance > 8000) { + Serial.println("Reset due invalid distance: 8 meter"); Wire.end(); - delay(100); + delay(1000); initializeTanksensor(); } else { Serial.print("Distance"); From bd2e4d3da0271d48a859f6282922f44555dea26f Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 21:06:14 +0100 Subject: [PATCH 19/22] distance sensor is reset after powercycle --- esp32test/Esp32DeepSleepTest/src/main.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/esp32test/Esp32DeepSleepTest/src/main.cpp b/esp32test/Esp32DeepSleepTest/src/main.cpp index f7f600f..aced6b8 100644 --- a/esp32test/Esp32DeepSleepTest/src/main.cpp +++ b/esp32test/Esp32DeepSleepTest/src/main.cpp @@ -10,6 +10,7 @@ #define SENSOR_PLANT 17 VL53L0X tankSensor; +bool distanceReady = false; void initializeTanksensor() { Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */); @@ -17,7 +18,6 @@ void initializeTanksensor() { delay(100); tankSensor.setTimeout(500); long start = millis(); - bool distanceReady = false; while (start + 500 > millis()) { if (tankSensor.init()) @@ -31,7 +31,7 @@ void initializeTanksensor() { } } - if (distanceReady) + if ((distanceReady) && (!tankSensor.timeoutOccurred())) { Serial.println("Sensor init done"); tankSensor.setSignalRateLimit(0.1); @@ -61,7 +61,7 @@ void loop() { delay(500); - if (!tankSensor.timeoutOccurred()) + if ((distanceReady) && (!tankSensor.timeoutOccurred())) { uint16_t distance = tankSensor.readRangeSingleMillimeters(); if (distance > 8000) { From 90f2d821a784c476019e7408c3423dfdcada1caa Mon Sep 17 00:00:00 2001 From: Ollo Date: Wed, 22 Mar 2023 22:04:49 +0100 Subject: [PATCH 20/22] Swapped water sensor pins --- esp32/include/ControllerConfiguration.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/esp32/include/ControllerConfiguration.h b/esp32/include/ControllerConfiguration.h index 92af105..e2dd7aa 100644 --- a/esp32/include/ControllerConfiguration.h +++ b/esp32/include/ControllerConfiguration.h @@ -66,8 +66,8 @@ #define OUTPUT_ENABLE_PUMP GPIO_NUM_13 /**< GPIO 13 - Enable Pumps */ #define SENSOR_ONEWIRE GPIO_NUM_4 /**< GPIO 12 - Temperatur sensor, Battery and other cool onewire stuff */ -#define SENSOR_TANK_SDA GPIO_NUM_16 /**< GPIO 16 - echo feedback of water sensor */ -#define SENSOR_TANK_SCL GPIO_NUM_17 /**< GPIO 17 - trigger for water sensor */ +#define SENSOR_TANK_SDA GPIO_NUM_17 /**< GPIO 17 - water sensor SDA */ +#define SENSOR_TANK_SCL GPIO_NUM_16 /**< GPIO 16 - water sensor SCL */ #define BUTTON GPIO_NUM_0 /**< GPIO 0 - Fix button of NodeMCU */ #define CUSTOM1_PIN1 GPIO_NUM_34 /** direct gpio */ From 704684d4374d2deb7a441026c36715640f2d0533 Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 24 Mar 2023 18:06:21 +0100 Subject: [PATCH 21/22] Fixed water distance and publish percentage --- esp32/src/main.cpp | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 890391d..1482d48 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -1040,7 +1040,7 @@ void plantcontrol() readOneWireSensors(); - Serial << "W : " << waterRawSensor.getAverage() << " cm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << "%)" << endl; + Serial << "W : " << waterRawSensor.getAverage() << " mm (" << String(waterLevelMax.get() - waterRawSensor.getAverage()) << " mm left)" << endl; float batteryVoltage = battery.getVoltage(BATTSENSOR_INDEX_BATTERY); float chipTemp = battery.getTemperature(); @@ -1048,14 +1048,19 @@ void plantcontrol() if (aliveWasRead()) { - float remaining = waterLevelMax.get() - waterRawSensor.getAverage(); - if (!isnan(remaining)) + /* Publish water values, if available */ + if (waterRawSensor.getCount() > 0) { - sensorWater.setProperty("remaining").send(String(remaining)); - } - if (!isnan(waterRawSensor.getAverage())) - { - sensorWater.setProperty("distance").send(String(waterRawSensor.getAverage())); + float remaining = (waterLevelMax.get() - waterRawSensor.getAverage()); + if (!isnan(remaining)) + { + /* measuring the distance from top -> smaller value means more water: */ + sensorWater.setProperty("remaining").send(String(100.0 - (remaining/100))); + } + if (!isnan(waterRawSensor.getAverage())) + { + sensorWater.setProperty("distance").send(String(waterRawSensor.getAverage())); + } } sensorLipo.setProperty("percent").send(String(100 * batteryVoltage / VOLT_MAX_BATT)); sensorLipo.setProperty("volt").send(String(batteryVoltage)); From 5df69f20a8e6120ccd174e3d02835418d9e98dde Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 24 Mar 2023 18:40:57 +0100 Subject: [PATCH 22/22] Stop pumps due to water sensor --- esp32/src/main.cpp | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/esp32/src/main.cpp b/esp32/src/main.cpp index 1482d48..6a3a4b1 100644 --- a/esp32/src/main.cpp +++ b/esp32/src/main.cpp @@ -1095,7 +1095,12 @@ bool isLowLight = mSolarVoltage <= 9; #endif // TIMED_LIGHT_PIN bool isLiquid = waterTemp > 5; - bool hasWater = true; // FIXME remaining > waterLevelMin.get(); + bool hasWater = true; // By default activate the pump + if (waterRawSensor.getCount() > 0) + { + hasWater = ( (waterLevelMax.get() - waterRawSensor.getAverage()) > waterLevelMin.get() ); + } + // FIXME no water warning message pumpToRun = determineNextPump(isLowLight); // early aborts @@ -1108,6 +1113,8 @@ bool isLowLight = mSolarVoltage <= 9; { log(LOG_LEVEL_INFO, LOG_PUMP_AND_DOWNLOADMODE, LOG_PUMP_AND_DOWNLOADMODE_CODE); pumpToRun = -1; + } else { + /* Pump can be used :) */ } } else @@ -1116,8 +1123,9 @@ bool isLowLight = mSolarVoltage <= 9; pumpToRun = -1; } } - else{ - log(LOG_LEVEL_ERROR, LOG_VERY_COLD_WATER, LOG_VERY_COLD_WATER_CODE); + else + { + log(LOG_LEVEL_ERROR, LOG_VERY_COLD_WATER, LOG_VERY_COLD_WATER_CODE); pumpToRun = -1; } }