2019-11-08 19:36:59 +00:00
|
|
|
/*
|
2019-11-11 10:29:15 +00:00
|
|
|
xsns_57_tsl2591.ino - TSL2591 light sensor support for Tasmota
|
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Markus Bösling and Theo Arends
|
2019-11-08 19:36:59 +00:00
|
|
|
|
|
|
|
This program 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.
|
|
|
|
|
|
|
|
This program 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/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_TSL2591
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* TSL2591 - Light Intensity
|
|
|
|
*
|
|
|
|
* I2C Addresses: 0x29 (low), 0x39 (float) or 0x49 (high)
|
2019-11-11 10:29:15 +00:00
|
|
|
* (Used library supports address 0x29 only)
|
2019-11-08 19:36:59 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_57 57
|
2019-11-11 10:29:15 +00:00
|
|
|
#define XI2C_40 40 // See I2CDEVICES.md
|
|
|
|
|
|
|
|
#define TSL2591_ADDRESS 0x29 // Used library only supports this address only
|
2019-11-08 19:36:59 +00:00
|
|
|
|
|
|
|
#include <Adafruit_TSL2591.h>
|
|
|
|
|
2019-11-09 15:38:23 +00:00
|
|
|
Adafruit_TSL2591 tsl = Adafruit_TSL2591();
|
2019-11-08 19:36:59 +00:00
|
|
|
|
|
|
|
uint8_t tsl2591_type = 0;
|
|
|
|
uint8_t tsl2591_valid = 0;
|
|
|
|
float tsl2591_lux = 0;
|
|
|
|
|
2023-12-02 11:35:56 +00:00
|
|
|
tsl2591Gain_t gain_enum_array[4] = {TSL2591_GAIN_LOW,TSL2591_GAIN_MED,TSL2591_GAIN_HIGH,TSL2591_GAIN_MAX};
|
|
|
|
tsl2591IntegrationTime_t int_enum_array[6] = {TSL2591_INTEGRATIONTIME_100MS,TSL2591_INTEGRATIONTIME_200MS,TSL2591_INTEGRATIONTIME_300MS,TSL2591_INTEGRATIONTIME_400MS,TSL2591_INTEGRATIONTIME_500MS,TSL2591_INTEGRATIONTIME_600MS};
|
|
|
|
|
2019-11-08 19:36:59 +00:00
|
|
|
void Tsl2591Init(void)
|
|
|
|
{
|
2019-11-12 21:57:48 +00:00
|
|
|
// if (I2cSetDevice(0x29) || I2cSetDevice(0x39) || I2cSetDevice(0x49)) {
|
|
|
|
if (I2cSetDevice(0x29)) {
|
2019-11-11 10:29:15 +00:00
|
|
|
if (tsl.begin()) {
|
|
|
|
tsl.setGain(TSL2591_GAIN_MED);
|
|
|
|
tsl.setTiming(TSL2591_INTEGRATIONTIME_300MS);
|
|
|
|
tsl2591_type = 1;
|
|
|
|
I2cSetActiveFound(TSL2591_ADDRESS, "TSL2591");
|
|
|
|
}
|
2019-11-08 19:36:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 13:33:11 +00:00
|
|
|
void Tsl2591Read(void)
|
2019-11-08 19:36:59 +00:00
|
|
|
{
|
|
|
|
uint32_t lum = tsl.getFullLuminosity();
|
|
|
|
uint16_t ir, full;
|
|
|
|
ir = lum >> 16;
|
|
|
|
full = lum & 0xFFFF;
|
|
|
|
tsl2591_lux = tsl.calculateLux(full, ir);
|
|
|
|
tsl2591_valid = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Tsl2591EverySecond(void)
|
|
|
|
{
|
2019-11-12 21:57:48 +00:00
|
|
|
Tsl2591Read();
|
2019-11-08 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_TSL2591[] PROGMEM =
|
|
|
|
"{s}TSL2591 " D_ILLUMINANCE "{m}%s " D_UNIT_LUX "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
|
|
|
void Tsl2591Show(bool json)
|
|
|
|
{
|
|
|
|
if (tsl2591_valid) {
|
|
|
|
char lux_str[10];
|
|
|
|
dtostrf(tsl2591_lux, sizeof(lux_str)-1, 3, lux_str);
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(PSTR(",\"TSL2591\":{\"" D_JSON_ILLUMINANCE "\":%s}"), lux_str);
|
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) { DomoticzSensor(DZ_ILLUMINANCE, tsl2591_lux); }
|
2019-11-08 19:36:59 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_TSL2591, lux_str);
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 11:35:56 +00:00
|
|
|
bool tsl2591CommandSensor() {
|
|
|
|
bool serviced = true;
|
|
|
|
char argument[XdrvMailbox.data_len];
|
|
|
|
long value = 0;
|
|
|
|
|
|
|
|
for (uint32_t ca = 0; ca < XdrvMailbox.data_len; ca++) {
|
|
|
|
if ((' ' == XdrvMailbox.data[ca]) || ('=' == XdrvMailbox.data[ca])) { XdrvMailbox.data[ca] = ','; }
|
|
|
|
}
|
|
|
|
|
|
|
|
bool any_value = (strchr(XdrvMailbox.data, ',') != nullptr);
|
|
|
|
if (any_value) { value = strtol(ArgV(argument, 2), nullptr, 10); }
|
|
|
|
|
|
|
|
tsl.setGain(gain_enum_array[XdrvMailbox.payload-1]);
|
|
|
|
tsl.setTiming(int_enum_array[value-1]);
|
|
|
|
|
|
|
|
Response_P(PSTR("{\"Gain input\":%d,\"Gain hex\":0x%x,"),XdrvMailbox.payload,tsl.getGain());
|
|
|
|
ResponseAppend_P(PSTR("\"Timing input\":%d,\"Timing hex\":0x0%x}"),value,tsl.getTiming());
|
|
|
|
return serviced;
|
|
|
|
}
|
|
|
|
|
2019-11-08 19:36:59 +00:00
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns57(uint32_t function)
|
2019-11-08 19:36:59 +00:00
|
|
|
{
|
2019-11-11 10:29:15 +00:00
|
|
|
if (!I2cEnabled(XI2C_40)) { return false; }
|
|
|
|
|
2019-11-08 19:36:59 +00:00
|
|
|
bool result = false;
|
|
|
|
|
2019-11-12 21:57:48 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Tsl2591Init();
|
|
|
|
}
|
|
|
|
else if (tsl2591_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Tsl2591EverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Tsl2591Show(1);
|
|
|
|
break;
|
2019-11-08 19:36:59 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-12 21:57:48 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Tsl2591Show(0);
|
|
|
|
break;
|
2019-11-08 19:36:59 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2023-12-02 11:35:56 +00:00
|
|
|
case FUNC_COMMAND_SENSOR:
|
|
|
|
if (XSNS_57 == XdrvMailbox.index) {
|
|
|
|
result = tsl2591CommandSensor();
|
|
|
|
}
|
|
|
|
break;
|
2019-11-12 21:57:48 +00:00
|
|
|
}
|
2019-11-08 19:36:59 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // USE_TSL2591
|
|
|
|
#endif // USE_I2C
|