diff --git a/include/waterlevel_vl6180x.h b/include/_waterlevel_vl6180x.h similarity index 100% rename from include/waterlevel_vl6180x.h rename to include/_waterlevel_vl6180x.h diff --git a/include/waterlevel.h b/include/waterlevel.h index 9438d9d..99b2ef0 100644 --- a/include/waterlevel.h +++ b/include/waterlevel.h @@ -3,7 +3,6 @@ #include #include //pololu/VL53L0X@^1.3.1 -#include //https://github.com/pololu/vl6180x-arduino @@ -21,7 +20,7 @@ // +++++++++++++++ VL53L0X +++++++++++++++ VL53L0X sensorA; -#define PIN_VL53L0X_XSHUT 19 +#define PIN_VL53L0X_XSHUT_A 19 // Uncomment this line to use long range mode. This // increases the sensitivity of the sensor and extends its // potential range, but increases the likelihood of getting @@ -56,11 +55,25 @@ float waterlevelA_calib_reservoirArea=20*20*3.1416; //area in cm^2. barrel diame uint16_t distanceA_unsuccessful_count=0; -// +++++++++++++++ VL6180X +++++++++++++++ -VL6180X sensorB; -// To try different scaling factors, change the following define. -// Valid scaling factors are 1, 2, or 3. -#define SCALING 1 +// +++++++++++++++ VL53L0X +++++++++++++++ +VL53L0X sensorB; + +#define PIN_VL53L0X_XSHUT_A 19 +// Uncomment this line to use long range mode. This +// increases the sensitivity of the sensor and extends its +// potential range, but increases the likelihood of getting +// an inaccurate reading because of reflections from objects +// other than the intended target. It works best in dark +// conditions. + +//#define LONG_RANGE + +// Uncomment ONE of these two lines to get +// - higher speed at the cost of lower accuracy OR +// - higher accuracy at the cost of lower speed + +//#define HIGH_SPEED +#define HIGH_ACCURACY @@ -72,8 +85,8 @@ float watervolumeB=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir //Calibration -float waterlevelB_calib_offset=260.86; //c -float waterlevelB_calib_factor=-1.107; //m +float waterlevelB_calib_offset=532.78; //c +float waterlevelB_calib_factor=-1.179; //m float waterlevelB_calib_reservoirArea=56.5*36.5; //area in cm^2 @@ -82,7 +95,6 @@ uint16_t distanceB_unsuccessful_count=0; - float waterlevelA_heightToVolume(float distance); float waterlevelB_heightToVolume(float distance); @@ -93,8 +105,8 @@ mqttValueTiming timing_waterlevelB; void waterlevel_setup() { - pinMode(PIN_VL53L0X_XSHUT, OUTPUT); - digitalWrite(PIN_VL53L0X_XSHUT, LOW); //pull to GND + pinMode(PIN_VL53L0X_XSHUT_A, OUTPUT); + digitalWrite(PIN_VL53L0X_XSHUT_A, LOW); //pull to GND @@ -157,33 +169,46 @@ void waterlevel_setup() { Serial.println(Wire.getClock()); + //Initialize SensorB first sensorB.setTimeout(1000); - Serial.println("init A"); - sensorB.init(); + if (!sensorB.init()) + { + Serial.println("Failed to detect and initialize sensorA!"); + publishInfo("error/waterlevel","Failed to detect and initialize sensorA"); + delay(1000); + } + Serial.println("set addr 0x2A"); sensorB.setAddress(0x2A); //change address Serial.println("conf Default"); - sensorB.configureDefault(); - Serial.println("set scaling"); - sensorB.setScaling(SCALING); - - - - /* - Serial.println("Connect second sensor now!"); - delay(1000); - Serial.println("waiting 5s"); - delay(5000); - Serial.println("done waiting");*/ + + #if defined LONG_RANGE + // lower the return signal rate limit (default is 0.25 MCPS) + sensorB.setSignalRateLimit(0.1); + // increase laser pulse periods (defaults are 14 and 10 PCLKs) + sensorB.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18); + sensorB.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14); + #endif + + #if defined HIGH_SPEED + // reduce timing budget to 20 ms (default is about 33 ms) + sensorB.setMeasurementTimingBudget(20000); + #elif defined HIGH_ACCURACY + // increase timing budget to 200 ms + sensorB.setMeasurementTimingBudget(200000); + #endif + + + // Stop driving this sensor's XSHUT low. This should allow the carrier // board to pull it high. (We do NOT want to drive XSHUT high since it is // not level shifted.) Then wait a bit for the sensor to start up. - pinMode(PIN_VL53L0X_XSHUT, INPUT); + pinMode(PIN_VL53L0X_XSHUT_A, INPUT); delay(50); - + //Initialize Sensor A after SensorB's address was changed sensorA.setTimeout(1000); if (!sensorA.init()) { @@ -258,7 +283,7 @@ void waterlevel_loop(unsigned long loopmillis) { if (isValueArrayOKf(waterlevelAMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){ float _filteredDistance=getFilteredf(waterlevelAMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF); - //Serial.print("Filtered reading A="); Serial.print(_filteredDistance);Serial.println(); + Serial.print("Filtered reading A="); Serial.print(_filteredDistance);Serial.println(); //Invert distance and offset waterlevelA=constrain(waterlevelA_calib_offset+waterlevelA_calib_factor*_filteredDistance,0,1000); @@ -305,7 +330,7 @@ void waterlevel_loop(unsigned long loopmillis) { if (isValueArrayOKf(waterlevelBMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){ float _filteredDistance=getFilteredf(waterlevelBMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF); - //Serial.print("Filtered reading B="); Serial.print(_filteredDistance);Serial.println(); + Serial.print("Filtered reading B="); Serial.print(_filteredDistance);Serial.println(); //Invert distance and offset waterlevelB=constrain(waterlevelB_calib_offset+waterlevelB_calib_factor*_filteredDistance,0,1000); watervolumeB=waterlevelB_heightToVolume(waterlevelB); diff --git a/include/wifi_functions.h b/include/wifi_functions.h index fa1b438..842a89a 100644 --- a/include/wifi_functions.h +++ b/include/wifi_functions.h @@ -76,6 +76,10 @@ void messageReceived(String &topic, String &payload) { force_ec_measurement=true; Serial.println("Forced EC Measurement"); } + if (topic==((String)client_id+"/errorack") && payload=="true") { //error acknowledge + valueError=false; + Serial.println("Reset value error flag"); + } } bool mqtt_loop(unsigned long loopmillis) { diff --git a/src/main.cpp b/src/main.cpp index 020b8ad..0b56683 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -150,14 +150,20 @@ void loop() { if (!eccalibrationoutput && !digitalRead(PIN_BUTTON)) { valueError=false; Serial.println("Reset ValueError flag by user"); - digitalWrite(PIN_LED,valueError); + digitalWrite(PIN_LED,valueError); //set led before delay to blink if error persists delay(100); } + bool last_valueError=true; + if (!eccalibrationoutput && last_valueError!=valueError) { //update led if valueerror flag changed + last_valueError=valueError; + digitalWrite(PIN_LED,valueError); + } + - if (eccalibrationoutput && !digitalRead(PIN_BUTTON) && !getReading) { + if (eccalibrationoutput && !digitalRead(PIN_BUTTON) && !getReading) { //Calibration UI if (!isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)) { for (uint8_t blink=0;blink<5;blink++) { digitalWrite(PIN_LED,HIGH); @@ -174,7 +180,7 @@ void loop() { } - if (eccalibrationoutput && ec_flag_measurement_available && getReading) { + if (eccalibrationoutput && ec_flag_measurement_available && getReading) { //Calibration UI ec_flag_measurement_available=false; getReading=false;