mirror of
https://github.com/arendst/Tasmota.git
synced 2025-07-24 03:06:33 +00:00
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:
parent
4b09af8a36
commit
49febe4a54
@ -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 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 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 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)
|
||||||
|
@ -10,6 +10,7 @@
|
|||||||
- 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 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
|
### 8.2.0.2 20200328
|
||||||
|
|
||||||
|
@ -25,17 +25,18 @@
|
|||||||
* I2C Address: 0x23 or 0x5C
|
* I2C Address: 0x23 or 0x5C
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
#define XSNS_10 10
|
#define XSNS_10 10
|
||||||
#define XI2C_11 11 // See I2CDEVICES.md
|
#define XI2C_11 11 // See I2CDEVICES.md
|
||||||
|
|
||||||
#define BH1750_ADDR1 0x23
|
#define BH1750_ADDR1 0x23
|
||||||
#define BH1750_ADDR2 0x5C
|
#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_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_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_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 {
|
struct BH1750DATA {
|
||||||
uint8_t address;
|
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 resolution[3] = { BH1750_CONTINUOUS_HIGH_RES_MODE, BH1750_CONTINUOUS_HIGH_RES_MODE2, BH1750_CONTINUOUS_LOW_RES_MODE };
|
||||||
uint8_t type = 0;
|
uint8_t type = 0;
|
||||||
uint8_t valid = 0;
|
uint8_t valid = 0;
|
||||||
|
uint8_t mtreg = 69; // Default Measurement Time
|
||||||
uint16_t illuminance = 0;
|
uint16_t illuminance = 0;
|
||||||
char types[7] = "BH1750";
|
char types[7] = "BH1750";
|
||||||
} Bh1750;
|
} Bh1750;
|
||||||
@ -56,14 +58,29 @@ bool Bh1750SetResolution(void)
|
|||||||
return (!Wire.endTransmission());
|
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)
|
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; }
|
||||||
Bh1750.illuminance = (Wire.read() << 8) | Wire.read();
|
Bh1750.illuminance = (Wire.read() << 8) | Wire.read();
|
||||||
if (1 == Settings.SensorBits1.bh1750_resolution) { Bh1750.illuminance >>= 1; }
|
Bh1750.illuminance /= (1.2 * (69 / Bh1750.mtreg));
|
||||||
Bh1750.illuminance /= 1.2;
|
if (1 == Settings.SensorBits1.bh1750_resolution) {
|
||||||
|
Bh1750.illuminance >>= 1;
|
||||||
|
}
|
||||||
|
|
||||||
Bh1750.valid = SENSOR_MAX_MISS;
|
Bh1750.valid = SENSOR_MAX_MISS;
|
||||||
return true;
|
return true;
|
||||||
@ -77,7 +94,7 @@ void Bh1750Detect(void)
|
|||||||
Bh1750.address = Bh1750.addresses[i];
|
Bh1750.address = Bh1750.addresses[i];
|
||||||
if (I2cActive(Bh1750.address)) { continue; }
|
if (I2cActive(Bh1750.address)) { continue; }
|
||||||
|
|
||||||
if (Bh1750SetResolution()) {
|
if (Bh1750SetMTreg()) {
|
||||||
I2cSetActiveFound(Bh1750.address, Bh1750.types);
|
I2cSetActiveFound(Bh1750.address, Bh1750.types);
|
||||||
Bh1750.type = 1;
|
Bh1750.type = 1;
|
||||||
break;
|
break;
|
||||||
@ -96,27 +113,25 @@ void Bh1750EverySecond(void)
|
|||||||
/*********************************************************************************************\
|
/*********************************************************************************************\
|
||||||
* Command Sensor10
|
* Command Sensor10
|
||||||
*
|
*
|
||||||
* 0 - High resolution mode (default)
|
* 0 - High resolution mode (default)
|
||||||
* 1 - High resolution mode 2
|
* 1 - High resolution mode 2
|
||||||
* 2 - Low resolution mode
|
* 2 - Low resolution mode
|
||||||
|
* 31..254 - Measurement Time value (not persistent, default is 69)
|
||||||
\*********************************************************************************************/
|
\*********************************************************************************************/
|
||||||
|
|
||||||
bool Bh1750CommandSensor(void)
|
bool Bh1750CommandSensor(void)
|
||||||
{
|
{
|
||||||
uint32_t message_index = Settings.SensorBits1.bh1750_resolution;
|
|
||||||
if (XdrvMailbox.data_len) {
|
if (XdrvMailbox.data_len) {
|
||||||
switch (XdrvMailbox.payload) {
|
if ((XdrvMailbox.payload >= 0) && (XdrvMailbox.payload <= 2)) {
|
||||||
case 0:
|
Settings.SensorBits1.bh1750_resolution = XdrvMailbox.payload;
|
||||||
case 1:
|
Bh1750SetResolution();
|
||||||
case 2:
|
}
|
||||||
Settings.SensorBits1.bh1750_resolution = XdrvMailbox.payload;
|
else if ((XdrvMailbox.payload > 30) && (XdrvMailbox.payload < 255)) {
|
||||||
Bh1750SetResolution();
|
Bh1750.mtreg = XdrvMailbox.payload;
|
||||||
message_index = Settings.SensorBits1.bh1750_resolution;
|
Bh1750SetMTreg();
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
char res_text[64];
|
Response_P(PSTR("{\"" D_CMND_SENSOR "10\":{\"Resolution\":%d,\"MTime\":%d}}"), Settings.SensorBits1.bh1750_resolution, Bh1750.mtreg);
|
||||||
Response_P(S_JSON_SENSOR_INDEX_SVALUE, XSNS_10, GetTextIndexed(res_text, sizeof(res_text), message_index, kBhMessages));
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user