Update sensor drivers

Update sensor drivers
This commit is contained in:
Theo Arends 2018-07-11 15:35:12 +02:00
parent 4bc3ce70dd
commit 29ed5c1a27
3 changed files with 41 additions and 15 deletions

View File

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

View File

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

View File

@ -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} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
"%s{s}%s " D_ILLUMINANCE "{m}%d " D_UNIT_LUX "{e}"; // {s} = <tr><th>, {m} = </th><td>, {e} = </td></tr>
#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;