Compare commits

..

No commits in common. "275c641ec8b52dcf1a368de9c9cfd69706c4d475" and "a8b751f6623aad84e0d904f67576937507624af5" have entirely different histories.

12 changed files with 252 additions and 389 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,159 +0,0 @@
#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();
}
}

View File

@ -1,41 +0,0 @@
#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

@ -1,97 +0,0 @@
//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();
}
}
}

View File

@ -1,36 +0,0 @@
#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 dataBMP180_pressure_minchange=0.5
-D SENSOR_TCS34725 -D SENSOR_TCS34725
-D SENSOR_TCS34725_LUX_minchange=500 -D dataTCS34725_lux_minchange=500
-D SENSOR_TCS34725_LUX_senddelaymax=1000*60*1 -D dataTCS34725_lux_senddelaymax=1000*60*1
-D SENSOR_TCS34725_COLORTEMP_minchange=100 -D dataTCS34725_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 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
-D SENSOR_HS1101_PIN=D5 -D SENSOR_HS1101_PIN=D5
@ -258,9 +258,9 @@ build_flags =
-D dataBH1750_senddelaymax=1000*60*2 -D dataBH1750_senddelaymax=1000*60*2
-D SENSOR_VL53L1X -D SENSOR_VL53L1X
-D SENSOR_VL53L1X_minchange=100 -D dataVL53L1X_minchange=100
-D SENSOR_VL53L1X_senddelaymax=1000*30 -D dataVL53L1X_senddelaymax=1000*30
-D SENSOR_VL53L1X_readdelay=1000 -D dataVL53L1X_readdelay=1000
@ -284,10 +284,10 @@ monitor_speed = 115200
build_flags = build_flags =
-D SENSOR_HTU21D -D SENSOR_HTU21D
-D dataHTU21D_temperature_minchange=0.1 -D dataHTU21D_temperature_minchange=0.2
-D dataHTU21D_temperature_senddelaymax=1000*60*60 -D dataHTU21D_temperature_senddelaymax=1000*60*20
-D dataHTU21D_humidity_minchange=1.0 -D dataHTU21D_humidity_minchange=1.0
-D dataHTU21D_humidity_senddelaymax=1000*60*60 -D dataHTU21D_humidity_senddelaymax=1000*60*30

View File

@ -228,40 +228,37 @@
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
#include "sensor_tcs34725.cpp" //#include "Adafruit_TCS34725.h"
Sensor_TCS34725 sensor_tcs34725; #include "tcs34725_agc.h" //class code from example https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/tcs34725autorange/tcs34725autorange.ino
#ifndef SENSOR_TCS34725_LUX_minchange //Connect SCL to D1, SDA to D2, GND and 3v3
#define SENSOR_TCS34725_LUX_minchange 500 //Maximum measurable light is around 20k Lux. (direct sunlight is easily above 20k Lux)
#endif //Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); //initializer from standart class
#ifndef SENSOR_TCS34725_LUX_senddelaymax tcs34725 tcs; //wrapper class with agc
#define SENSOR_TCS34725_LUX_senddelaymax 1000*60*5 bool tcs34725init_ok=false;
#endif struct sensordata dataTCS34725_lux;
#ifndef SENSOR_TCS34725_LUX_readdelay struct sensordata dataTCS34725_colortemp;
#define SENSOR_TCS34725_LUX_readdelay 1000*10 uint16_t value_colortemp, value_tcs_lux, value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c;
#endif unsigned long lastread_tcs34725=0;
#ifndef SENSOR_TCS34725_COLORTEMP_minchange #define TCS34725_MINLUXFORCT 30 //send only colortemperature values if lux is at least this high
#define SENSOR_TCS34725_COLORTEMP_minchange 100 #ifndef TCS34725_LUXFACTOR
#endif #define TCS34725_LUXFACTOR 1
#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
#endif #endif
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X
#include "sensor_vl53l1x.cpp" #ifndef WIRE_H
Sensor_VL53L1X sensor_vl53l1x; #include <Wire.h>
#ifndef SENSOR_VL53L1X_minchange #define WIRE_H
#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 #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 #endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER
@ -390,14 +387,44 @@ void setup() {
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
sensor_tcs34725.init(); Serial.println("initializing tcs34725");
sensor_tcs34725.setSettings_Lux(SENSOR_TCS34725_LUX_minchange,SENSOR_TCS34725_LUX_senddelaymax,SENSOR_TCS34725_LUX_readdelay); if (!tcs.begin()) {
sensor_tcs34725.setSettings_Colortemp(SENSOR_TCS34725_COLORTEMP_minchange,SENSOR_TCS34725_COLORTEMP_senddelaymax,SENSOR_TCS34725_COLORTEMP_readdelay); 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
#endif #endif
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X
sensor_vl53l1x.init(); Serial.println("initializing vl53l1x");
sensor_vl53l1x.setSettings(SENSOR_VL53L1X_minchange,SENSOR_VL53L1X_senddelaymax,SENSOR_VL53L1X_readdelay); 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
#endif #endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER
@ -485,11 +512,19 @@ void setup() {
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
sensor_tcs34725.advertise(sensorNode); #if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode.advertise("light_tcs");
#else
sensorNode.advertise("light");
#endif
sensorNode.advertise("colortemp");
#endif #endif
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X
sensor_vl53l1x.advertise(sensorNode); sensorNode.advertise("tofstatus");
sensorNode.advertise("tofrange");
sensorNode.advertise("tofpeaksignal");
sensorNode.advertise("tofambient");
#endif #endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER
@ -511,6 +546,166 @@ void loop() {
Homie.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 #ifdef SENSOR_ANEMOMETER
void loop_anemometer() void loop_anemometer()
@ -646,11 +841,16 @@ void loopHandler() {
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
sensor_tcs34725.sensorloop(); if (tcs34725init_ok) {
loop_TCS34725_lux();
loop_TCS34725_colortemp();
}
#endif #endif
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X
sensor_vl53l1x.sensorloop(); if (vl53l1xinit_ok) {
loop_VL53L1X();
}
#endif #endif
#ifdef SENSOR_ANEMOMETER #ifdef SENSOR_ANEMOMETER