implement tcs34725 light color sensor

This commit is contained in:
interfisch 2020-10-23 23:09:01 +02:00
parent 17eed1b279
commit 954526277f
2 changed files with 129 additions and 12 deletions

View File

@ -30,12 +30,16 @@ monitor_speed = 115200
build_flags =
-D SENSOR_BMP180
-D dataBMP180_temperature_minchange=0.2
-D dataBMP180_pressure_minchange=0.1
-D dataBMP180_temperature_minchange=0.5
-D dataBMP180_pressure_minchange=0.5
-D SENSOR_TCS34725
-D dataTCS34725_lux_minchange=20
-D dataTCS34725_colortemp_minchange=100
lib_deps =
Adafruit BMP085 Library@1.1.0
https://github.com/adafruit/Adafruit_TCS34725
ArduinoJson@6.16.1 #dependency of homie. using older version because of "ambiguous overload for operator|" error
Homie@3.0.0
@ -56,7 +60,7 @@ build_flags =
-D SENSOR_BMP180
-D dataBMP180_temperature_minchange=0.2
-D dataBMP180_pressure_minchange=0.1
-D dataBMP180_pressure_minchange=0.5
-D SENSOR_PIR
-D PIRPIN=D6

View File

@ -43,6 +43,7 @@ struct sensordata
#ifdef SENSOR_BMP180
//Connect SCL to D1, SDA to D2, GND and 3v3
#include <Adafruit_BMP085.h>
bool bmp180init_ok=false;
Adafruit_BMP085 bmp180;
struct sensordata dataBMP180_temperature; //struct values are changed in setup()
float value_temperatureBMP=0;
@ -55,6 +56,7 @@ struct sensordata
#include <Wire.h>
#include <BH1750.h>
BH1750 lightMeter(0x23);
bool bh1750init_ok=false;
sensordata dataBH1750;
float value_lightBH1750=0;
#endif
@ -150,15 +152,20 @@ struct sensordata
void readSDS018();
#endif
#ifdef SENSOR_TCS34725
#include "Adafruit_TCS34725.h"
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_700MS, TCS34725_GAIN_1X);
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;
#endif
// data/homie/config.json hochladen mit platformio run --target uploadfs
// config contains homie device name, mqtt ip and wifi credentials
/*
float humidity; //[%RH] DHT
float temperature; //[deg C] DHT
float light; //[Lux] BH1750
bool movement //true bei pir output hight, false wenn low HC12?501?
*/
@ -194,6 +201,8 @@ void setup() {
Serial.println("initializing bmp180");
if (!bmp180.begin()){
Serial.println("#ERROR: BMP180 init fail\n\n");
}else{
bmp180init_ok=true; //stays false if init failed, sensor will not be read in loop
}
#ifdef dataBMP180_temperature_minchange
dataBMP180_temperature.minchange=dataBMP180_temperature_minchange;
@ -208,6 +217,7 @@ void setup() {
Wire.begin();
if (lightMeter.begin(BH1750::CONTINUOUS_HIGH_RES_MODE)) {
Serial.println(F("BH1750 Advanced begin"));
bh1750init_ok=true;
} else {
Serial.println(F("Error initialising BH1750"));
}
@ -279,6 +289,21 @@ void setup() {
#endif
#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_colortemp_minchange
dataTCS34725_colortemp.minchange=dataTCS34725_colortemp_minchange;
#endif
#endif
//Homie_setFirmware(FW_NAME, FW_VERSION);
//Homie_setBrand(FW_NAME);
@ -325,6 +350,15 @@ void setup() {
sensorNode.advertise("dust_pm10");
#endif
#ifdef SENSOR_TCS34725
#if defined(SENSOR_LDR) || defined(SENSOR_BH1750)
sensorNode.advertise("light_tcs");
#else
sensorNode.advertise("light");
#endif
sensorNode.advertise("colortemp");
#endif
Serial.println("connecting..");
Homie.setup();
@ -650,6 +684,74 @@ void loop_SDS018_pm10()
}
#endif
#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)) {
tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
}
value_tcs_lux = tcs.calculateLux(value_tcs_r, value_tcs_g, value_tcs_b);
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)) {
tcs.getRawData(&value_tcs_r, &value_tcs_g, &value_tcs_b, &value_tcs_c);
}
// colorTemp = tcs.calculateColorTemperature(r, g, b);
value_colortemp = tcs.calculateColorTemperature_dn40(value_tcs_r, value_tcs_g, value_tcs_b, value_tcs_c);
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;
sensorNode.setProperty("colortemp").send(String(value_colortemp));
d.lastsentvalue=value_colortemp;
d.lastsent=millis();
}
}
#endif
void loopHandler() {
@ -660,12 +762,16 @@ void loopHandler() {
#endif
#ifdef SENSOR_BMP180
if (bmp180init_ok) {
loop_BMP180_temperature();
loop_BMP180_pressure();
}
#endif
#ifdef SENSOR_BH1750
if (bh1750init_ok) {
loop_BH1750();
}
#endif
#ifdef SENSOR_LDR
@ -685,6 +791,13 @@ void loopHandler() {
loop_SDS018_pm10();
#endif
#ifdef SENSOR_TCS34725
if (tcs34725init_ok) {
loop_TCS34725_lux();
loop_TCS34725_colortemp();
}
#endif
}