From 486fd638139eb69375013378805e8dd2806ebcc4 Mon Sep 17 00:00:00 2001 From: Jon Date: Fri, 10 Jul 2020 02:35:10 +0900 Subject: [PATCH] Ahtx0 detections (#695) * Detect when sensor isn't available or read fails * Matching code guidelines Co-authored-by: Jonathan Michael Wells --- main/ZsensorAHTx0.ino | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/main/ZsensorAHTx0.ino b/main/ZsensorAHTx0.ino index f102420..e19e76d 100644 --- a/main/ZsensorAHTx0.ino +++ b/main/ZsensorAHTx0.ino @@ -56,9 +56,13 @@ void setupZsensorAHTx0() { # if defined(ESP32) Wire.begin(AHT_I2C_SDA, AHT_I2C_SCL); - ahtSensor.begin(&Wire); + if (!ahtSensor.begin(&Wire)) { + Log.error(F("Failed to initialize AHTx0 sensor!" CR)); + } # else - ahtSensor.begin(); + if (!ahtSensor.begin()) { + Log.error(F("Failed to initialize AHTx0 sensor!" CR)); + } # endif } @@ -71,9 +75,8 @@ void MeasureAHTTempHum() { static float persisted_aht_hum; sensors_event_t ahtTempC, ahtHum; - ahtSensor.getEvent(&ahtHum, &ahtTempC); // get sensor data - - if (ahtTempC.temperature >= 998 || ahtHum.relative_humidity >= 998) { + if (!ahtSensor.getEvent(&ahtHum, &ahtTempC)) // get sensor data + { Log.error(F("Failed to read from sensor AHTx0!" CR)); return; } @@ -101,8 +104,9 @@ void MeasureAHTTempHum() { Log.notice(F("Same Humidity. Don't send it" CR)); } - if (AHTx0data.size() > 0) + if (AHTx0data.size() > 0) { pub(AHTTOPIC, AHTx0data); + } } persisted_aht_tempc = ahtTempC.temperature; persisted_aht_hum = ahtHum.relative_humidity;