szazo / DHT11_Python

Pure Python library for reading DHT11 sensor on Raspberry Pi
MIT License
296 stars 225 forks source link

This program can't return correct temperature data when sub-zero temperature. #21

Open NaohikoYamaguchi opened 1 year ago

NaohikoYamaguchi commented 1 year ago

Hi,

I am Naohiko Yamaguchi from Japan.

Perhaps, this program can't return correct temperature data when sub-zero temperature.

Current code for calculate Temperature/Humidity is below.

 temperature = the_bytes[2] + float(the_bytes[3]) / 10
 humidity = the_bytes[0] + float(the_bytes[1]) / 10

DHT11 will return 1 in MSB of the_bytes[3] when sub-zero temperature. However, current code is not support this specific.

Otherwise, am I wrong?

DavidBazout commented 1 year ago

Hello, your thought is correct. For measuring sub-zero temperatures, these lines of code should be added.

    # DHT11 will return 1 in MSB of the_bytes[3] when sub-zero temperature.
    if not bits[24]:
        temperature = the_bytes[2] + float(the_bytes[3]) / 10

    else:
        temperature = 0.0 - the_bytes[2] - float(the_bytes[3] - 128) / 10
NaohikoYamaguchi commented 1 year ago

Hello, DavidBazout! Thank you for your response! I understood.