2017-01-28 13:41:01 +00:00
|
|
|
/*
|
2019-10-27 10:13:24 +00:00
|
|
|
xsns_10_bh1750.ino - BH1750 ambient light sensor support for Tasmota
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2019-12-31 13:23:34 +00:00
|
|
|
Copyright (C) 2020 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
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-04-02 14:17:54 +01:00
|
|
|
#define XSNS_10 10
|
|
|
|
#define XI2C_11 11 // See I2CDEVICES.md
|
2018-11-06 16:33:51 +00:00
|
|
|
|
2020-04-02 14:17:54 +01:00
|
|
|
#define BH1750_ADDR1 0x23
|
|
|
|
#define BH1750_ADDR2 0x5C
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2020-04-02 14:17:54 +01:00
|
|
|
#define BH1750_CONTINUOUS_HIGH_RES_MODE2 0x11 // Start measurement at 0.5 lx resolution. Measurement time is approx 120ms.
|
|
|
|
#define BH1750_CONTINUOUS_HIGH_RES_MODE 0x10 // Start measurement at 1 lx resolution. Measurement time is approx 120ms.
|
|
|
|
#define BH1750_CONTINUOUS_LOW_RES_MODE 0x13 // Start measurement at 4 lx resolution. Measurement time is approx 16ms.
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2020-04-02 14:17:54 +01:00
|
|
|
#define BH1750_MEASUREMENT_TIME_HIGH 0x40 // Measurement Time register high 3 bits
|
|
|
|
#define BH1750_MEASUREMENT_TIME_LOW 0x60 // Measurement Time register low 5 bits
|
2020-04-01 15:46:12 +01:00
|
|
|
|
|
|
|
struct BH1750DATA {
|
|
|
|
uint8_t address;
|
|
|
|
uint8_t addresses[2] = { BH1750_ADDR1, BH1750_ADDR2 };
|
|
|
|
uint8_t resolution[3] = { BH1750_CONTINUOUS_HIGH_RES_MODE, BH1750_CONTINUOUS_HIGH_RES_MODE2, BH1750_CONTINUOUS_LOW_RES_MODE };
|
|
|
|
uint8_t type = 0;
|
|
|
|
uint8_t valid = 0;
|
2020-04-02 14:17:54 +01:00
|
|
|
uint8_t mtreg = 69; // Default Measurement Time
|
2020-04-01 15:46:12 +01:00
|
|
|
uint16_t illuminance = 0;
|
|
|
|
char types[7] = "BH1750";
|
|
|
|
} Bh1750;
|
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Bh1750SetResolution(void)
|
|
|
|
{
|
|
|
|
Wire.beginTransmission(Bh1750.address);
|
|
|
|
Wire.write(Bh1750.resolution[Settings.SensorBits1.bh1750_resolution]);
|
|
|
|
return (!Wire.endTransmission());
|
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2020-04-02 14:17:54 +01:00
|
|
|
bool Bh1750SetMTreg(void)
|
|
|
|
{
|
|
|
|
Wire.beginTransmission(Bh1750.address);
|
|
|
|
uint8_t data = BH1750_MEASUREMENT_TIME_HIGH | ((Bh1750.mtreg >> 5) & 0x07);
|
|
|
|
Wire.write(data);
|
|
|
|
if (Wire.endTransmission()) { return false; }
|
|
|
|
Wire.beginTransmission(Bh1750.address);
|
|
|
|
data = BH1750_MEASUREMENT_TIME_LOW | (Bh1750.mtreg & 0x1F);
|
|
|
|
Wire.write(data);
|
|
|
|
if (Wire.endTransmission()) { return false; }
|
|
|
|
return Bh1750SetResolution();
|
|
|
|
}
|
|
|
|
|
2018-11-14 13:32:09 +00:00
|
|
|
bool Bh1750Read(void)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2020-04-01 15:46:12 +01:00
|
|
|
if (Bh1750.valid) { Bh1750.valid--; }
|
2018-07-11 14:35:12 +01:00
|
|
|
|
2020-04-01 15:46:12 +01:00
|
|
|
if (2 != Wire.requestFrom(Bh1750.address, (uint8_t)2)) { return false; }
|
|
|
|
Bh1750.illuminance = (Wire.read() << 8) | Wire.read();
|
2020-04-02 14:17:54 +01:00
|
|
|
Bh1750.illuminance /= (1.2 * (69 / Bh1750.mtreg));
|
|
|
|
if (1 == Settings.SensorBits1.bh1750_resolution) {
|
|
|
|
Bh1750.illuminance >>= 1;
|
|
|
|
}
|
2020-04-01 15:46:12 +01:00
|
|
|
|
|
|
|
Bh1750.valid = SENSOR_MAX_MISS;
|
2018-07-11 14:35:12 +01:00
|
|
|
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
|
|
|
{
|
2020-04-01 15:46:12 +01:00
|
|
|
for (uint32_t i = 0; i < sizeof(Bh1750.addresses); i++) {
|
|
|
|
Bh1750.address = Bh1750.addresses[i];
|
|
|
|
if (I2cActive(Bh1750.address)) { continue; }
|
|
|
|
|
2020-04-02 14:17:54 +01:00
|
|
|
if (Bh1750SetMTreg()) {
|
2020-04-01 15:46:12 +01:00
|
|
|
I2cSetActiveFound(Bh1750.address, Bh1750.types);
|
|
|
|
Bh1750.type = 1;
|
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
|
|
|
{
|
2019-11-11 16:32:44 +00:00
|
|
|
// 1mS
|
|
|
|
if (!Bh1750Read()) {
|
2020-04-01 15:46:12 +01:00
|
|
|
AddLogMissed(Bh1750.types, Bh1750.valid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Command Sensor10
|
|
|
|
*
|
2020-04-02 14:17:54 +01:00
|
|
|
* 0 - High resolution mode (default)
|
|
|
|
* 1 - High resolution mode 2
|
|
|
|
* 2 - Low resolution mode
|
|
|
|
* 31..254 - Measurement Time value (not persistent, default is 69)
|
2020-04-01 15:46:12 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
|
|
|
bool Bh1750CommandSensor(void)
|
|
|
|
{
|
|
|
|
if (XdrvMailbox.data_len) {
|
2020-04-02 14:17:54 +01:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
|
|
|
|
Settings.SensorBits1.bh1750_resolution = XdrvMailbox.payload;
|
|
|
|
Bh1750SetResolution();
|
|
|
|
}
|
|
|
|
else if ((XdrvMailbox.payload > 30) && (XdrvMailbox.payload < 255)) {
|
|
|
|
Bh1750.mtreg = XdrvMailbox.payload;
|
|
|
|
Bh1750SetMTreg();
|
2020-04-01 15:46:12 +01:00
|
|
|
}
|
2018-07-11 14:35:12 +01:00
|
|
|
}
|
2020-04-02 14:17:54 +01:00
|
|
|
Response_P(PSTR("{\"" D_CMND_SENSOR "10\":{\"Resolution\":%d,\"MTime\":%d}}"), Settings.SensorBits1.bh1750_resolution, Bh1750.mtreg);
|
2020-04-01 15:46:12 +01:00
|
|
|
|
|
|
|
return true;
|
2018-07-11 14:35:12 +01:00
|
|
|
}
|
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
void Bh1750Show(bool json)
|
2017-01-28 13:41:01 +00:00
|
|
|
{
|
2020-04-01 15:46:12 +01:00
|
|
|
if (Bh1750.valid) {
|
2017-11-04 15:36:51 +00:00
|
|
|
if (json) {
|
2020-04-01 15:46:12 +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) {
|
2020-04-01 15:46:12 +01:00
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, Bh1750.illuminance);
|
2018-07-11 14:35:12 +01:00
|
|
|
}
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
|
|
|
} else {
|
2020-04-01 15:46:12 +01: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-11-04 09:38:05 +00:00
|
|
|
if (!I2cEnabled(XI2C_11)) { return false; }
|
2019-11-03 16:54:39 +00:00
|
|
|
|
2019-01-28 13:08:33 +00:00
|
|
|
bool result = false;
|
2017-11-03 17:07:25 +00:00
|
|
|
|
2019-11-11 16:32:44 +00:00
|
|
|
if (FUNC_INIT == function) {
|
|
|
|
Bh1750Detect();
|
|
|
|
}
|
2020-04-01 15:46:12 +01:00
|
|
|
else if (Bh1750.type) {
|
2019-11-11 16:32:44 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Bh1750EverySecond();
|
|
|
|
break;
|
2020-04-01 15:46:12 +01:00
|
|
|
case FUNC_COMMAND_SENSOR:
|
|
|
|
if (XSNS_10 == XdrvMailbox.index) {
|
|
|
|
result = Bh1750CommandSensor();
|
|
|
|
}
|
|
|
|
break;
|
2019-11-11 16:32:44 +00:00
|
|
|
case FUNC_JSON_APPEND:
|
|
|
|
Bh1750Show(1);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#ifdef USE_WEBSERVER
|
2019-11-11 16:32:44 +00:00
|
|
|
case FUNC_WEB_SENSOR:
|
|
|
|
Bh1750Show(0);
|
|
|
|
break;
|
2017-11-03 17:07:25 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2019-11-11 16:32:44 +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
|