add error acknowledgment via mqtt

This commit is contained in:
interfisch 2024-05-12 09:23:41 +02:00
parent ec6ad40a34
commit f20d6a528f
4 changed files with 68 additions and 33 deletions

View File

@ -3,7 +3,6 @@
#include <Wire.h> #include <Wire.h>
#include <VL53L0X.h> //pololu/VL53L0X@^1.3.1 #include <VL53L0X.h> //pololu/VL53L0X@^1.3.1
#include <VL6180X.h> //https://github.com/pololu/vl6180x-arduino
@ -21,7 +20,7 @@
// +++++++++++++++ VL53L0X +++++++++++++++ // +++++++++++++++ VL53L0X +++++++++++++++
VL53L0X sensorA; VL53L0X sensorA;
#define PIN_VL53L0X_XSHUT 19 #define PIN_VL53L0X_XSHUT_A 19
// Uncomment this line to use long range mode. This // Uncomment this line to use long range mode. This
// increases the sensitivity of the sensor and extends its // increases the sensitivity of the sensor and extends its
// potential range, but increases the likelihood of getting // 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; uint16_t distanceA_unsuccessful_count=0;
// +++++++++++++++ VL6180X +++++++++++++++ // +++++++++++++++ VL53L0X +++++++++++++++
VL6180X sensorB; VL53L0X sensorB;
// To try different scaling factors, change the following define.
// Valid scaling factors are 1, 2, or 3. #define PIN_VL53L0X_XSHUT_A 19
#define SCALING 1 // 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 //Calibration
float waterlevelB_calib_offset=260.86; //c float waterlevelB_calib_offset=532.78; //c
float waterlevelB_calib_factor=-1.107; //m float waterlevelB_calib_factor=-1.179; //m
float waterlevelB_calib_reservoirArea=56.5*36.5; //area in cm^2 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 waterlevelA_heightToVolume(float distance);
float waterlevelB_heightToVolume(float distance); float waterlevelB_heightToVolume(float distance);
@ -93,8 +105,8 @@ mqttValueTiming timing_waterlevelB;
void waterlevel_setup() { void waterlevel_setup() {
pinMode(PIN_VL53L0X_XSHUT, OUTPUT); pinMode(PIN_VL53L0X_XSHUT_A, OUTPUT);
digitalWrite(PIN_VL53L0X_XSHUT, LOW); //pull to GND digitalWrite(PIN_VL53L0X_XSHUT_A, LOW); //pull to GND
@ -157,33 +169,46 @@ void waterlevel_setup() {
Serial.println(Wire.getClock()); Serial.println(Wire.getClock());
//Initialize SensorB first
sensorB.setTimeout(1000); sensorB.setTimeout(1000);
Serial.println("init A"); if (!sensorB.init())
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"); Serial.println("set addr 0x2A");
sensorB.setAddress(0x2A); //change address sensorB.setAddress(0x2A); //change address
Serial.println("conf Default"); Serial.println("conf Default");
sensorB.configureDefault();
Serial.println("set scaling");
sensorB.setScaling(SCALING);
/* #if defined LONG_RANGE
Serial.println("Connect second sensor now!"); // lower the return signal rate limit (default is 0.25 MCPS)
delay(1000); sensorB.setSignalRateLimit(0.1);
Serial.println("waiting 5s"); // increase laser pulse periods (defaults are 14 and 10 PCLKs)
delay(5000); sensorB.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
Serial.println("done waiting");*/ 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 // 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 // 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. // 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); delay(50);
//Initialize Sensor A after SensorB's address was changed
sensorA.setTimeout(1000); sensorA.setTimeout(1000);
if (!sensorA.init()) if (!sensorA.init())
{ {
@ -258,7 +283,7 @@ void waterlevel_loop(unsigned long loopmillis) {
if (isValueArrayOKf(waterlevelAMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){ if (isValueArrayOKf(waterlevelAMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){
float _filteredDistance=getFilteredf(waterlevelAMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF); 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 //Invert distance and offset
waterlevelA=constrain(waterlevelA_calib_offset+waterlevelA_calib_factor*_filteredDistance,0,1000); 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)){ if (isValueArrayOKf(waterlevelBMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){
float _filteredDistance=getFilteredf(waterlevelBMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF); 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 //Invert distance and offset
waterlevelB=constrain(waterlevelB_calib_offset+waterlevelB_calib_factor*_filteredDistance,0,1000); waterlevelB=constrain(waterlevelB_calib_offset+waterlevelB_calib_factor*_filteredDistance,0,1000);
watervolumeB=waterlevelB_heightToVolume(waterlevelB); watervolumeB=waterlevelB_heightToVolume(waterlevelB);

View File

@ -76,6 +76,10 @@ void messageReceived(String &topic, String &payload) {
force_ec_measurement=true; force_ec_measurement=true;
Serial.println("Forced EC Measurement"); 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) { bool mqtt_loop(unsigned long loopmillis) {

View File

@ -150,14 +150,20 @@ void loop() {
if (!eccalibrationoutput && !digitalRead(PIN_BUTTON)) { if (!eccalibrationoutput && !digitalRead(PIN_BUTTON)) {
valueError=false; valueError=false;
Serial.println("Reset ValueError flag by user"); 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); 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)) { if (!isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)) {
for (uint8_t blink=0;blink<5;blink++) { for (uint8_t blink=0;blink<5;blink++) {
digitalWrite(PIN_LED,HIGH); 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; ec_flag_measurement_available=false;
getReading=false; getReading=false;