dotnet / iot

This repo includes .NET Core implementations for various IoT boards, chips, displays and PCBs.
MIT License
2.15k stars 581 forks source link

DHT22 Wrong temperature readings. #984

Closed BrammyS closed 4 years ago

BrammyS commented 4 years ago

The temperature output dropped to to 1°C when the real temperature got above 26°C.

Steps to reproduce

using var dht = new Dht22(26);
while (true)
 {
         // Try to read the temperature.
         var temp = dht.Temperature;
         if (!dht.IsLastReadSuccessful) continue;

          // Try to read the humidity.
          var humidity = dht.Humidity;
          if (!dht.IsLastReadSuccessful) continue;

          Console.WriteLine($"Temperature: {temp.Celsius:0.0} °C, 'Humidity: {humidity:0.0} %");

          break;
}

Expected behavior

Output: Temperature: 26.0 °C, 'Humidity: 36.1 %

Actual behavior

Output: Temperature: 1.0 °C, 'Humidity: 36.1 %

Versions used

Add following information:

BrammyS commented 4 years ago

8a60d15c0ab975022315428a4098dcf6

I ran it with a small python script as well. And it resulted in the correct values.

BrammyS commented 4 years ago

I was running a Dht11 sensor before this, and i never had any problems with it. Except with the 2°C inaccuracy. :P

joperezr commented 4 years ago

I was running a Dht11 sensor before this, and i never had any problems with it.

So when you say ya were running it before, do you mean with python or with .net core? If you were doing it with .net core and it was working correctly, did you recompile or updated the nuget package to get a newer version?

cc: @Ellerbach @ZhangGaoxing as FYI.

BrammyS commented 4 years ago

@joperezr The Dht11 was almost running on the exact same code. Just replace the

using var dht = new Dht22(26);

With

using var dht = new Dht11(26);

I just remebered that i updated to .net core 3.1 as well. So the Dht11 was running on .net core 3.0 and the Dht22 is running on .net core 3.1. You can see all the changes here. I can try running the Dht11 on .net core 3.1 as well to see if the same thing will happen.

fomenkodmitry commented 4 years ago

Sustained, similar bug. You can see the code here: https://github.com/fomenkodmitry/NetCore-raspberrypi4-dht22 Platform: Rasberry pi 4 model b, net core 3.1, dht22

python3 ../../humidity.py 
Temp=26.3*C  Humidity=33.4%
Temp=26.3*C  Humidity=33.9%
Temp=26.3*C  Humidity=33.9%
dotnet run
Hello DHT!
Temperature: NaN°C, Humidity: NaN%
Temperature: NaN°C, Humidity: NaN%
Temperature: NaN°C, Humidity: NaN%
Temperature: 1.7°C, Humidity: 33.2%
Temperature: 1.7°C, Humidity: 33.3%
Ellerbach commented 4 years ago

@fomenkodmitry and @BramEsendam, you are right, there was a mistake. I found some time to dig into the code and found the problem. Line 32 in the Dht222.cs file should be:

var temp = ((readBuff[2] & 0x7F) * 256 + readBuff[3]) * 0.1;

@joperezr, will make a PR asap.

joperezr commented 4 years ago

I've merged that PR so hopefully the results will be fine now. I'll go ahead and close this issue assuming it does, but please feel free to reopen if that is not the case.