2017-12-06 13:04:58 +00:00
|
|
|
/*
|
|
|
|
xsns_16_tsl2561.ino - TSL2561 light sensor support for Sonoff-Tasmota
|
|
|
|
|
2018-02-20 15:10:31 +00:00
|
|
|
Copyright (C) 2018 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-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
|
|
|
|
|
|
|
void Tsl2561Detect()
|
|
|
|
{
|
2018-02-20 15:10:31 +00:00
|
|
|
if (!Tsl.available()) {
|
|
|
|
Tsl.begin();
|
|
|
|
if (Tsl.available()) {
|
|
|
|
snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "TSL2561", Tsl.address());
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2017-12-06 13:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
const char HTTP_SNS_TSL2561[] PROGMEM =
|
2018-02-20 15:10:31 +00:00
|
|
|
"%s{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
|
|
|
|
|
|
|
|
void Tsl2561Show(boolean json)
|
|
|
|
{
|
2018-02-20 15:10:31 +00:00
|
|
|
uint8_t id;
|
|
|
|
bool gain;
|
|
|
|
Tsl2561::exposure_t exposure;
|
|
|
|
uint16_t scaledFull, scaledIr;
|
|
|
|
uint32_t full, ir;
|
|
|
|
uint32_t milliLux;
|
|
|
|
|
|
|
|
if (Tsl.available()) {
|
|
|
|
if (Tsl.on()) {
|
|
|
|
if( Tsl.id(id)
|
|
|
|
&& Tsl2561Util::autoGain(Tsl, gain, exposure, scaledFull, scaledIr)
|
|
|
|
&& Tsl2561Util::normalizedLuminosity(gain, exposure, full = scaledFull, ir = scaledIr)
|
|
|
|
&& Tsl2561Util::milliLux(full, ir, milliLux, Tsl2561::packageCS(id))) {
|
|
|
|
|
|
|
|
snprintf_P(log_data, sizeof(log_data), PSTR(D_ILLUMINANCE " g:%d, e:%d, f:%u, i:%u -> %u.%03u " D_UNIT_LUX),
|
|
|
|
gain, exposure, full, ir, milliLux/1000, milliLux%1000);
|
|
|
|
AddLog(LOG_LEVEL_DEBUG);
|
2017-12-06 13:04:58 +00:00
|
|
|
|
2018-02-20 15:10:31 +00:00
|
|
|
if (json) {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"TSL2561\":{\"" D_JSON_ILLUMINANCE "\":%u.%03u}"),
|
|
|
|
mqtt_data, milliLux/1000, milliLux%1000);
|
2017-12-06 13:04:58 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-04-11 09:11:20 +01:00
|
|
|
if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, (milliLux+500)/1000);
|
2017-12-06 13:04:58 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2018-02-20 15:10:31 +00:00
|
|
|
} else {
|
|
|
|
snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_TSL2561, mqtt_data, milliLux/1000, milliLux%1000);
|
2017-12-06 13:04:58 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2018-02-20 15:10:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Tsl.off();
|
2017-12-06 13:04:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
#define XSNS_16
|
|
|
|
|
|
|
|
boolean Xsns16(byte function)
|
|
|
|
{
|
|
|
|
boolean result = false;
|
|
|
|
|
|
|
|
if (i2c_flg) {
|
|
|
|
switch (function) {
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_PREP_BEFORE_TELEPERIOD:
|
2017-12-06 13:04:58 +00:00
|
|
|
Tsl2561Detect();
|
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-12-06 13:04:58 +00:00
|
|
|
Tsl2561Show(1);
|
|
|
|
break;
|
|
|
|
#ifdef USE_WEBSERVER
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_WEB_APPEND:
|
2017-12-06 13:04:58 +00:00
|
|
|
Tsl2561Show(0);
|
|
|
|
break;
|
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-05-12 20:15:59 +01:00
|
|
|
#endif // USE_TSL2561
|
2018-02-20 15:10:31 +00:00
|
|
|
#endif // USE_I2C
|