Compare commits

...

3 Commits

Author SHA1 Message Date
interfisch 275c641ec8 move vl53l1x to class 2021-11-07 22:31:24 +01:00
interfisch c5bad1cc44 initialize sensor objects in constructor 2021-11-07 22:05:57 +01:00
interfisch 4dbc5b186c move tcs34725 to class 2021-11-07 21:56:42 +01:00
12 changed files with 389 additions and 252 deletions

View File

@ -5,7 +5,7 @@
Sensor_BH1750::Sensor_BH1750()
{
BH1750 bh1750(0x23); //0x23 if addr connected to ground (=pin open), 0x5c if addr pulled high
bh1750 = new BH1750(0x23); //0x23 if addr connected to ground (=pin open), 0x5c if addr pulled high
}
void Sensor_BH1750::init() //Things to be done during setup()

View File

@ -5,7 +5,7 @@
Sensor_BMP180::Sensor_BMP180()
{
Adafruit_BMP085 bmp180;
bmp180 = new Adafruit_BMP085();
}
void Sensor_BMP180::init() //Things to be done during setup()

View File

@ -6,7 +6,7 @@
Sensor_HTU21D::Sensor_HTU21D()
{
Adafruit_HTU21DF htu;
htu = new Adafruit_HTU21DF();
}
void Sensor_HTU21D::init() //Things to be done during setup()

View File

@ -7,6 +7,9 @@ Sensor_MHZ19B::Sensor_MHZ19B(int prx,int ptx)
pin_rx=prx;
pin_tx=ptx;
mhz19 = new MHZ19();
mhz19_swSerial = new SoftwareSerial();
/*
* MHZ19 Library: https://platformio.org/lib/show/1620/SevSegSPI
* Software Serial Library: https://platformio.org/lib/show/168/EspSoftwareSerial

View File

@ -25,7 +25,6 @@ private:
int mhz19_readValue_reimplemented(Stream *_streamRef, MHZ19 *_mhz19Ref);
byte mhz19_getCheckSum(byte* packet);
bool mhz19_ready;
MHZ19 *mhz19;

View File

@ -10,6 +10,8 @@ Sensor_SDS018::Sensor_SDS018(int prx,int ptx)
{
pin_rx=prx;
pin_tx=ptx;
sds018_swSerial = new SoftwareSerial();
}
void Sensor_SDS018::init() //Things to be done during setup()

159
include/sensor_tcs34725.cpp Normal file
View File

@ -0,0 +1,159 @@
#include "sensor_tcs34725.h"
//#include "Adafruit_TCS34725.h"
//Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); //initializer from standart class
//Connect SCL to D1, SDA to D2, GND and 3v3
//Maximum measurable light is around 20k Lux. (direct sunlight is easily above 20k Lux)
Sensor_TCS34725::Sensor_TCS34725()
{
tcs = new tcs34725();
#ifndef TCS34725_MINLUXFORCT
#define TCS34725_MINLUXFORCT 30 //send only colortemperature values if lux is at least this high
#endif
#ifndef TCS34725_LUXFACTOR
#define TCS34725_LUXFACTOR 1
#endif
}
void Sensor_TCS34725::init() //Things to be done during setup()
{
Serial.println("initializing tcs34725");
if (!tcs->begin()) {
Serial.println("No TCS34725 found!");
}else{
init_ok=true;
}
}
//Also called during setup()
void Sensor_TCS34725::setSettings_Lux(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data_lux.minchange=minchange;
data_lux.senddelaymax=senddelaymax;
data_lux.readdelay=readdelay;
}
//Also called during setup()
void Sensor_TCS34725::setSettings_Colortemp(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data_colortemp.minchange=minchange;
data_colortemp.senddelaymax=senddelaymax;
data_colortemp.readdelay=readdelay;
}
//Called during setup
void Sensor_TCS34725::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode->advertise("light_tcs");
#else
sensorNode->advertise("light");
#endif
sensorNode->advertise("colortemp");
}
void Sensor_TCS34725::sensorloop()
{
loop_lux();
loop_colortemp();
}
void Sensor_TCS34725::loop_lux()
{
sensordata &d=data_lux;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
tcs->getData();
lastread_tcs34725=millis();
if (tcs->isSaturated){
Serial.println("Warning: tcs34725 is saturated");
#ifdef STATUSNODE
sensorNode->setProperty("status").send("TCS34725 is saturated");
#endif
}
}
//value_tcs_lux = tcs.calculateLux(value_tcs_r, value_tcs_g, value_tcs_b);
uint16_t _value_tcs_lux = tcs->lux*TCS34725_LUXFACTOR;
if (!tcs->isSaturated && _value_tcs_lux<65535){ //sometimes false high reading accur around 65535 sometimes less. with isSaturated check only 65535 values appeared.
d.value = _value_tcs_lux;
}
if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending TCS Lux. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
Homie.getLogger() << "light tcs " << ": " << d.value << endl;
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode.setProperty("light_tcs").send(String(d.value));
#else
sensorNode->setProperty("light").send(String(d.value));
#endif
d.lastsentvalue=d.value;
d.lastsent=millis();
}
}
void Sensor_TCS34725::loop_colortemp()
{
sensordata &d=data_colortemp;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
tcs->getData();
lastread_tcs34725=millis();
if (tcs->isSaturated){
Serial.println("Warning: tcs34725 is saturated");
}
}
// colorTemp = tcs.calculateColorTemperature(r, g, b);
//value_colortemp = tcs.calculateColorTemperature_dn40(value_tcs_r, value_tcs_g, value_tcs_b, value_tcs_c);
if (!tcs->isSaturated){
d.value = tcs->ct; //with agc
}
if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending TCS colortemp. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
Homie.getLogger() << "colortemp tcs " << ": " << d.value << endl;
if (tcs->lux>=TCS34725_MINLUXFORCT) {
if (d.value > 1) {
sensorNode->setProperty("colortemp").send(String(d.value));
}else{
Homie.getLogger() << "didn't send tcs ct because value is too low" << endl;
sensorNode->setProperty("colortemp").send(String(-1));
}
}else{
Homie.getLogger() << "didn't send tcs ct because light too low: " << tcs->lux << "lux" << endl;
sensorNode->setProperty("colortemp").send(String(-1));
}
d.lastsentvalue=d.value;
d.lastsent=millis();
}
}

41
include/sensor_tcs34725.h Normal file
View File

@ -0,0 +1,41 @@
#ifndef SENSOR_TCS34725_H
#define SENSOR_TCS34725_H
#include "sensordata.h"
#include <Homie.h>
#include "tcs34725_agc.h" //class code from example https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/tcs34725autorange/tcs34725autorange.ino
class Sensor_TCS34725
{
private:
tcs34725 *tcs; //wrapper class with agc
HomieNode *sensorNode; //reference to HomieNode
struct sensordata data_lux; //struct values are changed in setup()
struct sensordata data_colortemp; //struct values are changed in setup()
uint16_t value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c;
unsigned long lastread_tcs34725=0;
bool init_ok;
public:
Sensor_TCS34725();
void loop_lux();
void loop_colortemp();
void init();
void setSettings_Lux(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void setSettings_Colortemp(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

View File

@ -0,0 +1,97 @@
//Connect SCL to D1, SDA to D2, GND and 3v3
#include "sensor_vl53l1x.h"
Sensor_VL53L1X::Sensor_VL53L1X()
{
vl53l1x = new VL53L1X();
}
void Sensor_VL53L1X::init() //Things to be done during setup()
{
Serial.println("initializing vl53l1x");
vl53l1x->setTimeout(500);
if (!vl53l1x->init()) {
Serial.println("No vl53l1x found!");
}else{
init_ok=true;
vl53l1x->setDistanceMode(VL53L1X::Long);
vl53l1x->setMeasurementTimingBudget(50000);
vl53l1x->startContinuous(1000); //This period should be at least as long as the timing budget.
}
}
//Also called during setup()
void Sensor_VL53L1X::setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay)
{
data.minchange=minchange;
data.senddelaymax=senddelaymax;
data.readdelay=readdelay;
}
//Called during setup
void Sensor_VL53L1X::advertise(HomieNode& p_sensorNode)
{
sensorNode = &p_sensorNode;
sensorNode->advertise("tofstatus");
sensorNode->advertise("tofrange");
sensorNode->advertise("tofpeaksignal");
sensorNode->advertise("tofambient");
}
void Sensor_VL53L1X::sensorloop()
{
if (init_ok) {
sensordata &d=data;
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();
}
d.value=vl53l1x->ranging_data.range_mm;
/* for debugging
Serial.print("range: ");
Serial.print(vl53l1x.ranging_data.range_mm);
Serial.print("\tstatus: ");
Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
Serial.print("\tstatus=");
Serial.print(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);
Serial.println();
*/
if (abs((int)d.lastsentvalue-d.value)>=d.minchange){ //int abs
_changed=true;
}
if (lastsentvalue_vl53l1x_status!=vl53l1x->ranging_data.range_status) { //sensor status changed
_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");
Homie.getLogger() << "range vl53l1x " << ": " << d.value << endl;
sensorNode->setProperty("tofstatus").send(VL53L1X::rangeStatusToString(vl53l1x->ranging_data.range_status));
sensorNode->setProperty("tofrange").send(String(d.value));
sensorNode->setProperty("tofpeaksignal").send(String(vl53l1x->ranging_data.peak_signal_count_rate_MCPS));
sensorNode->setProperty("tofambient").send(String(vl53l1x->ranging_data.ambient_count_rate_MCPS));
d.lastsentvalue=d.value;
lastsentvalue_vl53l1x_status=vl53l1x->ranging_data.range_status;
d.lastsent=millis();
}
}
}

36
include/sensor_vl53l1x.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef SENSOR_VL53L1X_H
#define SENSOR_VL53L1X_H
#include "sensordata.h"
#include <Homie.h>
#include <Wire.h>
#include <VL53L1X.h>
class Sensor_VL53L1X
{
private:
VL53L1X *vl53l1x;
HomieNode *sensorNode; //reference to HomieNode
struct sensordata data; //struct values are changed in setup()
bool init_ok;
unsigned long lastread_vl53l1x=0;
VL53L1X::RangeStatus lastsentvalue_vl53l1x_status;
public:
Sensor_VL53L1X();
void init();
void setSettings(float minchange, unsigned long senddelaymax, unsigned long readdelay);
void advertise(HomieNode& p_sensorNode);
void sensorloop();
};
#endif

View File

@ -37,10 +37,10 @@ build_flags =
-D dataBMP180_pressure_minchange=0.5
-D SENSOR_TCS34725
-D dataTCS34725_lux_minchange=500
-D dataTCS34725_lux_senddelaymax=1000*60*1
-D dataTCS34725_colortemp_minchange=100
-D TCS34725_LUXFACTOR=0.3 #measured with luxmeter. with half tennis ball was 1.7ev less. 1/2^1.7=0,3078
-D SENSOR_TCS34725_LUX_minchange=500
-D SENSOR_TCS34725_LUX_senddelaymax=1000*60*1
-D SENSOR_TCS34725_COLORTEMP_minchange=100
-D SENSOR_TCS34725_LUXFACTOR=0.3 #measured with luxmeter. with half tennis ball was 1.7ev less. 1/2^1.7=0,3078
-D SENSOR_HS1101
-D SENSOR_HS1101_PIN=D5
@ -258,9 +258,9 @@ build_flags =
-D dataBH1750_senddelaymax=1000*60*2
-D SENSOR_VL53L1X
-D dataVL53L1X_minchange=100
-D dataVL53L1X_senddelaymax=1000*30
-D dataVL53L1X_readdelay=1000
-D SENSOR_VL53L1X_minchange=100
-D SENSOR_VL53L1X_senddelaymax=1000*30
-D SENSOR_VL53L1X_readdelay=1000
@ -284,10 +284,10 @@ monitor_speed = 115200
build_flags =
-D SENSOR_HTU21D
-D dataHTU21D_temperature_minchange=0.2
-D dataHTU21D_temperature_senddelaymax=1000*60*20
-D dataHTU21D_temperature_minchange=0.1
-D dataHTU21D_temperature_senddelaymax=1000*60*60
-D dataHTU21D_humidity_minchange=1.0
-D dataHTU21D_humidity_senddelaymax=1000*60*30
-D dataHTU21D_humidity_senddelaymax=1000*60*60

View File

@ -228,37 +228,40 @@
#endif
#ifdef SENSOR_TCS34725
//#include "Adafruit_TCS34725.h"
#include "tcs34725_agc.h" //class code from example https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/tcs34725autorange/tcs34725autorange.ino
//Connect SCL to D1, SDA to D2, GND and 3v3
//Maximum measurable light is around 20k Lux. (direct sunlight is easily above 20k Lux)
//Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); //initializer from standart class
tcs34725 tcs; //wrapper class with agc
bool tcs34725init_ok=false;
struct sensordata dataTCS34725_lux;
struct sensordata dataTCS34725_colortemp;
uint16_t value_colortemp, value_tcs_lux, value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c;
unsigned long lastread_tcs34725=0;
#define TCS34725_MINLUXFORCT 30 //send only colortemperature values if lux is at least this high
#ifndef TCS34725_LUXFACTOR
#define TCS34725_LUXFACTOR 1
#include "sensor_tcs34725.cpp"
Sensor_TCS34725 sensor_tcs34725;
#ifndef SENSOR_TCS34725_LUX_minchange
#define SENSOR_TCS34725_LUX_minchange 500
#endif
#ifndef SENSOR_TCS34725_LUX_senddelaymax
#define SENSOR_TCS34725_LUX_senddelaymax 1000*60*5
#endif
#ifndef SENSOR_TCS34725_LUX_readdelay
#define SENSOR_TCS34725_LUX_readdelay 1000*10
#endif
#ifndef SENSOR_TCS34725_COLORTEMP_minchange
#define SENSOR_TCS34725_COLORTEMP_minchange 100
#endif
#ifndef SENSOR_TCS34725_COLORTEMP_senddelaymax
#define SENSOR_TCS34725_COLORTEMP_senddelaymax 1000*60*5
#endif
#ifndef SENSOR_TCS34725_COLORTEMP_readdelay
#define SENSOR_TCS34725_COLORTEMP_readdelay 1000*10
#endif
#endif
#ifdef SENSOR_VL53L1X
#ifndef WIRE_H
#include <Wire.h>
#define WIRE_H
#include "sensor_vl53l1x.cpp"
Sensor_VL53L1X sensor_vl53l1x;
#ifndef SENSOR_VL53L1X_minchange
#define SENSOR_VL53L1X_minchange 100
#endif
#ifndef SENSOR_VL53L1X_senddelaymax
#define SENSOR_VL53L1X_senddelaymax 1000*30
#endif
#ifndef SENSOR_VL53L1X_readdelay
#define SENSOR_VL53L1X_readdelay 1000
#endif
#include <VL53L1X.h>
VL53L1X vl53l1x;
bool vl53l1xinit_ok=false;
struct sensordata dataVL53L1X;
uint16_t value_vl53l1x_range;
unsigned long lastread_vl53l1x=0;
VL53L1X::RangeStatus lastsentvalue_vl53l1x_status;
#endif
#ifdef SENSOR_ANEMOMETER
@ -387,44 +390,14 @@ void setup() {
#endif
#ifdef SENSOR_TCS34725
Serial.println("initializing tcs34725");
if (!tcs.begin()) {
Serial.println("No TCS34725 found!");
}else{
tcs34725init_ok=true;
}
#ifdef dataTCS34725_lux_minchange
dataTCS34725_lux.minchange=dataTCS34725_lux_minchange;
#endif
#ifdef dataTCS34725_lux_senddelaymax
dataTCS34725_lux.senddelaymax=dataTCS34725_lux_senddelaymax;
#endif
#ifdef dataTCS34725_colortemp_minchange
dataTCS34725_colortemp.minchange=dataTCS34725_colortemp_minchange;
#endif
sensor_tcs34725.init();
sensor_tcs34725.setSettings_Lux(SENSOR_TCS34725_LUX_minchange,SENSOR_TCS34725_LUX_senddelaymax,SENSOR_TCS34725_LUX_readdelay);
sensor_tcs34725.setSettings_Colortemp(SENSOR_TCS34725_COLORTEMP_minchange,SENSOR_TCS34725_COLORTEMP_senddelaymax,SENSOR_TCS34725_COLORTEMP_readdelay);
#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(1000); //This period should be at least as long as the timing budget.
}
#ifdef dataVL53L1X_minchange
dataVL53L1X.minchange=dataVL53L1X_minchange;
#endif
#ifdef dataVL53L1X_senddelaymax
dataVL53L1X.senddelaymax=dataVL53L1X_senddelaymax;
#endif
#ifdef dataVL53L1X_readdelay
dataVL53L1X.readdelay=dataVL53L1X_readdelay;
#endif
sensor_vl53l1x.init();
sensor_vl53l1x.setSettings(SENSOR_VL53L1X_minchange,SENSOR_VL53L1X_senddelaymax,SENSOR_VL53L1X_readdelay);
#endif
#ifdef SENSOR_ANEMOMETER
@ -512,19 +485,11 @@ void setup() {
#endif
#ifdef SENSOR_TCS34725
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode.advertise("light_tcs");
#else
sensorNode.advertise("light");
#endif
sensorNode.advertise("colortemp");
sensor_tcs34725.advertise(sensorNode);
#endif
#ifdef SENSOR_VL53L1X
sensorNode.advertise("tofstatus");
sensorNode.advertise("tofrange");
sensorNode.advertise("tofpeaksignal");
sensorNode.advertise("tofambient");
sensor_vl53l1x.advertise(sensorNode);
#endif
#ifdef SENSOR_ANEMOMETER
@ -546,166 +511,6 @@ void loop() {
Homie.loop();
}
#ifdef SENSOR_TCS34725
void loop_TCS34725_lux()
{
sensordata &d=dataTCS34725_lux;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
tcs.getData();
lastread_tcs34725=millis();
if (tcs.isSaturated){
Serial.println("Warning: tcs34725 is saturated");
#ifdef STATUSNODE
sensorNode.setProperty("status").send("TCS34725 is saturated");
#endif
}
}
//value_tcs_lux = tcs.calculateLux(value_tcs_r, value_tcs_g, value_tcs_b);
uint16_t _value_tcs_lux = tcs.lux*TCS34725_LUXFACTOR;
if (!tcs.isSaturated && _value_tcs_lux<65535){ //sometimes false high reading accur around 65535 sometimes less. with isSaturated check only 65535 values appeared.
value_tcs_lux = _value_tcs_lux;
}
if (abs((int)d.lastsentvalue-value_tcs_lux)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending TCS Lux. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "light tcs " << ": " << value_tcs_lux << endl;
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode.setProperty("light_tcs").send(String(value_tcs_lux));
#else
sensorNode.setProperty("light").send(String(value_tcs_lux));
#endif
d.lastsentvalue=value_tcs_lux;
d.lastsent=millis();
}
}
void loop_TCS34725_colortemp()
{
sensordata &d=dataTCS34725_colortemp;
bool _changed=false;
if (millis() >= (d.lastreadtime+d.readdelay)) {
if (millis() >= (lastread_tcs34725+d.readdelay)) { //avoid reading sensor twice in a short time
//tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
tcs.getData();
lastread_tcs34725=millis();
if (tcs.isSaturated){
Serial.println("Warning: tcs34725 is saturated");
}
}
// colorTemp = tcs.calculateColorTemperature(r, g, b);
//value_colortemp = tcs.calculateColorTemperature_dn40(value_tcs_r, value_tcs_g, value_tcs_b, value_tcs_c);
if (!tcs.isSaturated){
value_colortemp = tcs.ct; //with agc
}
if (abs((int)d.lastsentvalue-value_colortemp)>=d.minchange){ //int abs
_changed=true;
}
d.lastreadtime=millis();
}
if (_changed || millis() >= (d.lastsent+d.senddelaymax)) {
Serial.print("Sending TCS colortemp. reason=");
if (_changed) Serial.println("change"); else Serial.println("time");
checkESPStatus();
Homie.getLogger() << "colortemp tcs " << ": " << value_colortemp << endl;
if (tcs.lux>=TCS34725_MINLUXFORCT) {
if (value_colortemp > 1) {
sensorNode.setProperty("colortemp").send(String(value_colortemp));
}else{
Homie.getLogger() << "didn't send tcs ct because value is too low" << endl;
sensorNode.setProperty("colortemp").send(String(-1));
}
}else{
Homie.getLogger() << "didn't send tcs ct because light too low: " << tcs.lux << "lux" << endl;
sensorNode.setProperty("colortemp").send(String(-1));
}
d.lastsentvalue=value_colortemp;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_VL53L1X
void loop_VL53L1X()
{
sensordata &d=dataVL53L1X;
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;
/* for debugging
Serial.print("range: ");
Serial.print(vl53l1x.ranging_data.range_mm);
Serial.print("\tstatus: ");
Serial.print(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
Serial.print("\tstatus=");
Serial.print(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);
Serial.println();
*/
if (abs((int)d.lastsentvalue-value_vl53l1x_range)>=d.minchange){ //int abs
_changed=true;
}
if (lastsentvalue_vl53l1x_status!=vl53l1x.ranging_data.range_status) { //sensor status changed
_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("tofstatus").send(VL53L1X::rangeStatusToString(vl53l1x.ranging_data.range_status));
sensorNode.setProperty("tofrange").send(String(value_vl53l1x_range));
sensorNode.setProperty("tofpeaksignal").send(String(vl53l1x.ranging_data.peak_signal_count_rate_MCPS));
sensorNode.setProperty("tofambient").send(String(vl53l1x.ranging_data.ambient_count_rate_MCPS));
d.lastsentvalue=value_vl53l1x_range;
lastsentvalue_vl53l1x_status=vl53l1x.ranging_data.range_status;
d.lastsent=millis();
}
}
#endif
#ifdef SENSOR_ANEMOMETER
void loop_anemometer()
@ -841,16 +646,11 @@ void loopHandler() {
#endif
#ifdef SENSOR_TCS34725
if (tcs34725init_ok) {
loop_TCS34725_lux();
loop_TCS34725_colortemp();
}
sensor_tcs34725.sensorloop();
#endif
#ifdef SENSOR_VL53L1X
if (vl53l1xinit_ok) {
loop_VL53L1X();
}
sensor_vl53l1x.sensorloop();
#endif
#ifdef SENSOR_ANEMOMETER