From 1e87d26088aa8aa60544ca62bc070fdd89e998de Mon Sep 17 00:00:00 2001 From: Fisch Date: Wed, 13 Oct 2021 14:56:09 +0200 Subject: [PATCH] add vl53l1x sensor --- platformio.ini | 9 ++++-- src/main.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/platformio.ini b/platformio.ini index 07819f5..9b497d9 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,8 +17,8 @@ #data_dir=data_sensoresp2 #data_dir=data_sensoresp3 #data_dir=data_sensoresp4 -#data_dir=data_sensoresp5 -data_dir=data_sensoresp3dprinter +data_dir=data_sensoresp5 +#data_dir=data_sensoresp3dprinter #Outdoor @@ -237,12 +237,17 @@ build_flags = -D dataBH1750_minchange=10.0 -D dataBH1750_senddelaymax=1000*60*2 + -D SENSOR_VL53L1X + -D dataBH1750_minchange=10.0 + -D dataBH1750_senddelaymax=1000*60*2 + lib_deps = adafruit/DHT sensor library@1.3.10 claws/BH1750@1.1.4 ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error Homie@3.0.0 + pololu/VL53L1X @ ^1.3.0 diff --git a/src/main.cpp b/src/main.cpp index 4813988..b5570a5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -228,6 +228,22 @@ struct sensordata #endif #endif +#ifdef SENSOR_VL53L1X + #ifndef WIRE_H + #include + #define WIRE_H + #endif + #include + VL53L1X vl53l1x; + bool vl53l1xinit_ok=false; + struct sensordata dataVL53L1X_range; + struct sensordata dataVL53L1X_peak_signal_count_rate; + struct sensordata dataVL53L1X_ambient_count_rate; + + uint16_t value_vl53l1x_range; + unsigned long lastread_vl53l1x=0; +#endif + #ifdef SENSOR_ANEMOMETER //uses ATS177 Latched hall sensor for rotation sensing sensordata dataAnemometer; @@ -462,6 +478,23 @@ void setup() { #endif #endif + #ifdef SENSOR_VL53L1X + Serial.println("initializing vl53l1x"); + vl53l1x.setTimeout(500); + if (!vl53l1x.init()) { + Serial.println("No vl53l1x found!"); + }else{ + vl53l1xinit_ok=true; + vl53l1x.setDistanceMode(VL53L1X::Long); + vl53l1x.setMeasurementTimingBudget(50000); + vl53l1x.startContinuous(50); //This period should be at least as long as the timing budget. + } + #ifdef dataVL53L1X_range_minchange + dataVL53L1X_range.minchange=dataVL53L1X_range_minchange; + #endif + + #endif + #ifdef SENSOR_ANEMOMETER pinMode(ANEMOMETERPIN,INPUT_PULLUP); attachInterrupt(digitalPinToInterrupt(ANEMOMETERPIN),interrupt_anemometer,CHANGE); //anemometer interrupt @@ -574,6 +607,10 @@ void setup() { sensorNode.advertise("colortemp"); #endif + #ifdef SENSOR_VL53L1X + sensorNode.advertise("range"); + #endif + #ifdef SENSOR_ANEMOMETER sensorNode.advertise("windspeed"); #endif @@ -1160,6 +1197,54 @@ void loop_TCS34725_colortemp() } #endif + + +#ifdef SENSOR_VL53L1X +void loop_VL53L1X_range() +{ + sensordata &d=dataVL53L1X_range; + + bool _changed=false; + if (millis() >= (d.lastreadtime+d.readdelay)) { + if (millis() >= (lastread_vl53l1x+d.readdelay)) { //avoid reading sensor twice in a short time + //tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c); + vl53l1x.read(); + lastread_vl53l1x=millis(); + } + + value_vl53l1x_range=vl53l1x.ranging_data.range_mm; + Serial.print("range: "); + Serial.print(vl53l1x.ranging_data.range_mm); + Serial.print("\tstatus: "); + Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status)); + Serial.print("\tpeak signal: "); + Serial.print(vl53l1x.ranging_data.peak_signal_count_rate_MCPS); + Serial.print("\tambient: "); + Serial.print(vl53l1x.ranging_data.ambient_count_rate_MCPS); + + if (abs((int)d.lastsentvalue-value_vl53l1x_range)>=d.minchange){ //int abs + _changed=true; + } + d.lastreadtime=millis(); + } + + if (_changed || millis() >= (d.lastsent+d.senddelaymax)) { + Serial.print("Sending VL53L1X range. reason="); + if (_changed) Serial.println("change"); else Serial.println("time"); + checkESPStatus(); + + Homie.getLogger() << "range vl53l1x " << ": " << value_vl53l1x_range << endl; + + sensorNode.setProperty("range").send(String(value_vl53l1x_range)); + + + d.lastsentvalue=value_vl53l1x_range; + + d.lastsent=millis(); + } +} +#endif + #ifdef SENSOR_ANEMOMETER void loop_anemometer() {