Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect temperature value below 0 C with DHT22 sensor #2638

Open
djsomi opened this issue Feb 19, 2025 · 1 comment
Open

Incorrect temperature value below 0 C with DHT22 sensor #2638

djsomi opened this issue Feb 19, 2025 · 1 comment
Labels

Comments

@djsomi
Copy link

djsomi commented Feb 19, 2025

Device

Sonoff POW R2

Version

ESPURNA 1.19.0 built 2025-02-18 20:05:14

Bug description

I get invalid data from DHT22 if temperature under 0 C.

404.6°C shows when temperature is -4.6°C.

What can be the problem, and how to fix it?

Steps to reproduce

No response

Build tools used

PlatformIO, VScode

Any relevant log output (when available)

Decoded stack trace (when available)

@djsomi djsomi added the bug label Feb 19, 2025
@mcspr
Copy link
Collaborator

mcspr commented Feb 20, 2025

Looks like the problem is with the number representation (encoding) in memory. Current code expects one thing, while the device returns something completely different when number is negative.

Some references
adafruit/DHT-sensor-library#182
arendst/Tasmota#14165
arendst/Tasmota#14173
https://en.wikipedia.org/wiki/Signed_number_representations#Sign%E2%80%93magnitude

Sensor code has to
a) prefer s16 in initial conversion
b) check hardware temperature limits for the sake of sanity
c) detect sign-magnitude and swap bits for dht22s like yours in this block

} else {
_temperature = (dhtData[2] & 0x7F) * 256 + dhtData[3];
_temperature /= 10;
if (dhtData[2] & 0x80) {
_temperature *= -1;
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants