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
|
|
|
|
2021-01-01 12:44:04 +00:00
|
|
|
Copyright (C) 2021 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
|
|
|
*
|
2020-06-01 17:00:56 +01:00
|
|
|
* Bh1750Resolution1 0..2 - Set BH1750 1 resolution mode
|
|
|
|
* Bh1750Resolution2 0..2 - Set BH1750 2 resolution mode
|
|
|
|
* Bh1750MTime1 30..255 - Set BH1750 1 MT register
|
|
|
|
* Bh1750MTime2 30..255 - Set BH1750 2 MT register
|
|
|
|
*
|
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
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
#define D_PRFX_BH1750 "Bh1750"
|
|
|
|
#define D_CMND_RESOLUTION "Resolution"
|
|
|
|
#define D_CMND_MTREG "MTime"
|
|
|
|
|
|
|
|
const char kBh1750Commands[] PROGMEM = D_PRFX_BH1750 "|" // Prefix
|
|
|
|
D_CMND_RESOLUTION "|" D_CMND_MTREG ;
|
|
|
|
|
|
|
|
void (* const Bh1750Command[])(void) PROGMEM = {
|
|
|
|
&CmndBh1750Resolution, &CmndBh1750MTime };
|
|
|
|
|
|
|
|
struct {
|
2020-04-01 15:46:12 +01:00
|
|
|
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 };
|
2020-06-01 17:00:56 +01:00
|
|
|
uint8_t count = 0;
|
|
|
|
char types[7] = "BH1750";
|
|
|
|
} Bh1750;
|
|
|
|
|
|
|
|
struct {
|
|
|
|
uint8_t address;
|
2023-10-19 17:01:12 +01:00
|
|
|
uint8_t bus;
|
2020-04-01 15:46:12 +01:00
|
|
|
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;
|
2020-06-01 17:00:56 +01:00
|
|
|
} Bh1750_sensors[2];
|
2020-04-01 15:46:12 +01:00
|
|
|
|
|
|
|
/*********************************************************************************************/
|
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
uint8_t Bh1750Resolution(uint32_t sensor_index) {
|
2021-06-11 17:14:12 +01:00
|
|
|
uint8_t settings_resolution = Settings->SensorBits1.bh1750_1_resolution;
|
2020-06-01 17:00:56 +01:00
|
|
|
if (1 == sensor_index) {
|
2021-06-11 17:14:12 +01:00
|
|
|
settings_resolution = Settings->SensorBits1.bh1750_2_resolution;
|
2020-06-01 17:00:56 +01:00
|
|
|
}
|
|
|
|
return settings_resolution;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Bh1750SetResolution(uint32_t sensor_index) {
|
2024-09-16 15:27:30 +01:00
|
|
|
/*
|
2023-10-19 17:01:12 +01:00
|
|
|
TwoWire& myWire = I2cGetWire(Bh1750_sensors[sensor_index].bus);
|
|
|
|
myWire.beginTransmission(Bh1750_sensors[sensor_index].address);
|
|
|
|
myWire.write(Bh1750.resolution[Bh1750Resolution(sensor_index)]);
|
|
|
|
return (!myWire.endTransmission());
|
2024-09-16 15:27:30 +01:00
|
|
|
*/
|
|
|
|
return I2cWrite0(Bh1750_sensors[sensor_index].address, Bh1750.resolution[Bh1750Resolution(sensor_index)], Bh1750_sensors[sensor_index].bus);
|
2020-04-01 15:46:12 +01:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
bool Bh1750SetMTreg(uint32_t sensor_index) {
|
2024-09-16 15:27:30 +01:00
|
|
|
/*
|
2023-10-19 17:01:12 +01:00
|
|
|
TwoWire& myWire = I2cGetWire(Bh1750_sensors[sensor_index].bus);
|
|
|
|
if (&myWire == nullptr) { return false; } // No valid I2c bus
|
|
|
|
myWire.beginTransmission(Bh1750_sensors[sensor_index].address);
|
2020-06-01 17:00:56 +01:00
|
|
|
uint8_t data = BH1750_MEASUREMENT_TIME_HIGH | ((Bh1750_sensors[sensor_index].mtreg >> 5) & 0x07);
|
2023-10-19 17:01:12 +01:00
|
|
|
myWire.write(data);
|
|
|
|
if (myWire.endTransmission()) { return false; }
|
|
|
|
myWire.beginTransmission(Bh1750_sensors[sensor_index].address);
|
2020-06-01 17:00:56 +01:00
|
|
|
data = BH1750_MEASUREMENT_TIME_LOW | (Bh1750_sensors[sensor_index].mtreg & 0x1F);
|
2023-10-19 17:01:12 +01:00
|
|
|
myWire.write(data);
|
|
|
|
if (myWire.endTransmission()) { return false; }
|
2024-09-16 15:27:30 +01:00
|
|
|
*/
|
|
|
|
uint8_t reg = BH1750_MEASUREMENT_TIME_HIGH | ((Bh1750_sensors[sensor_index].mtreg >> 5) & 0x07);
|
|
|
|
if (!I2cWrite0(Bh1750_sensors[sensor_index].address, reg, Bh1750_sensors[sensor_index].bus)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
reg = BH1750_MEASUREMENT_TIME_LOW | (Bh1750_sensors[sensor_index].mtreg & 0x1F);
|
|
|
|
if (!I2cWrite0(Bh1750_sensors[sensor_index].address, reg, Bh1750_sensors[sensor_index].bus)) {
|
|
|
|
return false;
|
|
|
|
}
|
2020-06-01 17:00:56 +01:00
|
|
|
return Bh1750SetResolution(sensor_index);
|
2020-04-02 14:17:54 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
bool Bh1750Read(uint32_t sensor_index) {
|
|
|
|
if (Bh1750_sensors[sensor_index].valid) { Bh1750_sensors[sensor_index].valid--; }
|
2024-09-16 15:27:30 +01:00
|
|
|
/*
|
2023-10-19 17:01:12 +01:00
|
|
|
TwoWire& myWire = I2cGetWire(Bh1750_sensors[sensor_index].bus);
|
|
|
|
if (2 != myWire.requestFrom(Bh1750_sensors[sensor_index].address, (uint8_t)2)) { return false; }
|
2018-07-11 14:35:12 +01:00
|
|
|
|
2023-10-19 17:01:12 +01:00
|
|
|
float illuminance = (myWire.read() << 8) | myWire.read();
|
2024-09-16 15:27:30 +01:00
|
|
|
*/
|
|
|
|
uint8_t data[2];
|
|
|
|
if (I2cReadBuffer0(Bh1750_sensors[sensor_index].address, data, 2, Bh1750_sensors[sensor_index].bus)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
float illuminance = (data[0] << 8) | data[1];
|
2022-07-20 10:11:18 +01:00
|
|
|
illuminance *= 57.5 / (float)Bh1750_sensors[sensor_index].mtreg; // Fix #16022
|
2020-06-01 17:00:56 +01:00
|
|
|
if (1 == Bh1750Resolution(sensor_index)) {
|
2020-04-05 10:52:02 +01:00
|
|
|
illuminance /= 2;
|
2020-04-02 14:17:54 +01:00
|
|
|
}
|
2020-06-01 17:00:56 +01:00
|
|
|
Bh1750_sensors[sensor_index].illuminance = illuminance;
|
2020-04-01 15:46:12 +01:00
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
Bh1750_sensors[sensor_index].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
|
|
|
/********************************************************************************************/
|
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
void Bh1750Detect(void) {
|
2023-10-19 17:01:12 +01:00
|
|
|
for (uint32_t bus = 0; bus < 2; bus++) {
|
|
|
|
for (uint32_t i = 0; i < sizeof(Bh1750.addresses); i++) {
|
|
|
|
if (!I2cSetDevice(Bh1750.addresses[i], bus)) { continue; }
|
|
|
|
|
|
|
|
Bh1750_sensors[Bh1750.count].address = Bh1750.addresses[i];
|
|
|
|
Bh1750_sensors[Bh1750.count].bus = bus;
|
|
|
|
if (Bh1750SetMTreg(Bh1750.count)) {
|
|
|
|
I2cSetActiveFound(Bh1750_sensors[Bh1750.count].address, Bh1750.types, Bh1750_sensors[Bh1750.count].bus);
|
|
|
|
Bh1750.count++;
|
|
|
|
if (2 == Bh1750.count) { return; }
|
|
|
|
}
|
2017-11-11 11:33:30 +00:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
void Bh1750EverySecond(void) {
|
|
|
|
for (uint32_t i = 0; i < Bh1750.count; i++) {
|
|
|
|
// 1mS
|
|
|
|
if (!Bh1750Read(i)) {
|
|
|
|
// AddLogMissed(Bh1750.types, Bh1750.valid);
|
|
|
|
}
|
2020-04-01 15:46:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*********************************************************************************************\
|
2020-06-01 17:00:56 +01:00
|
|
|
* Commands
|
2020-04-01 15:46:12 +01:00
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
void CmndBh1750Resolution(void) {
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= Bh1750.count)) {
|
2020-04-02 14:17:54 +01:00
|
|
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
|
2020-06-01 17:00:56 +01:00
|
|
|
if (1 == XdrvMailbox.index) {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->SensorBits1.bh1750_1_resolution = XdrvMailbox.payload;
|
2020-06-01 17:00:56 +01:00
|
|
|
} else {
|
2021-06-11 17:14:12 +01:00
|
|
|
Settings->SensorBits1.bh1750_2_resolution = XdrvMailbox.payload;
|
2020-06-01 17:00:56 +01:00
|
|
|
}
|
|
|
|
Bh1750SetResolution(XdrvMailbox.index -1);
|
2020-04-01 15:46:12 +01:00
|
|
|
}
|
2020-06-01 17:00:56 +01:00
|
|
|
ResponseCmndIdxNumber(Bh1750Resolution(XdrvMailbox.index -1));
|
2018-07-11 14:35:12 +01:00
|
|
|
}
|
2020-06-01 17:00:56 +01:00
|
|
|
}
|
2020-04-01 15:46:12 +01:00
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
void CmndBh1750MTime(void) {
|
|
|
|
if ((XdrvMailbox.index > 0) && (XdrvMailbox.index <= Bh1750.count)) {
|
|
|
|
if ((XdrvMailbox.payload > 30) && (XdrvMailbox.payload < 255)) {
|
|
|
|
Bh1750_sensors[XdrvMailbox.index -1].mtreg = XdrvMailbox.payload;
|
|
|
|
Bh1750SetMTreg(XdrvMailbox.index -1);
|
|
|
|
}
|
|
|
|
ResponseCmndIdxNumber(Bh1750_sensors[XdrvMailbox.index -1].mtreg);
|
|
|
|
}
|
2018-07-11 14:35:12 +01:00
|
|
|
}
|
|
|
|
|
2020-06-01 17:00:56 +01:00
|
|
|
/********************************************************************************************/
|
|
|
|
|
|
|
|
void Bh1750Show(bool json) {
|
|
|
|
for (uint32_t sensor_index = 0; sensor_index < Bh1750.count; sensor_index++) {
|
|
|
|
if (Bh1750_sensors[sensor_index].valid) {
|
|
|
|
char sensor_name[10];
|
|
|
|
strlcpy(sensor_name, Bh1750.types, sizeof(sensor_name));
|
|
|
|
if (Bh1750.count > 1) {
|
2020-06-03 14:12:23 +01:00
|
|
|
snprintf_P(sensor_name, sizeof(sensor_name), PSTR("%s%c%02X"), sensor_name, IndexSeparator(), Bh1750_sensors[sensor_index].address); // BH1750-23
|
2018-07-11 14:35:12 +01:00
|
|
|
}
|
2020-06-01 17:00:56 +01:00
|
|
|
|
|
|
|
if (json) {
|
|
|
|
ResponseAppend_P(JSON_SNS_ILLUMINANCE, sensor_name, Bh1750_sensors[sensor_index].illuminance);
|
|
|
|
#ifdef USE_DOMOTICZ
|
2020-10-29 12:37:09 +00:00
|
|
|
if ((0 == TasmotaGlobal.tele_period) && (0 == sensor_index)) {
|
2020-06-01 17:00:56 +01:00
|
|
|
DomoticzSensor(DZ_ILLUMINANCE, Bh1750_sensors[sensor_index].illuminance);
|
|
|
|
}
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_DOMOTICZ
|
|
|
|
#ifdef USE_WEBSERVER
|
2020-06-01 17:00:56 +01:00
|
|
|
} else {
|
|
|
|
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, sensor_name, Bh1750_sensors[sensor_index].illuminance);
|
2017-11-04 15:36:51 +00:00
|
|
|
#endif // USE_WEBSERVER
|
2020-06-01 17:00:56 +01:00
|
|
|
}
|
2017-11-04 15:36:51 +00:00
|
|
|
}
|
2017-01-28 13:41:01 +00:00
|
|
|
}
|
|
|
|
}
|
2017-11-03 17:07:25 +00:00
|
|
|
|
|
|
|
/*********************************************************************************************\
|
|
|
|
* Interface
|
|
|
|
\*********************************************************************************************/
|
|
|
|
|
2022-11-11 09:44:56 +00:00
|
|
|
bool Xsns10(uint32_t function) {
|
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-06-01 17:00:56 +01:00
|
|
|
else if (Bh1750.count) {
|
2019-11-11 16:32:44 +00:00
|
|
|
switch (function) {
|
|
|
|
case FUNC_EVERY_SECOND:
|
|
|
|
Bh1750EverySecond();
|
|
|
|
break;
|
2020-06-01 17:00:56 +01:00
|
|
|
case FUNC_COMMAND:
|
|
|
|
result = DecodeCommand(kBh1750Commands, Bh1750Command);
|
2020-04-01 15:46:12 +01:00
|
|
|
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
|