2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2017-11-07 14:57:24 +00:00
|
|
|
xsns_10_bh1750.ino - BH1750 ambient light sensor support for Sonoff-Tasmota
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-01-01 12:55:01 +00:00
|
|
|
Copyright (C) 2019 Theo Arends
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-13 12:02:10 +01: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.
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2017-05-13 12:02:10 +01:00
|
|
|
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/>.
|
2017-01-28 13:41:01 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
#ifdef USE_BH1750
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* BH1750 - Ambient Light Intensity
|
2017-11-11 11:33:30 +00:00
|
|
|
*
|
|
|
|
* I2C Address: 0x23 or 0x5C
|
2017-01-28 13:41:01 +00:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2018-11-06 16:33:51 +00:00
|
|
|
#define XSNS_10 10
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#define BH1750_ADDR1 0x23
|
|
|
|
#define BH1750_ADDR2 0x5C
|
|
|
|
|
|
|
|
#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1lx resolution. Measurement time is approx 120ms.
|
|
|
|
|
2017-11-11 11:33:30 +00:00
|
|
|
uint8_t bh1750_address;
|
|
|
|
uint8_t bh1750_addresses[] = { BH1750_ADDR1, BH1750_ADDR2 };
|
2017-12-06 16:14:41 +00:00
|
|
|
uint8_t bh1750_type = 0;
|
2018-07-11 14:35:12 +01:00
|
|
|
uint8_t bh1750_valid = 0;
|
|
|
|
uint16_t bh1750_illuminance = 0;
|
|
|
|
char bh1750_types[] = "BH1750";
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
bool Bh1750Read(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-07-11 14:35:12 +01:00
|
|
|
if (bh1750_valid) { bh1750_valid--; }
|
|
|
|
|
|
|
|
if (2 != Wire.requestFrom(bh1750_address, (uint8_t)2)) { return false; }
|
2019-01-28 13:08:33 +00:00
|
|
|
uint8_t msb = Wire.read();
|
|
|
|
uint8_t lsb = Wire.read();
|
2018-07-11 14:35:12 +01:00
|
|
|
bh1750_illuminance = ((msb << 8) | lsb) / 1.2;
|
|
|
|
bh1750_valid = SENSOR_MAX_MISS;
|
|
|
|
return true;
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Bh1750Detect(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2017-10-18 17:22:34 +01:00
|
|
|
if (bh1750_type) {
|
2017-11-11 11:33:30 +00:00
|
|
|
return;
|
2017-04-25 17:24:42 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-06-30 15:44:36 +01:00
|
|
|
for (uint32_t i = 0; i < sizeof(bh1750_addresses); i++) {
|
2017-11-11 11:33:30 +00:00
|
|
|
bh1750_address = bh1750_addresses[i];
|
2017-10-18 17:22:34 +01:00
|
|
|
Wire.beginTransmission(bh1750_address);
|
2017-01-28 13:41:01 +00:00
|
|
|
Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE);
|
2017-11-11 11:33:30 +00:00
|
|
|
if (!Wire.endTransmission()) {
|
|
|
|
bh1750_type = 1;
|
2019-03-08 14:15:42 +00:00
|
|
|
AddLog_P2(LOG_LEVEL_DEBUG, S_LOG_I2C_FOUND_AT, bh1750_types, bh1750_address);
|
2017-11-11 11:33:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
void Bh1750EverySecond(void)
|
2018-07-11 14:35:12 +01:00
|
|
|
{
|
|
|
|
if (90 == (uptime %100)) {
|
2018-07-12 11:19:08 +01:00
|
|
|
// 1mS
|
2018-07-11 14:35:12 +01:00
|
|
|
Bh1750Detect();
|
|
|
|
}
|
2018-07-12 11:19:08 +01:00
|
|
|
else {
|
|
|
|
// 1mS
|
2018-07-11 14:35:12 +01:00
|
|
|
if (bh1750_type) {
|
|
|
|
if (!Bh1750Read()) {
|
|
|
|
AddLogMissed(bh1750_types, bh1750_valid);
|
|
|
|
// if (!bh1750_valid) { bh1750_type = 0; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Bh1750Show(bool json)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2018-07-11 14:35:12 +01:00
|
|
|
if (bh1750_valid) {
|
2017-11-04 15:36:51 +00:00
|
|
|
if (json) {
|
2019-05-16 17:43:23 +01:00
|
|
|
ResponseAppend_P(JSON_SNS_ILLUMINANCE, bh1750_types, bh1750_illuminance);
|
2017-11-04 15:36:51 +00:00
|
|
|
#ifdef USE_DOMOTICZ
|
2018-07-11 14:35:12 +01:00
|
|
|
if (0 == tele_period) {
|
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, bh1750_illuminance);
|
|
|
|
}
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2019-03-19 16:31:43 +00:00
|
|
|
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, bh1750_types, bh1750_illuminance);
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool Xsns10(uint8_t function)
|
2017-11-03 17:07:25 +00:00
|
|
|
{
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-11-03 17:07:25 +00:00
|
|
|
|
2017-11-04 15:36:51 +00:00
|
|
|
if (i2c_flg) {
|
|
|
|
switch (function) {
|
2018-07-11 14:35:12 +01:00
|
|
|
case FUNC_INIT:
|
2017-11-04 15:36:51 +00:00
|
|
|
Bh1750Detect();
|
|
|
|
break;
|
2018-07-11 14:35:12 +01:00
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Bh1750EverySecond();
|
|
|
|
break;
|
2017-12-25 16:41:12 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
2017-11-04 15:36:51 +00:00
|
|
|
Bh1750Show(1);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-03-19 16:31:43 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
2017-11-04 15:36:51 +00:00
|
|
|
Bh1750Show(0);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2017-11-04 15:36:51 +00:00
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-01-28 13:41:01 +00:00
|
|
|
#endif // USE_BH1750
|
|
|
|
#endif // USE_I2C
|