From 9f489c319f4894d78068fba2f1c3f55332d58105 Mon Sep 17 00:00:00 2001 From: Fisch Date: Tue, 20 Jun 2023 20:06:41 +0200 Subject: [PATCH] add vl6180 as waterlevel sensor --- include/waterlevel.h | 60 ++++++++++++++++++-------------------------- platformio.ini | 4 +-- src/main.cpp | 39 +++++++++++++--------------- 3 files changed, 44 insertions(+), 59 deletions(-) diff --git a/include/waterlevel.h b/include/waterlevel.h index 84aaa35..9c6c1a7 100644 --- a/include/waterlevel.h +++ b/include/waterlevel.h @@ -1,12 +1,17 @@ #ifndef _WATERLEVEL_H_ #define _WATERLEVEL_H_ +#include +#include -#include -#define HCSR04_PIN_ECHO 17 -#define HCSR04_PIN_TRIGGER 16 -#define HCSR04_TIMEOUT 5000 //default is 100000 (uS) -#define READINTERVAL_HCSR04 200 + +VL6180X sensor; +// To try different scaling factors, change the following define. +// Valid scaling factors are 1, 2, or 3. +#define SCALING 1 + + +#define READINTERVAL_WATERLEVEL 200 #define WATERLEVELMEAN_SIZE 32 #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 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 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 @@ -32,8 +34,13 @@ float waterlevel_heightToVolume(float distance); void waterlevel_setup() { - //HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO); - HCSR04.begin(HCSR04_PIN_TRIGGER, HCSR04_PIN_ECHO,HCSR04_TIMEOUT, HCSR04.eUltraSonicUnlock_t::unlockSkip); + Wire.begin(); + + sensor.init(); + sensor.configureDefault(); + sensor.setScaling(SCALING); + sensor.setTimeout(500); + for (uint16_t i=0;i=last_read_hcsr04+READINTERVAL_HCSR04) { - last_read_hcsr04=loopmillis; - float temperature=20.0; - if (tempCmean_air!=DEVICE_DISCONNECTED_C) { //sensor ok - temperature=tempCmean_air; - } + static unsigned long last_read_waterlevel; + if (loopmillis>=last_read_waterlevel+READINTERVAL_WATERLEVEL) { + last_read_waterlevel=loopmillis; + + + uint16_t distance=sensor.readRangeSingleMillimeters(); - double* distances = HCSR04.measureDistanceMm(temperature); - double distance=distances[0]; //Serial.print("Distance reading:"); Serial.println(distance); if (distance!=WATERLEVEL_UNAVAILABLE) { //successful waterlevelMean_array[waterlevelMean_array_pos]=distance; waterlevelMean_array_pos++; waterlevelMean_array_pos%=WATERLEVELMEAN_SIZE; - if (waterlevel_failcounter>0) { //reduce failcounter if sucessfull - waterlevel_failcounter--; - } - }else{ - 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); - }*/ - } } } diff --git a/platformio.ini b/platformio.ini index 09f7cc3..1203da0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,7 +17,7 @@ monitor_speed = 115200 lib_deps = https://github.com/milesburton/Arduino-Temperature-Control-Library/ - d03n3rfr1tz3/HC-SR04@^1.1.2 https://github.com/emilv/ArduinoSort/ robtillaart/ADS1X15@^0.3.9 - 256dpi/MQTT@^2.5.1 \ No newline at end of file + 256dpi/MQTT@^2.5.1 + https://github.com/pololu/vl6180x-arduino \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 51699eb..24c0d79 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -5,6 +5,7 @@ #include "wifi_functions.h" bool debug=true; //print Serial information +bool mqtt=false; @@ -26,7 +27,7 @@ ADS1115 ADS(0x48); // ######## Water Level -//#include "waterlevel.h" +#include "waterlevel.h" // ######## Flow Rate @@ -55,10 +56,12 @@ void setup() { pinMode(PIN_LED,OUTPUT); digitalWrite(PIN_LED,LOW); Serial.begin(115200); - WiFi.begin(ssid, pass); - client.begin(mqtt_host, net); - client.onMessage(messageReceived); - connect(); + if (mqtt) { + WiFi.begin(ssid, pass); + client.begin(mqtt_host, net); + client.onMessage(messageReceived); + connect(); + } //init ADS1115 if (!ADS.begin()) { @@ -70,8 +73,8 @@ void setup() { Serial.println("Setup EC"); ec_setup(); - //Serial.println("Setup Waterlevel"); - //waterlevel_setup(); + Serial.println("Setup Waterlevel"); + waterlevel_setup(); Serial.println("Setup Temperature"); temperature_setup(); @@ -113,7 +116,7 @@ void loop() { temperature_loop(loopmillis); - //waterlevel_loop(loopmillis); + waterlevel_loop(loopmillis); flow_loop(loopmillis); @@ -145,16 +148,11 @@ void loop() { if (loopmillis>last_check+2000) { //check values last_check=loopmillis; - - - if (tempCmean_air==DEVICE_DISCONNECTED_C || tempCmean_reservoir==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) { valueError=true; } - //if (waterlevel==WATERLEVEL_UNAVAILABLE) { - // valueError=true; - //} + if (sm_mean1==SM_DISCONNECTED || sm_mean2==SM_DISCONNECTED) { valueError=true; } @@ -165,8 +163,7 @@ void loop() { digitalWrite(PIN_LED,valueError); - - + if (debug) { Serial.println("_______________________"); @@ -216,10 +213,10 @@ void loop() { Serial.print(ec25); Serial.println(); - /*Serial.print("Waterlevel distance,volume = "); Serial.print(waterlevel); Serial.print(","); Serial.print(watervolume); - if (waterlevel_failcounter>0) { - Serial.print(" fails="); Serial.print(waterlevel_failcounter); - }*/ + Serial.print("Waterlevel,Volume = "); + Serial.print(waterlevel); Serial.print(","); + Serial.print(watervolume); + Serial.println(); @@ -229,7 +226,7 @@ void loop() { } - if (mqtt_loop(loopmillis)) { + if (mqtt && mqtt_loop(loopmillis)) { if (sendallnext_flag) { sendallnext_flag=false; enableTiming=false;