Compare commits
No commits in common. "5958942d9f2937ae7aa60b55d1e4ed289a45ba8e" and "ec6ad40a340fc4592d337843b4077403bafece2b" have entirely different histories.
5958942d9f
...
ec6ad40a34
|
@ -7,4 +7,3 @@
|
||||||
.piolibdeps
|
.piolibdeps
|
||||||
.clang_complete
|
.clang_complete
|
||||||
.gcc-flags.json
|
.gcc-flags.json
|
||||||
messung/*
|
|
||||||
|
|
|
@ -3,6 +3,7 @@
|
||||||
|
|
||||||
#include <Wire.h>
|
#include <Wire.h>
|
||||||
#include <VL53L0X.h> //pololu/VL53L0X@^1.3.1
|
#include <VL53L0X.h> //pololu/VL53L0X@^1.3.1
|
||||||
|
#include <VL6180X.h> //https://github.com/pololu/vl6180x-arduino
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -20,7 +21,7 @@
|
||||||
|
|
||||||
// +++++++++++++++ VL53L0X +++++++++++++++
|
// +++++++++++++++ VL53L0X +++++++++++++++
|
||||||
VL53L0X sensorA;
|
VL53L0X sensorA;
|
||||||
#define PIN_VL53L0X_XSHUT_A 19
|
#define PIN_VL53L0X_XSHUT 19
|
||||||
// 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
|
||||||
|
@ -55,25 +56,11 @@ float waterlevelA_calib_reservoirArea=20*20*3.1416; //area in cm^2. barrel diame
|
||||||
|
|
||||||
uint16_t distanceA_unsuccessful_count=0;
|
uint16_t distanceA_unsuccessful_count=0;
|
||||||
|
|
||||||
// +++++++++++++++ VL53L0X +++++++++++++++
|
// +++++++++++++++ VL6180X +++++++++++++++
|
||||||
VL53L0X sensorB;
|
VL6180X sensorB;
|
||||||
|
// To try different scaling factors, change the following define.
|
||||||
#define PIN_VL53L0X_XSHUT_A 19
|
// Valid scaling factors are 1, 2, or 3.
|
||||||
// Uncomment this line to use long range mode. This
|
#define SCALING 1
|
||||||
// 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
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -85,8 +72,8 @@ float watervolumeB=WATERLEVEL_UNAVAILABLE; //calculated Volume in Reservoir
|
||||||
|
|
||||||
|
|
||||||
//Calibration
|
//Calibration
|
||||||
float waterlevelB_calib_offset=532.78; //c
|
float waterlevelB_calib_offset=260.86; //c
|
||||||
float waterlevelB_calib_factor=-1.179; //m
|
float waterlevelB_calib_factor=-1.107; //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
|
||||||
|
@ -95,6 +82,7 @@ uint16_t distanceB_unsuccessful_count=0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
float waterlevelA_heightToVolume(float distance);
|
float waterlevelA_heightToVolume(float distance);
|
||||||
float waterlevelB_heightToVolume(float distance);
|
float waterlevelB_heightToVolume(float distance);
|
||||||
|
|
||||||
|
@ -105,8 +93,8 @@ mqttValueTiming timing_waterlevelB;
|
||||||
void waterlevel_setup() {
|
void waterlevel_setup() {
|
||||||
|
|
||||||
|
|
||||||
pinMode(PIN_VL53L0X_XSHUT_A, OUTPUT);
|
pinMode(PIN_VL53L0X_XSHUT, OUTPUT);
|
||||||
digitalWrite(PIN_VL53L0X_XSHUT_A, LOW); //pull to GND
|
digitalWrite(PIN_VL53L0X_XSHUT, LOW); //pull to GND
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -169,46 +157,33 @@ void waterlevel_setup() {
|
||||||
Serial.println(Wire.getClock());
|
Serial.println(Wire.getClock());
|
||||||
|
|
||||||
|
|
||||||
//Initialize SensorB first
|
|
||||||
sensorB.setTimeout(1000);
|
sensorB.setTimeout(1000);
|
||||||
if (!sensorB.init())
|
Serial.println("init A");
|
||||||
{
|
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");
|
Serial.println("set addr 0x2A");
|
||||||
sensorB.setAddress(0x2A); //change address
|
sensorB.setAddress(0x2A); //change address
|
||||||
Serial.println("conf Default");
|
Serial.println("conf Default");
|
||||||
|
sensorB.configureDefault();
|
||||||
|
Serial.println("set scaling");
|
||||||
|
sensorB.setScaling(SCALING);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#if defined LONG_RANGE
|
/*
|
||||||
// lower the return signal rate limit (default is 0.25 MCPS)
|
Serial.println("Connect second sensor now!");
|
||||||
sensorB.setSignalRateLimit(0.1);
|
delay(1000);
|
||||||
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
|
Serial.println("waiting 5s");
|
||||||
sensorB.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
|
delay(5000);
|
||||||
sensorB.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);
|
Serial.println("done waiting");*/
|
||||||
#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
|
// 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
|
// 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.
|
// not level shifted.) Then wait a bit for the sensor to start up.
|
||||||
pinMode(PIN_VL53L0X_XSHUT_A, INPUT);
|
pinMode(PIN_VL53L0X_XSHUT, INPUT);
|
||||||
delay(50);
|
delay(50);
|
||||||
|
|
||||||
//Initialize Sensor A after SensorB's address was changed
|
|
||||||
sensorA.setTimeout(1000);
|
sensorA.setTimeout(1000);
|
||||||
if (!sensorA.init())
|
if (!sensorA.init())
|
||||||
{
|
{
|
||||||
|
@ -283,7 +258,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);
|
||||||
|
@ -330,7 +305,7 @@ 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();
|
//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);
|
||||||
|
|
|
@ -76,15 +76,6 @@ void messageReceived(String &topic, String &payload) {
|
||||||
force_ec_measurement=true;
|
force_ec_measurement=true;
|
||||||
Serial.println("Forced EC Measurement");
|
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) {
|
bool mqtt_loop(unsigned long loopmillis) {
|
||||||
|
|
45
src/main.cpp
45
src/main.cpp
|
@ -3,14 +3,11 @@
|
||||||
|
|
||||||
/*TODO
|
/*TODO
|
||||||
mqtt function_: valueerror quittieren
|
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"
|
#include "wifi_functions.h"
|
||||||
|
|
||||||
bool debug=false; //print Serial information
|
bool debug=false; //print Serial information
|
||||||
|
@ -21,8 +18,6 @@ sudo stty -F /dev/ttyUSB0 115200
|
||||||
cat /dev/ttyUSB0 | tee received.txt
|
cat /dev/ttyUSB0 | tee received.txt
|
||||||
*/
|
*/
|
||||||
|
|
||||||
bool valuesStabilized=false; //gets set true when values are stable (avaeraging arrays filled)
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,6 +51,9 @@ ADS1115 ADS(0x48);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool valueError=false;
|
||||||
|
|
||||||
|
unsigned long last_check=0;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -152,20 +150,14 @@ void loop() {
|
||||||
if (!eccalibrationoutput && !digitalRead(PIN_BUTTON)) {
|
if (!eccalibrationoutput && !digitalRead(PIN_BUTTON)) {
|
||||||
valueError=false;
|
valueError=false;
|
||||||
Serial.println("Reset ValueError flag by user");
|
Serial.println("Reset ValueError flag by user");
|
||||||
digitalWrite(PIN_LED,valueError); //set led before delay to blink if error persists
|
digitalWrite(PIN_LED,valueError);
|
||||||
delay(100);
|
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)) {
|
if (!isValueArrayOK(ec_calib_array,EC_CALIB_ARRAY_SIZE,EC_ADC_UNAVAILABLE)) {
|
||||||
for (uint8_t blink=0;blink<5;blink++) {
|
for (uint8_t blink=0;blink<5;blink++) {
|
||||||
digitalWrite(PIN_LED,HIGH);
|
digitalWrite(PIN_LED,HIGH);
|
||||||
|
@ -182,7 +174,7 @@ void loop() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if (eccalibrationoutput && ec_flag_measurement_available && getReading) { //Calibration UI
|
if (eccalibrationoutput && ec_flag_measurement_available && getReading) {
|
||||||
|
|
||||||
ec_flag_measurement_available=false;
|
ec_flag_measurement_available=false;
|
||||||
getReading=false;
|
getReading=false;
|
||||||
|
@ -202,12 +194,8 @@ void loop() {
|
||||||
|
|
||||||
last_check=loopmillis;
|
last_check=loopmillis;
|
||||||
|
|
||||||
|
|
||||||
bool _noErrorsDuringLoop=true;
|
|
||||||
|
|
||||||
if (tempCmean_reservoir_a==DEVICE_DISCONNECTED_C || tempCmean_reservoir_b==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) {
|
if (tempCmean_reservoir_a==DEVICE_DISCONNECTED_C || tempCmean_reservoir_b==DEVICE_DISCONNECTED_C || tempCmean_case==DEVICE_DISCONNECTED_C) {
|
||||||
valueError=true;
|
valueError=true;
|
||||||
_noErrorsDuringLoop=false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -216,7 +204,6 @@ void loop() {
|
||||||
}*/
|
}*/
|
||||||
if (ec_A==EC_UNAVAILABLE || ec_B==EC_UNAVAILABLE){
|
if (ec_A==EC_UNAVAILABLE || ec_B==EC_UNAVAILABLE){
|
||||||
valueError=true;
|
valueError=true;
|
||||||
_noErrorsDuringLoop=false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -226,11 +213,9 @@ void loop() {
|
||||||
|
|
||||||
if (distanceA_unsuccessful_count>20) {
|
if (distanceA_unsuccessful_count>20) {
|
||||||
valueError=true;
|
valueError=true;
|
||||||
_noErrorsDuringLoop=false;
|
|
||||||
}
|
}
|
||||||
if (distanceB_unsuccessful_count>20) {
|
if (distanceB_unsuccessful_count>20) {
|
||||||
valueError=true;
|
valueError=true;
|
||||||
_noErrorsDuringLoop=false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -307,11 +292,8 @@ 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");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -346,24 +328,21 @@ void loop() {
|
||||||
|
|
||||||
|
|
||||||
static float last_flow_a=0;
|
static float last_flow_a=0;
|
||||||
if (valuesStabilized){
|
|
||||||
if (flow_a==0.0 && last_flow_a!=flow_a) {
|
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
|
publishValueTimedOverride("nft/flow",flow_a,2,timing_flow_a,loopmillis); //publish without waiting if flow is 0
|
||||||
}else{
|
}else{
|
||||||
publishValueTimed("nft/flow",flow_a,2,timing_flow_a,loopmillis);
|
publishValueTimed("nft/flow",flow_a,2,timing_flow_a,loopmillis);
|
||||||
}
|
}
|
||||||
last_flow_a=flow_a;
|
last_flow_a=flow_a;
|
||||||
}
|
|
||||||
|
|
||||||
static float last_flow_b=0;
|
static float last_flow_b=0;
|
||||||
if (valuesStabilized){
|
|
||||||
if (flow_b==0.0 && last_flow_b!=flow_b) {
|
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
|
publishValueTimedOverride("db/flow",flow_b,2,timing_flow_b,loopmillis); //publish without waiting if flow is 0
|
||||||
}else{
|
}else{
|
||||||
publishValueTimed("db/flow",flow_b,2,timing_flow_b,loopmillis);
|
publishValueTimed("db/flow",flow_b,2,timing_flow_b,loopmillis);
|
||||||
}
|
}
|
||||||
last_flow_b=flow_b;
|
last_flow_b=flow_b;
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue