From 7b346934ad70ec57fcfa359cdcd32f962e64beb1 Mon Sep 17 00:00:00 2001 From: Fisch Date: Sun, 12 May 2024 11:51:59 +0200 Subject: [PATCH] calibrate waterlevel sensors and fix new mqtt functions --- include/waterlevel.h | 35 ++++++++++++++++---------- include/wifi_functions.h | 2 ++ platformio.ini | 3 +-- src/main.cpp | 54 ++++++++++++++++------------------------ 4 files changed, 47 insertions(+), 47 deletions(-) diff --git a/include/waterlevel.h b/include/waterlevel.h index 99b2ef0..af51f12 100644 --- a/include/waterlevel.h +++ b/include/waterlevel.h @@ -47,8 +47,8 @@ float watervolumeA=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir //Calibration -float waterlevelA_calib_offset=532.78; //c -float waterlevelA_calib_factor=-1.179; //m +float waterlevelA_calib_offset=500.0; //c +float waterlevelA_calib_factor=-1.0; //m float waterlevelA_calib_reservoirArea=20*20*3.1416; //area in cm^2. barrel diameter inside is 400mm @@ -58,7 +58,7 @@ uint16_t distanceA_unsuccessful_count=0; // +++++++++++++++ VL53L0X +++++++++++++++ VL53L0X sensorB; -#define PIN_VL53L0X_XSHUT_A 19 +#define PIN_VL53L0X_XSHUT_B 23 // 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 @@ -85,8 +85,8 @@ float watervolumeB=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir //Calibration -float waterlevelB_calib_offset=532.78; //c -float waterlevelB_calib_factor=-1.179; //m +float waterlevelB_calib_offset=273.0; //c +float waterlevelB_calib_factor=-1.0; //m float waterlevelB_calib_reservoirArea=56.5*36.5; //area in cm^2 @@ -108,6 +108,10 @@ void waterlevel_setup() { pinMode(PIN_VL53L0X_XSHUT_A, OUTPUT); digitalWrite(PIN_VL53L0X_XSHUT_A, LOW); //pull to GND + pinMode(PIN_VL53L0X_XSHUT_B, OUTPUT); + digitalWrite(PIN_VL53L0X_XSHUT_B, LOW); //pull to GND + + @@ -162,19 +166,22 @@ void waterlevel_setup() { timing_waterlevelB.mintime=30*000; timing_waterlevelB.maxtime=60*60*1000; - + pinMode(PIN_VL53L0X_XSHUT_B, INPUT); //Enable Sensor B first, to change its address + delay(50); Wire.begin(); Serial.print("I2C Clock Speed="); Serial.println(Wire.getClock()); + delay(100); + //Initialize SensorB first - sensorB.setTimeout(1000); + sensorB.setTimeout(2000); if (!sensorB.init()) { - Serial.println("Failed to detect and initialize sensorA!"); - publishInfo("error/waterlevel","Failed to detect and initialize sensorA"); + Serial.println("Failed to detect and initialize sensorB!"); + publishInfo("error/waterlevel","Failed to detect and initialize sensorB"); delay(1000); } @@ -209,7 +216,7 @@ void waterlevel_setup() { delay(50); //Initialize Sensor A after SensorB's address was changed - sensorA.setTimeout(1000); + sensorA.setTimeout(2000); if (!sensorA.init()) { Serial.println("Failed to detect and initialize sensorA!"); @@ -283,7 +290,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); @@ -310,7 +317,7 @@ void waterlevel_loop(unsigned long loopmillis) { //Serial.print("Distance reading B="); Serial.print(distance);Serial.println(); - if (distance!=WATERLEVEL_UNAVAILABLE) { //successful + if (distance!=WATERLEVEL_UNAVAILABLE && distance!=65535) { //successful waterlevelBMean_array[waterlevelBMean_array_pos]=distance; waterlevelBMean_array_pos++; waterlevelBMean_array_pos%=WATERLEVELMEAN_SIZE; @@ -330,11 +337,13 @@ 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(); + //Invert distance and offset waterlevelB=constrain(waterlevelB_calib_offset+waterlevelB_calib_factor*_filteredDistance,0,1000); watervolumeB=waterlevelB_heightToVolume(waterlevelB); + //Serial.print("Filtered reading B="); Serial.print(_filteredDistance); Serial.print(" fixed="); Serial.println(waterlevelB); Serial.println(); + //float _meanWaterlevel=getMeanf(waterlevelMean,WATERLEVELMEAN_SIZE); //Serial.print("\t Dist="); Serial.print(_filteredWaterlevel); Serial.print("mm"); Serial.print("(+- "); Serial.print((getMaxf(waterlevelMean,WATERLEVELMEAN_SIZE)-getMinf(waterlevelMean,WATERLEVELMEAN_SIZE))/2.0); Serial.print(")"); Serial.print(" [mean="); Serial.print(_meanWaterlevel); Serial.print("]"); diff --git a/include/wifi_functions.h b/include/wifi_functions.h index d688c82..07d16e7 100644 --- a/include/wifi_functions.h +++ b/include/wifi_functions.h @@ -58,6 +58,8 @@ void connect() { Serial.println("\nconnected!"); client.subscribe((String)client_id+"/sendall"); client.subscribe((String)client_id+"/ec/trigger"); + client.subscribe((String)client_id+"/errorack"); + client.subscribe((String)client_id+"/reboot"); // client.unsubscribe("/hello"); } diff --git a/platformio.ini b/platformio.ini index a0f4bf2..4d66040 100644 --- a/platformio.ini +++ b/platformio.ini @@ -20,5 +20,4 @@ lib_deps = https://github.com/emilv/ArduinoSort/ robtillaart/ADS1X15@^0.3.9 256dpi/MQTT@^2.5.1 - pololu/VL53L0X@^1.3.1 - https://github.com/pololu/vl6180x-arduino \ No newline at end of file + pololu/VL53L0X@^1.3.1 \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8fa7138..08aa4d6 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,12 +1,6 @@ #include -/*TODO -mqtt function_: valueerror quittieren - -*/ - - bool valueError=false; unsigned long last_check=0; @@ -55,14 +49,6 @@ ADS1115 ADS(0x48); - - - - - - - - #define PIN_BUTTON 12 #define PIN_LED 13 @@ -149,17 +135,23 @@ void loop() { static bool getReading=false; - if (!eccalibrationoutput && !digitalRead(PIN_BUTTON)) { - valueError=false; - Serial.println("Reset ValueError flag by user"); - digitalWrite(PIN_LED,valueError); //set led before delay to blink if error persists - delay(100); - } + if (!eccalibrationoutput) { //Is in normal operation mode + if (!digitalRead(PIN_BUTTON)) { //button pressed + valueError=false; + Serial.println("Reset ValueError flag by user"); + 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); + static bool last_valueError=true; + if (!valuesStabilized) { //if values are not okay since boot + digitalWrite(PIN_LED,(loopmillis/250)%2==0); //blink led + }else{ //LED shows valueError flag status when values were okay once + if (last_valueError!=valueError) { //update led if valueerror flag changed + last_valueError=valueError; + digitalWrite(PIN_LED,valueError); + } + } } @@ -220,9 +212,6 @@ void loop() { } - if (!eccalibrationoutput) { - digitalWrite(PIN_LED,valueError); - } if (distanceA_unsuccessful_count>20) { valueError=true; @@ -235,6 +224,12 @@ void loop() { + if (_noErrorsDuringLoop && !valuesStabilized) { + valuesStabilized=true; //gets only set to true once + valueError=false; //clear error flag once after boot + Serial.println("Values Stable, clear error flag"); + } + if (debug) { Serial.println("_______________________"); Serial.print(millis()/1000.0,2); Serial.println(":"); @@ -307,11 +302,6 @@ void loop() { Serial.println(); - if (_noErrorsDuringLoop && !valuesStabilized) { - valuesStabilized=true; //gets only set to true once - valueError=false; //clear error flag once after boot - Serial.println("Values Stable, clear error flag"); - } }