move tcs34725 to class

This commit is contained in:
interfisch 2021-11-07 21:56:42 +01:00
parent a8b751f662
commit 4dbc5b186c
4 changed files with 231 additions and 145 deletions

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

@ -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 dataTCS34725_lux_minchange=500 -D SENSOR_TCS34725_LUX_minchange=500
-D dataTCS34725_lux_senddelaymax=1000*60*1 -D SENSOR_TCS34725_LUX_senddelaymax=1000*60*1
-D dataTCS34725_colortemp_minchange=100 -D SENSOR_TCS34725_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_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
@ -284,10 +284,10 @@ monitor_speed = 115200
build_flags = build_flags =
-D SENSOR_HTU21D -D SENSOR_HTU21D
-D dataHTU21D_temperature_minchange=0.2 -D dataHTU21D_temperature_minchange=0.1
-D dataHTU21D_temperature_senddelaymax=1000*60*20 -D dataHTU21D_temperature_senddelaymax=1000*60*60
-D dataHTU21D_humidity_minchange=1.0 -D dataHTU21D_humidity_minchange=1.0
-D dataHTU21D_humidity_senddelaymax=1000*60*30 -D dataHTU21D_humidity_senddelaymax=1000*60*60

View File

@ -228,20 +228,25 @@
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
//#include "Adafruit_TCS34725.h" #include "sensor_tcs34725.cpp"
#include "tcs34725_agc.h" //class code from example https://github.com/adafruit/Adafruit_TCS34725/blob/master/examples/tcs34725autorange/tcs34725autorange.ino Sensor_TCS34725 sensor_tcs34725;
//Connect SCL to D1, SDA to D2, GND and 3v3 #ifndef SENSOR_TCS34725_LUX_minchange
//Maximum measurable light is around 20k Lux. (direct sunlight is easily above 20k Lux) #define SENSOR_TCS34725_LUX_minchange 500
//Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X); //initializer from standart class #endif
tcs34725 tcs; //wrapper class with agc #ifndef SENSOR_TCS34725_LUX_senddelaymax
bool tcs34725init_ok=false; #define SENSOR_TCS34725_LUX_senddelaymax 1000*60*5
struct sensordata dataTCS34725_lux; #endif
struct sensordata dataTCS34725_colortemp; #ifndef SENSOR_TCS34725_LUX_readdelay
uint16_t value_colortemp, value_tcs_lux, value_tcs_r,value_tcs_g,value_tcs_b,value_tcs_c; #define SENSOR_TCS34725_LUX_readdelay 1000*10
unsigned long lastread_tcs34725=0; #endif
#define TCS34725_MINLUXFORCT 30 //send only colortemperature values if lux is at least this high #ifndef SENSOR_TCS34725_COLORTEMP_minchange
#ifndef TCS34725_LUXFACTOR #define SENSOR_TCS34725_COLORTEMP_minchange 100
#define TCS34725_LUXFACTOR 1 #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
#endif #endif
@ -387,21 +392,9 @@ void setup() {
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
Serial.println("initializing tcs34725"); sensor_tcs34725.init();
if (!tcs.begin()) { sensor_tcs34725.setSettings_Lux(SENSOR_TCS34725_LUX_minchange,SENSOR_TCS34725_LUX_senddelaymax,SENSOR_TCS34725_LUX_readdelay);
Serial.println("No TCS34725 found!"); sensor_tcs34725.setSettings_Colortemp(SENSOR_TCS34725_COLORTEMP_minchange,SENSOR_TCS34725_COLORTEMP_senddelaymax,SENSOR_TCS34725_COLORTEMP_readdelay);
}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
@ -512,12 +505,7 @@ void setup() {
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750) sensor_tcs34725.advertise(sensorNode);
sensorNode.advertise("light_tcs");
#else
sensorNode.advertise("light");
#endif
sensorNode.advertise("colortemp");
#endif #endif
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X
@ -546,105 +534,6 @@ 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
@ -841,10 +730,7 @@ void loopHandler() {
#endif #endif
#ifdef SENSOR_TCS34725 #ifdef SENSOR_TCS34725
if (tcs34725init_ok) { sensor_tcs34725.sensorloop();
loop_TCS34725_lux();
loop_TCS34725_colortemp();
}
#endif #endif
#ifdef SENSOR_VL53L1X #ifdef SENSOR_VL53L1X