From 49febe4a54310b5e128a16403a2f769d1182a573 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Thu, 2 Apr 2020 15:17:54 +0200 Subject: [PATCH] Add BH1750 Measurement Time control Add command ``Sensor10 31..254`` to control BH1750 measurement time which defaults to 69 (#8016) --- RELEASENOTES.md | 1 + tasmota/CHANGELOG.md | 1 + tasmota/xsns_10_bh1750.ino | 65 +++++++++++++++++++++++--------------- 3 files changed, 42 insertions(+), 25 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 44162fb7d..ec242c079 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -66,6 +66,7 @@ The following binary downloads have been compiled with ESP8266/Arduino library c - Add commands ``CounterDebounceLow`` and ``CounterDebounceHigh`` to control debouncing (#8021) - Add command ``SetOption90 1`` to disable non-json MQTT messages (#8044) - Add command ``Sensor10 0/1/2`` to control BH1750 resolution - 0 = High (default), 1 = High2, 2 = Low (#8016) +- Add command ``Sensor10 31..254`` to control BH1750 measurement time which defaults to 69 (#8016) - Add support for unreachable (unplugged) Zigbee devices in Philips Hue emulation and Alexa - Add support for 64x48 SSD1306 OLED (#6740) - Add support for up to four MQTT GroupTopics using the same optional Device Group names (#8014) diff --git a/tasmota/CHANGELOG.md b/tasmota/CHANGELOG.md index ab18b9a3e..7dbbf3290 100644 --- a/tasmota/CHANGELOG.md +++ b/tasmota/CHANGELOG.md @@ -10,6 +10,7 @@ - Add commands ``CounterDebounceLow`` and ``CounterDebounceHigh`` to control debouncing (#8021) - Add command ``SetOption90 1`` to disable non-json MQTT messages (#8044) - Add command ``Sensor10 0/1/2`` to control BH1750 resolution - 0 = High (default), 1 = High2, 2 = Low (#8016) +- Add command ``Sensor10 31..254`` to control BH1750 measurement time which defaults to 69 (#8016) ### 8.2.0.2 20200328 diff --git a/tasmota/xsns_10_bh1750.ino b/tasmota/xsns_10_bh1750.ino index 47bc6953a..1ba00b46e 100644 --- a/tasmota/xsns_10_bh1750.ino +++ b/tasmota/xsns_10_bh1750.ino @@ -25,17 +25,18 @@ * I2C Address: 0x23 or 0x5C \*********************************************************************************************/ -#define XSNS_10 10 -#define XI2C_11 11 // See I2CDEVICES.md +#define XSNS_10 10 +#define XI2C_11 11 // See I2CDEVICES.md -#define BH1750_ADDR1 0x23 -#define BH1750_ADDR2 0x5C +#define BH1750_ADDR1 0x23 +#define BH1750_ADDR2 0x5C -#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. +#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. -const char kBhMessages[] PROGMEM = "Resolution High|Resolution High2|Resolution Low"; +#define BH1750_MEASUREMENT_TIME_HIGH 0x40 // Measurement Time register high 3 bits +#define BH1750_MEASUREMENT_TIME_LOW 0x60 // Measurement Time register low 5 bits struct BH1750DATA { uint8_t address; @@ -43,6 +44,7 @@ struct BH1750DATA { 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; + uint8_t mtreg = 69; // Default Measurement Time uint16_t illuminance = 0; char types[7] = "BH1750"; } Bh1750; @@ -56,14 +58,29 @@ bool Bh1750SetResolution(void) return (!Wire.endTransmission()); } +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(); +} + bool Bh1750Read(void) { if (Bh1750.valid) { Bh1750.valid--; } if (2 != Wire.requestFrom(Bh1750.address, (uint8_t)2)) { return false; } Bh1750.illuminance = (Wire.read() << 8) | Wire.read(); - if (1 == Settings.SensorBits1.bh1750_resolution) { Bh1750.illuminance >>= 1; } - Bh1750.illuminance /= 1.2; + Bh1750.illuminance /= (1.2 * (69 / Bh1750.mtreg)); + if (1 == Settings.SensorBits1.bh1750_resolution) { + Bh1750.illuminance >>= 1; + } Bh1750.valid = SENSOR_MAX_MISS; return true; @@ -77,7 +94,7 @@ void Bh1750Detect(void) Bh1750.address = Bh1750.addresses[i]; if (I2cActive(Bh1750.address)) { continue; } - if (Bh1750SetResolution()) { + if (Bh1750SetMTreg()) { I2cSetActiveFound(Bh1750.address, Bh1750.types); Bh1750.type = 1; break; @@ -96,27 +113,25 @@ void Bh1750EverySecond(void) /*********************************************************************************************\ * Command Sensor10 * - * 0 - High resolution mode (default) - * 1 - High resolution mode 2 - * 2 - Low resolution mode + * 0 - High resolution mode (default) + * 1 - High resolution mode 2 + * 2 - Low resolution mode + * 31..254 - Measurement Time value (not persistent, default is 69) \*********************************************************************************************/ bool Bh1750CommandSensor(void) { - uint32_t message_index = Settings.SensorBits1.bh1750_resolution; if (XdrvMailbox.data_len) { - switch (XdrvMailbox.payload) { - case 0: - case 1: - case 2: - Settings.SensorBits1.bh1750_resolution = XdrvMailbox.payload; - Bh1750SetResolution(); - message_index = Settings.SensorBits1.bh1750_resolution; - break; + 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(); } } - char res_text[64]; - Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_10, GetTextIndexed(res_text, sizeof(res_text), message_index, kBhMessages)); + Response_P(PSTR("{\"" D_CMND_SENSOR "10\":{\"Resolution\":%d,\"MTime\":%d}}"), Settings.SensorBits1.bh1750_resolution, Bh1750.mtreg); return true; }