Add BH1750 resolution control

Add command ``Sensor10 0/1/2`` to control BH1750 resolution - 0 = High (default), 1 = High2, 2 = Low (#8016)
This commit is contained in:
Theo Arends 2020-04-01 16:46:12 +02:00
parent caff54da7c
commit 4b09af8a36
4 changed files with 79 additions and 29 deletions

View File

@ -65,6 +65,7 @@ The following binary downloads have been compiled with ESP8266/Arduino library c
- Add Zigbee command ``ZbBindState`` and ``manuf``attribute - Add Zigbee command ``ZbBindState`` and ``manuf``attribute
- Add commands ``CounterDebounceLow`` and ``CounterDebounceHigh`` to control debouncing (#8021) - Add commands ``CounterDebounceLow`` and ``CounterDebounceHigh`` to control debouncing (#8021)
- Add command ``SetOption90 1`` to disable non-json MQTT messages (#8044) - 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 unreachable (unplugged) Zigbee devices in Philips Hue emulation and Alexa
- Add support for 64x48 SSD1306 OLED (#6740) - Add support for 64x48 SSD1306 OLED (#6740)
- Add support for up to four MQTT GroupTopics using the same optional Device Group names (#8014) - Add support for up to four MQTT GroupTopics using the same optional Device Group names (#8014)

View File

@ -9,6 +9,7 @@
- Add Zigbee command ``ZbBindState`` and ``manuf``attribute - Add Zigbee command ``ZbBindState`` and ``manuf``attribute
- Add commands ``CounterDebounceLow`` and ``CounterDebounceHigh`` to control debouncing (#8021) - Add commands ``CounterDebounceLow`` and ``CounterDebounceHigh`` to control debouncing (#8021)
- Add command ``SetOption90 1`` to disable non-json MQTT messages (#8044) - 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 ### 8.2.0.2 20200328

View File

@ -205,8 +205,7 @@ typedef union {
uint8_t spare1 : 1; uint8_t spare1 : 1;
uint8_t spare2 : 1; uint8_t spare2 : 1;
uint8_t spare3 : 1; uint8_t spare3 : 1;
uint8_t spare4 : 1; uint8_t bh1750_resolution : 2; // Sensor10 1,2,3
uint8_t spare5 : 1;
uint8_t hx711_json_weight_change : 1; // Sensor34 8,x - Enable JSON message on weight change 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) uint8_t mhz19b_abc_disable : 1; // Disable ABC (Automatic Baseline Correction for MHZ19(B) (0 = Enabled (default), 1 = Disabled with Sensor15 command)
}; };

View File

@ -31,24 +31,41 @@
#define BH1750_ADDR1 0x23 #define BH1750_ADDR1 0x23
#define BH1750_ADDR2 0x5C #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; const char kBhMessages[] PROGMEM = "Resolution High|Resolution High2|Resolution Low";
uint8_t bh1750_addresses[] = { BH1750_ADDR1, BH1750_ADDR2 };
uint8_t bh1750_type = 0; struct BH1750DATA {
uint8_t bh1750_valid = 0; uint8_t address;
uint16_t bh1750_illuminance = 0; uint8_t addresses[2] = { BH1750_ADDR1, BH1750_ADDR2 };
char bh1750_types[] = "BH1750"; 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) bool Bh1750Read(void)
{ {
if (bh1750_valid) { bh1750_valid--; } if (Bh1750.valid) { Bh1750.valid--; }
if (2 != Wire.requestFrom(bh1750_address, (uint8_t)2)) { return false; } if (2 != Wire.requestFrom(Bh1750.address, (uint8_t)2)) { return false; }
uint8_t msb = Wire.read(); Bh1750.illuminance = (Wire.read() << 8) | Wire.read();
uint8_t lsb = Wire.read(); if (1 == Settings.SensorBits1.bh1750_resolution) { Bh1750.illuminance >>= 1; }
bh1750_illuminance = ((msb << 8) | lsb) / 1.2; Bh1750.illuminance /= 1.2;
bh1750_valid = SENSOR_MAX_MISS;
Bh1750.valid = SENSOR_MAX_MISS;
return true; return true;
} }
@ -56,14 +73,13 @@ bool Bh1750Read(void)
void Bh1750Detect(void) void Bh1750Detect(void)
{ {
for (uint32_t i = 0; i < sizeof(bh1750_addresses); i++) { for (uint32_t i = 0; i < sizeof(Bh1750.addresses); i++) {
bh1750_address = bh1750_addresses[i]; Bh1750.address = Bh1750.addresses[i];
if (I2cActive(bh1750_address)) { continue; } if (I2cActive(Bh1750.address)) { continue; }
Wire.beginTransmission(bh1750_address);
Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE); if (Bh1750SetResolution()) {
if (!Wire.endTransmission()) { I2cSetActiveFound(Bh1750.address, Bh1750.types);
I2cSetActiveFound(bh1750_address, bh1750_types); Bh1750.type = 1;
bh1750_type = 1;
break; break;
} }
} }
@ -73,23 +89,51 @@ void Bh1750EverySecond(void)
{ {
// 1mS // 1mS
if (!Bh1750Read()) { 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) void Bh1750Show(bool json)
{ {
if (bh1750_valid) { if (Bh1750.valid) {
if (json) { if (json) {
ResponseAppend_P(JSON_SNS_ILLUMINANCE, bh1750_types, bh1750_illuminance); ResponseAppend_P(JSON_SNS_ILLUMINANCE, Bh1750.types, Bh1750.illuminance);
#ifdef USE_DOMOTICZ #ifdef USE_DOMOTICZ
if (0 == tele_period) { if (0 == tele_period) {
DomoticzSensor(DZ_ILLUMINANCE, bh1750_illuminance); DomoticzSensor(DZ_ILLUMINANCE, Bh1750.illuminance);
} }
#endif // USE_DOMOTICZ #endif // USE_DOMOTICZ
#ifdef USE_WEBSERVER #ifdef USE_WEBSERVER
} else { } else {
WSContentSend_PD(HTTP_SNS_ILLUMINANCE, bh1750_types, bh1750_illuminance); WSContentSend_PD(HTTP_SNS_ILLUMINANCE, Bh1750.types, Bh1750.illuminance);
#endif // USE_WEBSERVER #endif // USE_WEBSERVER
} }
} }
@ -108,11 +152,16 @@ bool Xsns10(uint8_t function)
if (FUNC_INIT == function) { if (FUNC_INIT == function) {
Bh1750Detect(); Bh1750Detect();
} }
else if (bh1750_type) { else if (Bh1750.type) {
switch (function) { switch (function) {
case FUNC_EVERY_SECOND: case FUNC_EVERY_SECOND:
Bh1750EverySecond(); Bh1750EverySecond();
break; break;
case FUNC_COMMAND_SENSOR:
if (XSNS_10 == XdrvMailbox.index) {
result = Bh1750CommandSensor();
}
break;
case FUNC_JSON_APPEND: case FUNC_JSON_APPEND:
Bh1750Show(1); Bh1750Show(1);
break; break;