diff --git a/RELEASENOTES.md b/RELEASENOTES.md index 864a562e1..44162fb7d 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -65,6 +65,7 @@ The following binary downloads have been compiled with ESP8266/Arduino library c - Add Zigbee command ``ZbBindState`` and ``manuf``attribute - 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 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 0b4f503bd..ab18b9a3e 100644 --- a/tasmota/CHANGELOG.md +++ b/tasmota/CHANGELOG.md @@ -9,6 +9,7 @@ - Add Zigbee command ``ZbBindState`` and ``manuf``attribute - 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) ### 8.2.0.2 20200328 diff --git a/tasmota/settings.h b/tasmota/settings.h index 18bd7f1c6..44c2e061a 100644 --- a/tasmota/settings.h +++ b/tasmota/settings.h @@ -205,8 +205,7 @@ typedef union { uint8_t spare1 : 1; uint8_t spare2 : 1; uint8_t spare3 : 1; - uint8_t spare4 : 1; - uint8_t spare5 : 1; + uint8_t bh1750_resolution : 2; // Sensor10 1,2,3 uint8_t hx711_json_weight_change : 1; // Sensor34 8,x - Enable JSON message on weight change uint8_t mhz19b_abc_disable : 1; // Disable ABC (Automatic Baseline Correction for MHZ19(B) (0 = Enabled (default), 1 = Disabled with Sensor15 command) }; diff --git a/tasmota/xsns_10_bh1750.ino b/tasmota/xsns_10_bh1750.ino index 044089368..47bc6953a 100644 --- a/tasmota/xsns_10_bh1750.ino +++ b/tasmota/xsns_10_bh1750.ino @@ -31,24 +31,41 @@ #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. +#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. -uint8_t bh1750_address; -uint8_t bh1750_addresses[] = { BH1750_ADDR1, BH1750_ADDR2 }; -uint8_t bh1750_type = 0; -uint8_t bh1750_valid = 0; -uint16_t bh1750_illuminance = 0; -char bh1750_types[] = "BH1750"; +const char kBhMessages[] PROGMEM = "Resolution High|Resolution High2|Resolution Low"; + +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; + 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()); +} bool Bh1750Read(void) { - if (bh1750_valid) { bh1750_valid--; } + if (Bh1750.valid) { Bh1750.valid--; } - if (2 != Wire.requestFrom(bh1750_address, (uint8_t)2)) { return false; } - uint8_t msb = Wire.read(); - uint8_t lsb = Wire.read(); - bh1750_illuminance = ((msb << 8) | lsb) / 1.2; - bh1750_valid = SENSOR_MAX_MISS; + 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.valid = SENSOR_MAX_MISS; return true; } @@ -56,14 +73,13 @@ bool Bh1750Read(void) void Bh1750Detect(void) { - for (uint32_t i = 0; i < sizeof(bh1750_addresses); i++) { - bh1750_address = bh1750_addresses[i]; - if (I2cActive(bh1750_address)) { continue; } - Wire.beginTransmission(bh1750_address); - Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE); - if (!Wire.endTransmission()) { - I2cSetActiveFound(bh1750_address, bh1750_types); - bh1750_type = 1; + for (uint32_t i = 0; i < sizeof(Bh1750.addresses); i++) { + Bh1750.address = Bh1750.addresses[i]; + if (I2cActive(Bh1750.address)) { continue; } + + if (Bh1750SetResolution()) { + I2cSetActiveFound(Bh1750.address, Bh1750.types); + Bh1750.type = 1; break; } } @@ -73,23 +89,51 @@ void Bh1750EverySecond(void) { // 1mS if (!Bh1750Read()) { - AddLogMissed(bh1750_types, bh1750_valid); + AddLogMissed(Bh1750.types, Bh1750.valid); } } +/*********************************************************************************************\ + * Command Sensor10 + * + * 0 - High resolution mode (default) + * 1 - High resolution mode 2 + * 2 - Low resolution mode +\*********************************************************************************************/ + +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; + } + } + char res_text[64]; + Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_10, GetTextIndexed(res_text, sizeof(res_text), message_index, kBhMessages)); + + return true; +} + void Bh1750Show(bool json) { - if (bh1750_valid) { + if (Bh1750.valid) { if (json) { - ResponseAppend_P(JSON_SNS_ILLUMINANCE, bh1750_types, bh1750_illuminance); + ResponseAppend_P(JSON_SNS_ILLUMINANCE, Bh1750.types, Bh1750.illuminance); #ifdef USE_DOMOTICZ if (0 == tele_period) { - DomoticzSensor(DZ_ILLUMINANCE, bh1750_illuminance); + DomoticzSensor(DZ_ILLUMINANCE, Bh1750.illuminance); } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { - WSContentSend_PD(HTTP_SNS_ILLUMINANCE, bh1750_types, bh1750_illuminance); + WSContentSend_PD(HTTP_SNS_ILLUMINANCE, Bh1750.types, Bh1750.illuminance); #endif // USE_WEBSERVER } } @@ -108,11 +152,16 @@ bool Xsns10(uint8_t function) if (FUNC_INIT == function) { Bh1750Detect(); } - else if (bh1750_type) { + else if (Bh1750.type) { switch (function) { case FUNC_EVERY_SECOND: Bh1750EverySecond(); break; + case FUNC_COMMAND_SENSOR: + if (XSNS_10 == XdrvMailbox.index) { + result = Bh1750CommandSensor(); + } + break; case FUNC_JSON_APPEND: Bh1750Show(1); break;