Add BH1750 Measurement Time control

Add command ``Sensor10 31..254`` to control BH1750 measurement time which defaults to 69 (#8016)
This commit is contained in:
Theo Arends 2020-04-02 15:17:54 +02:00
parent 4b09af8a36
commit 49febe4a54
3 changed files with 42 additions and 25 deletions

View File

@ -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)

View File

@ -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

View File

@ -35,7 +35,8 @@
#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;
@ -99,24 +116,22 @@ void Bh1750EverySecond(void)
* 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:
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
Settings.SensorBits1.bh1750_resolution = XdrvMailbox.payload;
Bh1750SetResolution();
message_index = Settings.SensorBits1.bh1750_resolution;
break;
}
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;
}