From 1fad63a84dd765bca712e7c4517d837070238860 Mon Sep 17 00:00:00 2001 From: Ollo Date: Fri, 10 Mar 2023 22:50:16 +0100 Subject: [PATCH 1/2] 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 2/2] 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)