calibrate waterlevel sensors and fix new mqtt functions
This commit is contained in:
parent
5958942d9f
commit
7b346934ad
|
@ -47,8 +47,8 @@ float watervolumeA=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir
|
||||||
|
|
||||||
|
|
||||||
//Calibration
|
//Calibration
|
||||||
float waterlevelA_calib_offset=532.78; //c
|
float waterlevelA_calib_offset=500.0; //c
|
||||||
float waterlevelA_calib_factor=-1.179; //m
|
float waterlevelA_calib_factor=-1.0; //m
|
||||||
|
|
||||||
|
|
||||||
float waterlevelA_calib_reservoirArea=20*20*3.1416; //area in cm^2. barrel diameter inside is 400mm
|
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 +++++++++++++++
|
||||||
VL53L0X sensorB;
|
VL53L0X sensorB;
|
||||||
|
|
||||||
#define PIN_VL53L0X_XSHUT_A 19
|
#define PIN_VL53L0X_XSHUT_B 23
|
||||||
// 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
|
||||||
|
@ -85,8 +85,8 @@ float watervolumeB=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir
|
||||||
|
|
||||||
|
|
||||||
//Calibration
|
//Calibration
|
||||||
float waterlevelB_calib_offset=532.78; //c
|
float waterlevelB_calib_offset=273.0; //c
|
||||||
float waterlevelB_calib_factor=-1.179; //m
|
float waterlevelB_calib_factor=-1.0; //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
|
||||||
|
@ -108,6 +108,10 @@ void waterlevel_setup() {
|
||||||
pinMode(PIN_VL53L0X_XSHUT_A, OUTPUT);
|
pinMode(PIN_VL53L0X_XSHUT_A, OUTPUT);
|
||||||
digitalWrite(PIN_VL53L0X_XSHUT_A, LOW); //pull to GND
|
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.mintime=30*000;
|
||||||
timing_waterlevelB.maxtime=60*60*1000;
|
timing_waterlevelB.maxtime=60*60*1000;
|
||||||
|
|
||||||
|
pinMode(PIN_VL53L0X_XSHUT_B, INPUT); //Enable Sensor B first, to change its address
|
||||||
|
delay(50);
|
||||||
|
|
||||||
Wire.begin();
|
Wire.begin();
|
||||||
Serial.print("I2C Clock Speed=");
|
Serial.print("I2C Clock Speed=");
|
||||||
Serial.println(Wire.getClock());
|
Serial.println(Wire.getClock());
|
||||||
|
|
||||||
|
delay(100);
|
||||||
|
|
||||||
|
|
||||||
//Initialize SensorB first
|
//Initialize SensorB first
|
||||||
sensorB.setTimeout(1000);
|
sensorB.setTimeout(2000);
|
||||||
if (!sensorB.init())
|
if (!sensorB.init())
|
||||||
{
|
{
|
||||||
Serial.println("Failed to detect and initialize sensorA!");
|
Serial.println("Failed to detect and initialize sensorB!");
|
||||||
publishInfo("error/waterlevel","Failed to detect and initialize sensorA");
|
publishInfo("error/waterlevel","Failed to detect and initialize sensorB");
|
||||||
delay(1000);
|
delay(1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -209,7 +216,7 @@ void waterlevel_setup() {
|
||||||
delay(50);
|
delay(50);
|
||||||
|
|
||||||
//Initialize Sensor A after SensorB's address was changed
|
//Initialize Sensor A after SensorB's address was changed
|
||||||
sensorA.setTimeout(1000);
|
sensorA.setTimeout(2000);
|
||||||
if (!sensorA.init())
|
if (!sensorA.init())
|
||||||
{
|
{
|
||||||
Serial.println("Failed to detect and initialize sensorA!");
|
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)){
|
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);
|
||||||
|
@ -310,7 +317,7 @@ void waterlevel_loop(unsigned long loopmillis) {
|
||||||
//Serial.print("Distance reading B="); Serial.print(distance);Serial.println();
|
//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[waterlevelBMean_array_pos]=distance;
|
||||||
waterlevelBMean_array_pos++;
|
waterlevelBMean_array_pos++;
|
||||||
waterlevelBMean_array_pos%=WATERLEVELMEAN_SIZE;
|
waterlevelBMean_array_pos%=WATERLEVELMEAN_SIZE;
|
||||||
|
@ -330,11 +337,13 @@ 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();
|
|
||||||
//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);
|
||||||
|
|
||||||
|
//Serial.print("Filtered reading B="); Serial.print(_filteredDistance); Serial.print(" fixed="); Serial.println(waterlevelB); Serial.println();
|
||||||
|
|
||||||
|
|
||||||
//float _meanWaterlevel=getMeanf(waterlevelMean,WATERLEVELMEAN_SIZE);
|
//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("]");
|
//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("]");
|
||||||
|
|
|
@ -58,6 +58,8 @@ void connect() {
|
||||||
Serial.println("\nconnected!");
|
Serial.println("\nconnected!");
|
||||||
client.subscribe((String)client_id+"/sendall");
|
client.subscribe((String)client_id+"/sendall");
|
||||||
client.subscribe((String)client_id+"/ec/trigger");
|
client.subscribe((String)client_id+"/ec/trigger");
|
||||||
|
client.subscribe((String)client_id+"/errorack");
|
||||||
|
client.subscribe((String)client_id+"/reboot");
|
||||||
// client.unsubscribe("/hello");
|
// client.unsubscribe("/hello");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,5 +20,4 @@ lib_deps =
|
||||||
https://github.com/emilv/ArduinoSort/
|
https://github.com/emilv/ArduinoSort/
|
||||||
robtillaart/ADS1X15@^0.3.9
|
robtillaart/ADS1X15@^0.3.9
|
||||||
256dpi/MQTT@^2.5.1
|
256dpi/MQTT@^2.5.1
|
||||||
pololu/VL53L0X@^1.3.1
|
pololu/VL53L0X@^1.3.1
|
||||||
https://github.com/pololu/vl6180x-arduino
|
|
54
src/main.cpp
54
src/main.cpp
|
@ -1,12 +1,6 @@
|
||||||
#include <Arduino.h>
|
#include <Arduino.h>
|
||||||
|
|
||||||
|
|
||||||
/*TODO
|
|
||||||
mqtt function_: valueerror quittieren
|
|
||||||
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
bool valueError=false;
|
bool valueError=false;
|
||||||
|
|
||||||
unsigned long last_check=0;
|
unsigned long last_check=0;
|
||||||
|
@ -55,14 +49,6 @@ ADS1115 ADS(0x48);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#define PIN_BUTTON 12
|
#define PIN_BUTTON 12
|
||||||
#define PIN_LED 13
|
#define PIN_LED 13
|
||||||
|
|
||||||
|
@ -149,17 +135,23 @@ void loop() {
|
||||||
|
|
||||||
static bool getReading=false;
|
static bool getReading=false;
|
||||||
|
|
||||||
if (!eccalibrationoutput && !digitalRead(PIN_BUTTON)) {
|
if (!eccalibrationoutput) { //Is in normal operation mode
|
||||||
valueError=false;
|
if (!digitalRead(PIN_BUTTON)) { //button pressed
|
||||||
Serial.println("Reset ValueError flag by user");
|
valueError=false;
|
||||||
digitalWrite(PIN_LED,valueError); //set led before delay to blink if error persists
|
Serial.println("Reset ValueError flag by user");
|
||||||
delay(100);
|
digitalWrite(PIN_LED,valueError); //set led before delay to blink if error persists
|
||||||
}
|
delay(100);
|
||||||
|
}
|
||||||
|
|
||||||
bool last_valueError=true;
|
static bool last_valueError=true;
|
||||||
if (!eccalibrationoutput && last_valueError!=valueError) { //update led if valueerror flag changed
|
if (!valuesStabilized) { //if values are not okay since boot
|
||||||
last_valueError=valueError;
|
digitalWrite(PIN_LED,(loopmillis/250)%2==0); //blink led
|
||||||
digitalWrite(PIN_LED,valueError);
|
}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) {
|
if (distanceA_unsuccessful_count>20) {
|
||||||
valueError=true;
|
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) {
|
if (debug) {
|
||||||
Serial.println("_______________________");
|
Serial.println("_______________________");
|
||||||
Serial.print(millis()/1000.0,2); Serial.println(":");
|
Serial.print(millis()/1000.0,2); Serial.println(":");
|
||||||
|
@ -307,11 +302,6 @@ 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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue