From 5571267f6c07e982cbf30977a8c7f482e2735483 Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 21 Jun 2023 09:00:29 +0200 Subject: [PATCH] fix waterlevel calculation and calibrate --- include/waterlevel.h | 20 ++++++++++++++------ include/wifi_functions.h | 6 ++++-- messung/approximate.py | 2 +- src/main.cpp | 9 ++++++++- 4 files changed, 27 insertions(+), 10 deletions(-) diff --git a/include/waterlevel.h b/include/waterlevel.h index 9c6c1a7..ffe51c3 100644 --- a/include/waterlevel.h +++ b/include/waterlevel.h @@ -23,8 +23,9 @@ float watervolume=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir //Calibration -float waterlevel_calib_offset_measured=0; //Sollwert -float waterlevel_calib_offset_sensor=0; //Istwert +float waterlevel_calib_offset_measured=86; //Sollwert +float waterlevel_calib_offset_sensor=78; //Istwert +//raw reading is 78mm, ruler reads 86mm. VL8160 sensor is 169mm above bottom of reservoir. float waterlevel_calib_reservoirArea=27*36.5; //area in cm^2 @@ -32,8 +33,15 @@ float waterlevel_calib_reservoirArea=27*36.5; //area in cm^2 float waterlevel_heightToVolume(float distance); +mqttValueTiming timing_waterlevel; + void waterlevel_setup() { + timing_waterlevel.minchange=0.0; + timing_waterlevel.maxchange=3.0; + timing_waterlevel.mintime=30*000; + timing_waterlevel.maxtime=60*60*1000; + Wire.begin(); sensor.init(); @@ -65,12 +73,12 @@ void waterlevel_loop(unsigned long loopmillis) { } if (isValueArrayOKf(waterlevelMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){ - //float _distance=getFilteredf(waterlevelMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF); - float _distance=getMaxf(waterlevelMean_array,WATERLEVELMEAN_SIZE); + float _filteredDistance=getFilteredf(waterlevelMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF); + //Invert distance and offset - waterlevel=distance-(waterlevel_calib_offset_sensor+waterlevel_calib_offset_measured); - watervolume=waterlevel_heightToVolume(_distance); + waterlevel=(waterlevel_calib_offset_sensor+waterlevel_calib_offset_measured)-_filteredDistance; + watervolume=waterlevel_heightToVolume(waterlevel); //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 cb6954b..0cabfbd 100644 --- a/include/wifi_functions.h +++ b/include/wifi_functions.h @@ -29,7 +29,7 @@ bool enableTiming=true; -void publishValueTimed(String topic,float value,uint8_t decimals,mqttValueTiming &mqttvt,unsigned long loopmillis); +bool publishValueTimed(String topic,float value,uint8_t decimals,mqttValueTiming &mqttvt,unsigned long loopmillis); void publishValue(String topic,float value,uint8_t decimals); void connect() { @@ -97,7 +97,7 @@ bool mqtt_loop(unsigned long loopmillis) { return false; } -void publishValueTimed(String topic,float value,uint8_t decimals,mqttValueTiming &mqttvt,unsigned long loopmillis) { +bool publishValueTimed(String topic,float value,uint8_t decimals,mqttValueTiming &mqttvt,unsigned long loopmillis) { unsigned long timediff=loopmillis-mqttvt.lasttime; float valuediff=abs(value-mqttvt.lastvalue); valuediff=constrain(valuediff,mqttvt.minchange,mqttvt.maxchange); @@ -110,7 +110,9 @@ void publishValueTimed(String topic,float value,uint8_t decimals,mqttValueTiming mqttvt.lasttime=loopmillis; mqttvt.lastvalue=value; publishValue(topic,value,decimals); + return true; } + return false; } void publishValue(String topic,float value,uint8_t decimals) { diff --git a/messung/approximate.py b/messung/approximate.py index 92df6eb..cc4caf4 100644 --- a/messung/approximate.py +++ b/messung/approximate.py @@ -8,7 +8,7 @@ import matplotlib.pyplot as plt from pandas import * # reading CSV file -data = read_csv("20230509_NaCl_raw.csv") +data = read_csv("20230620_NaCl_raw.csv") # converting column data to list #solutionAdded = data['solutionAdded'].tolist() #in ml diff --git a/src/main.cpp b/src/main.cpp index da6be82..da71d13 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,7 +5,7 @@ #include "wifi_functions.h" bool debug=true; //print Serial information -bool mqtt=false; +bool mqtt=true; bool eccalibrationoutput=false; //serial output for ec calibration @@ -262,6 +262,13 @@ void loop() { } publishValueTimed("flow/flow",flow,2,timing_flow,loopmillis); + + if (waterlevel!=WATERLEVEL_UNAVAILABLE) { + bool _published=publishValueTimed("waterlevel/height",waterlevel,2,timing_waterlevel,loopmillis); + if (_published) { //use height for timing. send calculated volume with it + publishValue("waterlevel/volume",watervolume,2); + } + } if (ec_flag_measurement_available){