Compare commits

..

2 Commits

Author SHA1 Message Date
interfisch 5958942d9f add reboot via mqtt 2024-05-12 09:37:24 +02:00
interfisch f20d6a528f add error acknowledgment via mqtt 2024-05-12 09:23:41 +02:00
5 changed files with 108 additions and 52 deletions

1
.gitignore vendored
View File

@ -7,3 +7,4 @@
.piolibdeps .piolibdeps
.clang_complete .clang_complete
.gcc-flags.json .gcc-flags.json
messung/*

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,15 @@ 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");
}
if (topic==((String)client_id+"/reboot") && payload=="true") { //error acknowledge
Serial.println("Reboot by mqtt");
delay(100);
ESP.restart();
}
} }
bool mqtt_loop(unsigned long loopmillis) { bool mqtt_loop(unsigned long loopmillis) {

View File

@ -3,11 +3,14 @@
/*TODO /*TODO
mqtt function_: valueerror quittieren mqtt function_: valueerror quittieren
mqtt function: device reboot
check if sendallnext_flag does anything
Do not send first flow value (when filter aray is not full)
*/ */
bool valueError=false;
unsigned long last_check=0;
#include "wifi_functions.h" #include "wifi_functions.h"
bool debug=false; //print Serial information bool debug=false; //print Serial information
@ -18,6 +21,8 @@ sudo stty -F /dev/ttyUSB0 115200
cat /dev/ttyUSB0 | tee received.txt cat /dev/ttyUSB0 | tee received.txt
*/ */
bool valuesStabilized=false; //gets set true when values are stable (avaeraging arrays filled)
@ -51,9 +56,6 @@ ADS1115 ADS(0x48);
bool valueError=false;
unsigned long last_check=0;
@ -150,14 +152,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 +182,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;
@ -194,8 +202,12 @@ void loop() {
last_check=loopmillis; last_check=loopmillis;
bool _noErrorsDuringLoop=true;
if (tempCmean_reservoir_a==DEVICE_DISCONNECTED_C || tempCmean_reservoir_b==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) { if (tempCmean_reservoir_a==DEVICE_DISCONNECTED_C || tempCmean_reservoir_b==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) {
valueError=true; valueError=true;
_noErrorsDuringLoop=false;
} }
/* /*
@ -204,6 +216,7 @@ void loop() {
}*/ }*/
if (ec_A==EC_UNAVAILABLE || ec_B==EC_UNAVAILABLE){ if (ec_A==EC_UNAVAILABLE || ec_B==EC_UNAVAILABLE){
valueError=true; valueError=true;
_noErrorsDuringLoop=false;
} }
@ -213,9 +226,11 @@ void loop() {
if (distanceA_unsuccessful_count>20) { if (distanceA_unsuccessful_count>20) {
valueError=true; valueError=true;
_noErrorsDuringLoop=false;
} }
if (distanceB_unsuccessful_count>20) { if (distanceB_unsuccessful_count>20) {
valueError=true; valueError=true;
_noErrorsDuringLoop=false;
} }
@ -292,8 +307,11 @@ void loop() {
Serial.println(); 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");
}
} }
@ -328,21 +346,24 @@ void loop() {
static float last_flow_a=0; static float last_flow_a=0;
if (flow_a==0.0 && last_flow_a!=flow_a) { if (valuesStabilized){
publishValueTimedOverride("nft/flow",flow_a,2,timing_flow_a,loopmillis); //publish without waiting if flow is 0 if (flow_a==0.0 && last_flow_a!=flow_a) {
}else{ publishValueTimedOverride("nft/flow",flow_a,2,timing_flow_a,loopmillis); //publish without waiting if flow is 0
publishValueTimed("nft/flow",flow_a,2,timing_flow_a,loopmillis); }else{
publishValueTimed("nft/flow",flow_a,2,timing_flow_a,loopmillis);
}
last_flow_a=flow_a;
} }
last_flow_a=flow_a;
static float last_flow_b=0; static float last_flow_b=0;
if (flow_b==0.0 && last_flow_b!=flow_b) { if (valuesStabilized){
publishValueTimedOverride("db/flow",flow_b,2,timing_flow_b,loopmillis); //publish without waiting if flow is 0 if (flow_b==0.0 && last_flow_b!=flow_b) {
}else{ publishValueTimedOverride("db/flow",flow_b,2,timing_flow_b,loopmillis); //publish without waiting if flow is 0
publishValueTimed("db/flow",flow_b,2,timing_flow_b,loopmillis); }else{
publishValueTimed("db/flow",flow_b,2,timing_flow_b,loopmillis);
}
last_flow_b=flow_b;
} }
last_flow_b=flow_b;