From 29ed5c1a27cfe1dd8e3a3134eb6865f8426d6031 Mon Sep 17 00:00:00 2001 From: Theo Arends Date: Wed, 11 Jul 2018 15:35:12 +0200 Subject: [PATCH] Update sensor drivers Update sensor drivers --- sonoff/xsns_07_sht1x.ino | 3 ++- sonoff/xsns_08_htu21.ino | 3 ++- sonoff/xsns_10_bh1750.ino | 50 +++++++++++++++++++++++++++++---------- 3 files changed, 41 insertions(+), 15 deletions(-) diff --git a/sonoff/xsns_07_sht1x.ino b/sonoff/xsns_07_sht1x.ino index 40a76e7a8..2c0099c9b 100644 --- a/sonoff/xsns_07_sht1x.ino +++ b/sonoff/xsns_07_sht1x.ino @@ -175,13 +175,14 @@ void ShtEverySecond() if (sht_type && !(uptime %3)) { // Update every 3 seconds if (!ShtRead()) { AddLogMissed(sht_types, sht_valid); +// if (!sht_valid) { sht_type = 0; } } } } void ShtShow(boolean json) { - if (sht_type && sht_valid) { + if (sht_valid) { char temperature[10]; char humidity[10]; diff --git a/sonoff/xsns_08_htu21.ino b/sonoff/xsns_08_htu21.ino index ecb127dc6..0f3cfc735 100644 --- a/sonoff/xsns_08_htu21.ino +++ b/sonoff/xsns_08_htu21.ino @@ -234,6 +234,7 @@ void HtuEverySecond() if (htu_type) { if (!HtuRead()) { AddLogMissed(htu_types, htu_valid); +// if (!htu_valid) { htu_type = 0; } } } } @@ -241,7 +242,7 @@ void HtuEverySecond() void HtuShow(boolean json) { - if (htu_type && htu_valid) { + if (htu_valid) { char temperature[10]; char humidity[10]; diff --git a/sonoff/xsns_10_bh1750.ino b/sonoff/xsns_10_bh1750.ino index 9c02f02e0..aa2cee37d 100644 --- a/sonoff/xsns_10_bh1750.ino +++ b/sonoff/xsns_10_bh1750.ino @@ -33,14 +33,20 @@ 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"; -uint16_t Bh1750ReadLux() +bool Bh1750Read() { - Wire.requestFrom(bh1750_address, (uint8_t)2); + if (bh1750_valid) { bh1750_valid--; } + + if (2 != Wire.requestFrom(bh1750_address, (uint8_t)2)) { return false; } byte msb = Wire.read(); byte lsb = Wire.read(); - uint16_t value = ((msb << 8) | lsb) / 1.2; - return value; + bh1750_illuminance = ((msb << 8) | lsb) / 1.2; + bh1750_valid = SENSOR_MAX_MISS; + return true; } /********************************************************************************************/ @@ -57,31 +63,46 @@ void Bh1750Detect() Wire.write(BH1750_CONTINUOUS_HIGH_RES_MODE); if (!Wire.endTransmission()) { bh1750_type = 1; - snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, "BH1750", bh1750_address); + snprintf_P(log_data, sizeof(log_data), S_LOG_I2C_FOUND_AT, bh1750_types, bh1750_address); AddLog(LOG_LEVEL_DEBUG); break; } } } +void Bh1750EverySecond() +{ + if (90 == (uptime %100)) { + Bh1750Detect(); + } + else if (uptime &1) { + if (bh1750_type) { + if (!Bh1750Read()) { + AddLogMissed(bh1750_types, bh1750_valid); +// if (!bh1750_valid) { bh1750_type = 0; } + } + } + } +} + #ifdef USE_WEBSERVER const char HTTP_SNS_ILLUMINANCE[] PROGMEM = - "%s{s}BH1750 " D_ILLUMINANCE "{m}%d " D_UNIT_LUX "{e}"; // {s} = , {m} = , {e} = + "%s{s}%s " D_ILLUMINANCE "{m}%d " D_UNIT_LUX "{e}"; // {s} = , {m} = , {e} = #endif // USE_WEBSERVER void Bh1750Show(boolean json) { - if (bh1750_type) { - uint16_t illuminance = Bh1750ReadLux(); - + if (bh1750_valid) { if (json) { - snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"BH1750\":{\"" D_JSON_ILLUMINANCE "\":%d}"), mqtt_data, illuminance); + snprintf_P(mqtt_data, sizeof(mqtt_data), PSTR("%s,\"%s\":{\"" D_JSON_ILLUMINANCE "\":%d}"), mqtt_data, bh1750_types, bh1750_illuminance); #ifdef USE_DOMOTICZ - if (0 == tele_period) DomoticzSensor(DZ_ILLUMINANCE, illuminance); + if (0 == tele_period) { + DomoticzSensor(DZ_ILLUMINANCE, bh1750_illuminance); + } #endif // USE_DOMOTICZ #ifdef USE_WEBSERVER } else { - snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ILLUMINANCE, mqtt_data, illuminance); + snprintf_P(mqtt_data, sizeof(mqtt_data), HTTP_SNS_ILLUMINANCE, mqtt_data, bh1750_types, bh1750_illuminance); #endif // USE_WEBSERVER } } @@ -99,9 +120,12 @@ boolean Xsns10(byte function) if (i2c_flg) { switch (function) { - case FUNC_PREP_BEFORE_TELEPERIOD: + case FUNC_INIT: Bh1750Detect(); break; + case FUNC_EVERY_SECOND: + Bh1750EverySecond(); + break; case FUNC_JSON_APPEND: Bh1750Show(1); break;