add vl6180 as waterlevel sensor

This commit is contained in:
interfisch 2023-06-20 20:06:41 +02:00
parent 2335291a93
commit 9f489c319f
3 changed files with 44 additions and 59 deletions

View File

@ -1,12 +1,17 @@
#ifndef _WATERLEVEL_H_ #ifndef _WATERLEVEL_H_
#define _WATERLEVEL_H_ #define _WATERLEVEL_H_
#include <Wire.h>
#include <VL6180X.h>
#include <HCSR04.h>
#define HCSR04_PIN_ECHO 17 VL6180X sensor;
#define HCSR04_PIN_TRIGGER 16 // To try different scaling factors, change the following define.
#define HCSR04_TIMEOUT 5000 //default is 100000 (uS) // Valid scaling factors are 1, 2, or 3.
#define READINTERVAL_HCSR04 200 #define SCALING 1
#define READINTERVAL_WATERLEVEL 200
#define WATERLEVELMEAN_SIZE 32 #define WATERLEVELMEAN_SIZE 32
#define WATERLEVELMEAN_FILTER_CUTOFF 8 //max value is around WATERLEVELMEAN_SIZE/2 #define WATERLEVELMEAN_FILTER_CUTOFF 8 //max value is around WATERLEVELMEAN_SIZE/2
@ -16,13 +21,10 @@ uint16_t waterlevelMean_array_pos=0;
float waterlevel=WATERLEVEL_UNAVAILABLE; //distance from floor to water surface [mm] float waterlevel=WATERLEVEL_UNAVAILABLE; //distance from floor to water surface [mm]
float watervolume=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir float watervolume=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir
uint16_t waterlevel_failcounter=0;
#define WATERLEVEL_MAXFAILS 15 //maximum counter value
#define WATERLEVEL_FAILTHRESHOLD 10 //if failcounter is greater or equal this value waterlevel will not be valid
//Calibration //Calibration
float waterlevel_calib_offset_measured=0; //Sollwert float waterlevel_calib_offset_measured=0; //Sollwert
float waterlevel_calib_offset_sensor=178.67; //Istwert float waterlevel_calib_offset_sensor=0; //Istwert
float waterlevel_calib_reservoirArea=27*36.5; //area in cm^2 float waterlevel_calib_reservoirArea=27*36.5; //area in cm^2
@ -32,8 +34,13 @@ float waterlevel_heightToVolume(float distance);
void waterlevel_setup() { void waterlevel_setup() {
//HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO); Wire.begin();
HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO,HCSR04_TIMEOUT, HCSR04.eUltraSonicUnlock_t::unlockSkip);
sensor.init();
sensor.configureDefault();
sensor.setScaling(SCALING);
sensor.setTimeout(500);
for (uint16_t i=0;i<WATERLEVELMEAN_SIZE;i++) { for (uint16_t i=0;i<WATERLEVELMEAN_SIZE;i++) {
waterlevelMean_array[i]=-1; //-1 is also timeout value waterlevelMean_array[i]=-1; //-1 is also timeout value
@ -42,33 +49,21 @@ void waterlevel_setup() {
void waterlevel_loop(unsigned long loopmillis) { void waterlevel_loop(unsigned long loopmillis) {
static unsigned long last_read_hcsr04; static unsigned long last_read_waterlevel;
if (loopmillis>=last_read_hcsr04+READINTERVAL_HCSR04) { if (loopmillis>=last_read_waterlevel+READINTERVAL_WATERLEVEL) {
last_read_hcsr04=loopmillis; last_read_waterlevel=loopmillis;
float temperature=20.0;
if (tempCmean_air!=DEVICE_DISCONNECTED_C) { //sensor ok
temperature=tempCmean_air; uint16_t distance=sensor.readRangeSingleMillimeters();
}
double* distances = HCSR04.measureDistanceMm(temperature);
double distance=distances[0];
//Serial.print("Distance reading:"); Serial.println(distance); //Serial.print("Distance reading:"); Serial.println(distance);
if (distance!=WATERLEVEL_UNAVAILABLE) { //successful if (distance!=WATERLEVEL_UNAVAILABLE) { //successful
waterlevelMean_array[waterlevelMean_array_pos]=distance; waterlevelMean_array[waterlevelMean_array_pos]=distance;
waterlevelMean_array_pos++; waterlevelMean_array_pos++;
waterlevelMean_array_pos%=WATERLEVELMEAN_SIZE; waterlevelMean_array_pos%=WATERLEVELMEAN_SIZE;
if (waterlevel_failcounter>0) { //reduce failcounter if sucessfull
waterlevel_failcounter--;
}
}else{
if (waterlevel_failcounter<WATERLEVEL_MAXFAILS) {
waterlevel_failcounter++;
}
} }
if (isValueArrayOKf(waterlevelMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){ if (isValueArrayOKf(waterlevelMean_array,WATERLEVELMEAN_SIZE,WATERLEVEL_UNAVAILABLE)){
//float _distance=getFilteredf(waterlevelMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF); //float _distance=getFilteredf(waterlevelMean_array,WATERLEVELMEAN_SIZE,WATERLEVELMEAN_FILTER_CUTOFF);
float _distance=getMaxf(waterlevelMean_array,WATERLEVELMEAN_SIZE); float _distance=getMaxf(waterlevelMean_array,WATERLEVELMEAN_SIZE);
@ -81,13 +76,6 @@ void waterlevel_loop(unsigned long loopmillis) {
//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("]");
} }
if (waterlevel_failcounter>=WATERLEVEL_FAILTHRESHOLD) { //too many failed readings
waterlevel=WATERLEVEL_UNAVAILABLE;
watervolume=WATERLEVEL_UNAVAILABLE;
/*if (debug) {
Serial.print("Waterlevel Failcounter="); Serial.println(waterlevel_failcounter);
}*/
}
} }
} }

View File

@ -17,7 +17,7 @@ monitor_speed = 115200
lib_deps = lib_deps =
https://github.com/milesburton/Arduino-Temperature-Control-Library/ https://github.com/milesburton/Arduino-Temperature-Control-Library/
d03n3rfr1tz3/HC-SR04@^1.1.2
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
https://github.com/pololu/vl6180x-arduino

View File

@ -5,6 +5,7 @@
#include "wifi_functions.h" #include "wifi_functions.h"
bool debug=true; //print Serial information bool debug=true; //print Serial information
bool mqtt=false;
@ -26,7 +27,7 @@ ADS1115 ADS(0x48);
// ######## Water Level // ######## Water Level
//#include "waterlevel.h" #include "waterlevel.h"
// ######## Flow Rate // ######## Flow Rate
@ -55,10 +56,12 @@ void setup() {
pinMode(PIN_LED,OUTPUT); pinMode(PIN_LED,OUTPUT);
digitalWrite(PIN_LED,LOW); digitalWrite(PIN_LED,LOW);
Serial.begin(115200); Serial.begin(115200);
WiFi.begin(ssid, pass); if (mqtt) {
client.begin(mqtt_host, net); WiFi.begin(ssid, pass);
client.onMessage(messageReceived); client.begin(mqtt_host, net);
connect(); client.onMessage(messageReceived);
connect();
}
//init ADS1115 //init ADS1115
if (!ADS.begin()) { if (!ADS.begin()) {
@ -70,8 +73,8 @@ void setup() {
Serial.println("Setup EC"); Serial.println("Setup EC");
ec_setup(); ec_setup();
//Serial.println("Setup Waterlevel"); Serial.println("Setup Waterlevel");
//waterlevel_setup(); waterlevel_setup();
Serial.println("Setup Temperature"); Serial.println("Setup Temperature");
temperature_setup(); temperature_setup();
@ -113,7 +116,7 @@ void loop() {
temperature_loop(loopmillis); temperature_loop(loopmillis);
//waterlevel_loop(loopmillis); waterlevel_loop(loopmillis);
flow_loop(loopmillis); flow_loop(loopmillis);
@ -145,16 +148,11 @@ void loop() {
if (loopmillis>last_check+2000) { //check values if (loopmillis>last_check+2000) { //check values
last_check=loopmillis; last_check=loopmillis;
if (tempCmean_air==DEVICE_DISCONNECTED_C || tempCmean_reservoir==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) { if (tempCmean_air==DEVICE_DISCONNECTED_C || tempCmean_reservoir==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) {
valueError=true; valueError=true;
} }
//if (waterlevel==WATERLEVEL_UNAVAILABLE) {
// valueError=true;
//}
if (sm_mean1==SM_DISCONNECTED || sm_mean2==SM_DISCONNECTED) { if (sm_mean1==SM_DISCONNECTED || sm_mean2==SM_DISCONNECTED) {
valueError=true; valueError=true;
} }
@ -165,8 +163,7 @@ void loop() {
digitalWrite(PIN_LED,valueError); digitalWrite(PIN_LED,valueError);
if (debug) { if (debug) {
Serial.println("_______________________"); Serial.println("_______________________");
@ -216,10 +213,10 @@ void loop() {
Serial.print(ec25); Serial.print(ec25);
Serial.println(); Serial.println();
/*Serial.print("Waterlevel distance,volume = "); Serial.print(waterlevel); Serial.print(","); Serial.print(watervolume); Serial.print("Waterlevel,Volume = ");
if (waterlevel_failcounter>0) { Serial.print(waterlevel); Serial.print(",");
Serial.print(" fails="); Serial.print(waterlevel_failcounter); Serial.print(watervolume);
}*/
Serial.println(); Serial.println();
@ -229,7 +226,7 @@ void loop() {
} }
if (mqtt_loop(loopmillis)) { if (mqtt && mqtt_loop(loopmillis)) {
if (sendallnext_flag) { if (sendallnext_flag) {
sendallnext_flag=false; sendallnext_flag=false;
enableTiming=false; enableTiming=false;