Files
OpenMQTTGateway/main/sensorTSL2561.cpp
Florian 32174e37a5 [SYS] Reduce Logs flash footprint (#2232)
By building only the log level required into LOG_LEVEL

Co-authored-by: Florian <1technophile@users.noreply.github.com>
2025-08-19 09:15:41 -05:00

124 lines
4.5 KiB
C++

/*
Theengs OpenMQTTGateway - We Unite Sensors in One Open-Source Interface
Act as a gateway between your 433mhz, infrared IR, BLE, LoRa signal and one interface like an MQTT broker
Send and receiving command by MQTT
This is the Light Meter Addon based on modules with a TSL2561:
- Measures ambient Light Intensity in Lux (lx), Foot Candela (ftcd) and Watt/m^2 (wattsm2)
- Required Hardware Module: TSL2561 (for instance Banggood.com product ID 1129550)
- Dependencies: Adafruit_TSL2561 and Adafruit_Sensor
Connection Scheme:
--------------------
TSL2561------> ESP8266
==============================================
Vcc ---------> 3.3V
GND ---------> GND
SCL ---------> D1
SDA ---------> D2
ADD ---------> N/C (Not Connected)
Copyright: (c) Chris Broekema
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "User_config.h"
#ifdef ZsensorTSL2561
# include <Adafruit_Sensor.h>
# include <Adafruit_TSL2561_U.h>
# include "TheengsCommon.h"
# include "Wire.h"
# include "config_TSL2561.h"
# include "math.h"
//Time used to wait for an interval before resending measured values
static unsigned long timetsl2561 = 0;
//int TSL2561_i2c_addr = 0x37; // Light Sensor I2C Address (Set in Adafruit library)
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
void displaySensorDetails(void) {
sensor_t sensor;
tsl.getSensor(&sensor);
THEENGS_LOG_TRACE(F("------------------------------------" CR));
THEENGS_LOG_TRACE(("Sensor: %s" CR), sensor.name);
THEENGS_LOG_TRACE(("Driver Ver: %s" CR), sensor.version);
THEENGS_LOG_TRACE(("Unique ID: %s" CR), sensor.sensor_id);
THEENGS_LOG_TRACE(("Max Value: %s lux" CR), sensor.max_value);
THEENGS_LOG_TRACE(("Min Value: %s lux" CR), sensor.min_value);
THEENGS_LOG_TRACE(("Resolution: %s lux" CR), sensor.resolution);
THEENGS_LOG_TRACE(F("------------------------------------" CR));
delay(500);
}
void setupZsensorTSL2561() {
THEENGS_LOG_NOTICE(F("Setup TSL2561 on adress: %H" CR), TSL2561_ADDR_FLOAT);
Wire.begin();
Wire.beginTransmission(TSL2561_ADDR_FLOAT);
if (!tsl.begin()) {
THEENGS_LOG_ERROR(F("No TSL2561 detected" CR));
}
// enable auto ranging
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
// tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
// since we're slowly sampling, enable high resolution but slow mode TSL2561
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
// tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
THEENGS_LOG_TRACE(F("TSL2561 Initialized. Printing detials now." CR));
displaySensorDetails();
}
void MeasureLightIntensityTSL2561() {
if (millis() > (timetsl2561 + TimeBetweenReadingtsl2561)) {
static uint32_t persisted_lux;
timetsl2561 = millis();
THEENGS_LOG_TRACE(F("Creating TSL2561 buffer" CR));
StaticJsonDocument<JSON_MSG_BUFFER> TSL2561dataBuffer;
JsonObject TSL2561data = TSL2561dataBuffer.to<JsonObject>();
sensors_event_t event;
tsl.getEvent(&event);
if (event.light)
// if event.light == 0 the sensor is clipping, do not send
{
if (persisted_lux != event.light || tsl2561_always) {
persisted_lux = event.light;
TSL2561data["lux"] = (float)event.light;
TSL2561data["ftcd"] = (float)(event.light) / 10.764;
TSL2561data["wattsm2"] = (float)(event.light) / 683.0;
TSL2561data["origin"] = subjectTSL12561toMQTT;
enqueueJsonObject(TSL2561data);
} else {
THEENGS_LOG_TRACE(F("Same lux value, do not send" CR));
}
} else {
THEENGS_LOG_ERROR(F("Failed to read from TSL2561" CR));
}
}
}
#endif