mirror of
https://github.com/esphome/esphome.git
synced 2024-12-24 16:58:07 +01:00
Decode DHT11 decimal part (#861)
* Decode DHT11 decimal part * Fix comment * Fix comment * Handle negative temp for some DHT11 - code from the DHT12 component * Don't use the fractional part if the checksum is the 2 bytes one
This commit is contained in:
parent
ad76709d00
commit
b7b23ffdb2
@ -165,10 +165,24 @@ bool HOT DHT::read_sensor_(float *temperature, float *humidity, bool report_erro
|
||||
}
|
||||
|
||||
if (this->model_ == DHT_MODEL_DHT11) {
|
||||
*humidity = data[0];
|
||||
if (*humidity > 100)
|
||||
*humidity = NAN;
|
||||
*temperature = data[2];
|
||||
if (checksum_a == data[4]) {
|
||||
// Data format: 8bit integral RH data + 8bit decimal RH data + 8bit integral T data + 8bit decimal T data + 8bit
|
||||
// check sum - some models always have 0 in the decimal part
|
||||
const uint16_t raw_temperature = uint16_t(data[2]) * 10 + (data[3] & 0x7F);
|
||||
*temperature = raw_temperature / 10.0f;
|
||||
if ((data[3] & 0x80) != 0) {
|
||||
// negative
|
||||
*temperature *= -1;
|
||||
}
|
||||
|
||||
const uint16_t raw_humidity = uint16_t(data[0]) * 10 + data[1];
|
||||
*humidity = raw_humidity / 10.0f;
|
||||
} else {
|
||||
// For compatibily with DHT11 models which might only use 2 bytes checksums, only use the data from these two
|
||||
// bytes
|
||||
*temperature = data[2];
|
||||
*humidity = data[0];
|
||||
}
|
||||
} else {
|
||||
uint16_t raw_humidity = (uint16_t(data[0] & 0xFF) << 8) | (data[1] & 0xFF);
|
||||
uint16_t raw_temperature = (uint16_t(data[2] & 0xFF) << 8) | (data[3] & 0xFF);
|
||||
|
Loading…
Reference in New Issue
Block a user