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
.clang_complete
.gcc-flags.json
messung/*

View File

@ -3,7 +3,6 @@
#include <Wire.h>
#include <VL53L0X.h> //pololu/VL53L0X@^1.3.1
#include <VL6180X.h> //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);

View File

@ -76,6 +76,15 @@ 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");
}
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) {

View File

@ -3,11 +3,14 @@
/*TODO
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"
bool debug=false; //print Serial information
@ -18,6 +21,8 @@ sudo stty -F /dev/ttyUSB0 115200
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)) {
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 +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;
getReading=false;
@ -194,8 +202,12 @@ void loop() {
last_check=loopmillis;
bool _noErrorsDuringLoop=true;
if (tempCmean_reservoir_a==DEVICE_DISCONNECTED_C || tempCmean_reservoir_b==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) {
valueError=true;
_noErrorsDuringLoop=false;
}
/*
@ -204,6 +216,7 @@ void loop() {
}*/
if (ec_A==EC_UNAVAILABLE || ec_B==EC_UNAVAILABLE){
valueError=true;
_noErrorsDuringLoop=false;
}
@ -213,9 +226,11 @@ void loop() {
if (distanceA_unsuccessful_count>20) {
valueError=true;
_noErrorsDuringLoop=false;
}
if (distanceB_unsuccessful_count>20) {
valueError=true;
_noErrorsDuringLoop=false;
}
@ -292,8 +307,11 @@ 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");
}
}
@ -328,21 +346,24 @@ void loop() {
static float last_flow_a=0;
if (valuesStabilized){
if (flow_a==0.0 && last_flow_a!=flow_a) {
publishValueTimedOverride("nft/flow",flow_a,2,timing_flow_a,loopmillis); //publish without waiting if flow is 0
}else{
publishValueTimed("nft/flow",flow_a,2,timing_flow_a,loopmillis);
}
last_flow_a=flow_a;
}
static float last_flow_b=0;
if (valuesStabilized){
if (flow_b==0.0 && last_flow_b!=flow_b) {
publishValueTimedOverride("db/flow",flow_b,2,timing_flow_b,loopmillis); //publish without waiting if flow is 0
}else{
publishValueTimed("db/flow",flow_b,2,timing_flow_b,loopmillis);
}
last_flow_b=flow_b;
}