Reject failed HDC10xx I2C reads

This commit is contained in:
Firat17aze committed 2026-10-09 15:30:33 -04:00
1 parent d4efae2f20
commit d14fc2f7a9
2 files changed
+16 -4

No files matched your search

+8
View File
@@ -38,6 +38,14 @@ Supported hardware
.. .. include:: P072_events.repl
Reading errors
--------------
If selecting a sensor register fails, or the sensor returns fewer than two bytes
for temperature or humidity, both values are set to ``nan``. The failed reading
is not sent to controllers such as MQTT. The next scheduled reading retries the
sensor.
Datasheets
----------
+8 -4
View File
@@ -6,6 +6,7 @@
// ######################################################################################################
/** Changelog:
* 2026-10-09: Reject failed or short I2C reads instead of reporting maximum sensor values
* 2025-01-04 tonhuisman: Send initialization (resolution setting) only once during INIT, small code optimizations
* 2023-02-09 tonhuisman: Fix typo in temperature calculation (was 65526.0f instead of 65536.0f (2^16))
* 2023-02-08 tonhuisman: Add PLUGIN_I2C_GET_ADDRESS support
@@ -89,19 +90,22 @@ boolean Plugin_072(uint8_t function, struct EventStruct *event, String& string)
uint16_t hdc1080_rawtemp, hdc1080_rawhum;
float hdc1080_temp, hdc1080_hum;
UserVar.setFloat(event->TaskIndex, 0, NAN);
UserVar.setFloat(event->TaskIndex, 1, NAN);
Wire.beginTransmission(HDC1080_I2C_ADDRESS); // start transmission to device
Wire.write(0x00); // sends HDC1080_TEMPERATURE
Wire.endTransmission(); // end transmission
if (Wire.endTransmission() != 0) { break; } // end transmission
delay(9);
Wire.requestFrom(HDC1080_I2C_ADDRESS, 2); // read 2 bytes for temperature
if (Wire.requestFrom(HDC1080_I2C_ADDRESS, 2) != 2) { break; } // read 2 bytes for temperature
hdc1080_rawtemp = Wire.read() << 8 | Wire.read();
hdc1080_temp = (static_cast<float>(hdc1080_rawtemp) / 65536.0f) * 165.0f - 40.0f;
Wire.beginTransmission(HDC1080_I2C_ADDRESS); // start transmission to device
Wire.write(0x01); // sends HDC1080_HUMIDITY
Wire.endTransmission(); // end transmission
if (Wire.endTransmission() != 0) { break; } // end transmission
delay(9);
Wire.requestFrom(HDC1080_I2C_ADDRESS, 2); // read 2 bytes for humidity
if (Wire.requestFrom(HDC1080_I2C_ADDRESS, 2) != 2) { break; } // read 2 bytes for humidity
hdc1080_rawhum = Wire.read() << 8 | Wire.read();
hdc1080_hum = (static_cast<float>(hdc1080_rawhum) / 65536.0f) * 100.0f;