Start I2C sensor after power was activated

This commit is contained in:
Ollo 2023-03-10 22:50:16 +01:00
parent 712ef73a30
commit 1fad63a84d
2 changed files with 10 additions and 8 deletions

View File

@ -75,8 +75,6 @@
#define CUSTOM1_PIN5 GPIO_NUM_2 /** mosfet controlled */ #define CUSTOM1_PIN5 GPIO_NUM_2 /** mosfet controlled */
#define CUSTOM1_PIN7 GPIO_NUM_12 /** 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 /** \addtogroup Configuration
@ -116,7 +114,9 @@
#define MAX_CONFIG_SETTING_ITEMS 100 /**< Parameter, that can be configured in Homie */ #define MAX_CONFIG_SETTING_ITEMS 100 /**< Parameter, that can be configured in Homie */
#define MAX_JSON_CONFIG_FILE_SIZE_CUSTOM 2500 #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 DS18B20_RESOLUTION 9 /**< 9bit temperature resolution -> 0.5°C steps */
#define UTC_OFFSET_DE 3600 /* UTC offset in seconds for Germany */ #define UTC_OFFSET_DE 3600 /* UTC offset in seconds for Germany */

View File

@ -329,11 +329,13 @@ void readPowerSwitchedSensors()
} }
} }
waterRawSensor.clear(); Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL, 100000UL /* 100kHz */);
tankSensor.setTimeout(500); tankSensor.setTimeout(500);
tankSensor.setBus(&Wire);
long start = millis(); long start = millis();
bool distanceReady = false; bool distanceReady = false;
while (start + 500 > millis()) delay(50);
while ((start + WATERSENSOR_TIMEOUT) > millis())
{ {
if (tankSensor.init()) if (tankSensor.init())
{ {
@ -347,12 +349,13 @@ void readPowerSwitchedSensors()
} }
if (distanceReady) if (distanceReady)
{ {
waterRawSensor.clear();
tankSensor.setSignalRateLimit(0.1); tankSensor.setSignalRateLimit(0.1);
// increase laser pulse periods (defaults are 14 and 10 PCLKs) // increase laser pulse periods (defaults are 14 and 10 PCLKs)
tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18); tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); tankSensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
tankSensor.setMeasurementTimingBudget(200000); tankSensor.setMeasurementTimingBudget(200000);
for (int readCnt = 0; readCnt < 5; readCnt++) for (int readCnt = 0; readCnt < WATERSENSOR_CYCLE; readCnt++)
{ {
if (!tankSensor.timeoutOccurred()) if (!tankSensor.timeoutOccurred())
{ {
@ -362,7 +365,7 @@ void readPowerSwitchedSensors()
waterRawSensor.add(distance); waterRawSensor.add(distance);
} }
} }
delay(10); delay(50);
} }
Serial << "Distance sensor " << waterRawSensor.getMedian() << " mm" << endl; Serial << "Distance sensor " << waterRawSensor.getMedian() << " mm" << endl;
} }
@ -802,7 +805,6 @@ void safeSetup()
{ {
mPlants[i].initSensors(); mPlants[i].initSensors();
} }
Wire.begin(SENSOR_TANK_SDA, SENSOR_TANK_SCL);
readPowerSwitchedSensors(); readPowerSwitchedSensors();
Homie.setup(); Homie.setup();