2017-12-06 13:04:58 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_16_tsl2561.ino - TSL2561 light sensor support for Tasmota
|
2017-12-06 13:04:58 +00:00
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 Theo Arends and Joachim Banzhaf
|
2017-12-06 13:04:58 +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_TSL2561
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* TSL2561 - Light Intensity
|
|
|
|
*
|
2018-02-20 15:10:31 +00:00
|
|
|
* Using library https://github.com/joba-1/Joba_Tsl2561
|
2018-05-12 20:15:59 +01:00
|
|
|
*
|
|
|
|
* I2C Addresses: 0x29 (low), 0x39 (float) or 0x49 (high)
|
2017-12-06 13:04:58 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_16 16
|
2019-11-03 16:54:39 +00:00
|
|
|
#define XI2C_16 16 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2018-02-20 15:10:31 +00:00
|
|
|
#include <Tsl2561Util.h>
|
2017-12-06 13:04:58 +00:00
|
|
|
|
2018-02-20 15:10:31 +00:00
|
|
|
Tsl2561 Tsl(Wire);
|
2017-12-06 13:04:58 +00:00
|
|
|
|
2018-09-22 11:04:38 +01:00
|
|
|
uint8_t tsl2561_type = 0;
|
|
|
|
uint8_t tsl2561_valid = 0;
|
|
|
|
uint32_t tsl2561_milliLux = 0;
|
2020-09-28 21:08:38 +01:00
|
|
|
uint32_t tsl2561_full = 0;
|
|
|
|
uint32_t tsl2561_ir = 0;
|
2018-09-22 11:04:38 +01:00
|
|
|
char tsl2561_types[] = "TSL2561";
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
bool Tsl2561Read(void)
|
2018-09-22 11:04:38 +01:00
|
|
|
{
|
|
|
|
if (tsl2561_valid) { tsl2561_valid--; }
|
|
|
|
|
|
|
|
uint8_t id;
|
|
|
|
bool gain;
|
|
|
|
Tsl2561::exposure_t exposure;
|
|
|
|
uint16_t scaledFull, scaledIr;
|
|
|
|
|
|
|
|
if (Tsl.on()) {
|
|
|
|
if (Tsl.id(id)
|
|
|
|
&& Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr)
|
2020-09-28 21:08:38 +01:00
|
|
|
&& Tsl2561Util::normalizedLuminosity(gain, exposure, tsl2561_full = scaledFull, tsl2561_ir = scaledIr)) {
|
|
|
|
if (! Tsl2561Util::milliLux(tsl2561_full, tsl2561_ir, tsl2561_milliLux, Tsl2561::packageCS(id))) {
|
|
|
|
tsl2561_milliLux = 0;
|
|
|
|
}
|
2018-09-22 11:04:38 +01:00
|
|
|
} else{
|
|
|
|
tsl2561_milliLux = 0;
|
2020-09-28 21:08:38 +01:00
|
|
|
tsl2561_full = 0;
|
|
|
|
tsl2561_ir = 0;
|
2018-09-22 11:04:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
tsl2561_valid = SENSOR_MAX_MISS;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Tsl2561Detect(void)
|
2017-12-06 13:04:58 +00:00
|
|
|
{
|
2019-11-07 15:56:05 +00:00
|
|
|
if (I2cSetDevice(0x29) || I2cSetDevice(0x39) || I2cSetDevice(0x49)) {
|
2019-11-11 16:32:44 +00:00
|
|
|
uint8_t id;
|
2019-02-07 10:40:44 +00:00
|
|
|
Tsl.begin();
|
2019-02-11 18:22:14 +00:00
|
|
|
if (!Tsl.id(id)) return;
|
2019-01-18 21:10:22 +00:00
|
|
|
if (Tsl.on()) {
|
2018-09-22 11:04:38 +01:00
|
|
|
tsl2561_type = 1;
|
2019-11-09 17:34:22 +00:00
|
|
|
I2cSetActiveFound(Tsl.address(), tsl2561_types);
|
2017-12-06 13:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Tsl2561EverySecond(void)
|
2018-09-22 11:04:38 +01:00
|
|
|
{
|
2020-10-28 16:32:07 +00:00
|
|
|
if (!(TasmotaGlobal.uptime %2)) { // Every 2 seconds
|
2018-09-22 11:18:13 +01:00
|
|
|
// ?mS - 4Sec
|
2019-11-11 16:32:44 +00:00
|
|
|
if (!Tsl2561Read()) {
|
|
|
|
AddLogMissed(tsl2561_types, tsl2561_valid);
|
2018-09-22 11:04:38 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-06 13:04:58 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_TSL2561[] PROGMEM =
|
2019-03-19 16:31:43 +00:00
|
|
|
"{s}TSL2561 " D_ILLUMINANCE "{m}%u.%03u " D_UNIT_LUX "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
|
2017-12-06 13:04:58 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Tsl2561Show(bool json)
|
2017-12-06 13:04:58 +00:00
|
|
|
{
|
2018-09-22 11:04:38 +01:00
|
|
|
if (tsl2561_valid) {
|
|
|
|
if (json) {
|
2020-09-28 21:08:38 +01:00
|
|
|
ResponseAppend_P(PSTR(",\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u,\"IR\":%u,\"Broadband\":%u}"),
|
|
|
|
tsl2561_milliLux / 1000, tsl2561_milliLux % 1000, tsl2561_ir, tsl2561_full);
|
2017-12-06 13:04:58 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if (0 == TasmotaGlobal.tele_period) { DomoticzSensor(DZ_ILLUMINANCE, (tsl2561_milliLux + 500) / 1000); }
|
2017-12-06 13:04:58 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2018-09-22 11:04:38 +01:00
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_TSL2561, tsl2561_milliLux / 1000, tsl2561_milliLux % 1000);
|
2017-12-06 13:04:58 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns16(uint32_t function)
|
2017-12-06 13:04:58 +00:00
|
|
|
{
|
2019-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_16)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-12-06 13:04:58 +00:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Tsl2561Detect();
|
|
|
|
}
|
|
|
|
else if (tsl2561_type) {
|
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Tsl2561EverySecond();
|
|
|
|
break;
|
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Tsl2561Show(1);
|
|
|
|
break;
|
2017-12-06 13:04:58 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 16:32:44 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Tsl2561Show(0);
|
|
|
|
break;
|
2017-12-06 13:04:58 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2019-11-11 16:32:44 +00:00
|
|
|
}
|
2017-12-06 13:04:58 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-12 20:15:59 +01:00
|
|
|
#endif // USE_TSL2561
|
2018-02-20 15:10:31 +00:00
|
|
|
#endif // USE_I2C
|