iot: Dht22 example doesn't work
I have DHT22 sensors which works normally. I checked that by the default python script from Adafriut. I made a decision to give .net core on rpi platform a try with a simple example from the examples folder. I used Dhtxx example but it doesn’t work.
This is my code:
using System;
using System.Threading;
using Iot.Device.DHTxx;
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello DHT!");
using (DhtSensor dht = new DhtSensor(26, DhtType.Dht22))
{
while (true)
{
Console.WriteLine($"Temperature: {dht.Temperature.Celsius} °C, Humidity: {dht.Humidity} %");
Thread.Sleep(2000);
}
}
}
}
Project file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.2</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="IoT.Device.Bindings" Version="0.1.0-prerelease.19251.2" />
<PackageReference Include="System.Device.Gpio" Version="0.1.0-prerelease.19251.2" />
</ItemGroup>
</Project>
I wasn’t able to install System.Device.Gpio and IoT.Device.Bindings by using commands from the main readme.md so I just used the nearest proposed version by dotnet add.
I installed .net core on the rpi by using commands:
sudo apt-get install curl libunwind8 gettext
wget https://download.visualstudio.microsoft.com/download/pr/9650e3a6-0399-4330-a363-1add761127f9/14d80726c16d0e3d36db2ee5c11928e4/dotnet-sdk-2.2.102-linux-arm.tar.gz
wget https://download.visualstudio.microsoft.com/download/pr/9d049226-1f28-4d3d-a4ff-314e56b223c5/f67ab05a3d70b2bff46ff25e2b3acd2a/aspnetcore-runtime-2.2.1-linux-arm.tar.gz
mkdir -p $HOME/dotnet && tar zxf dotnet-sdk-2.2.102-linux-arm.tar.gz -C $HOME/dotnet
export DOTNET_ROOT=$HOME/dotnet
export PATH=$PATH:$HOME/dotnet
tar zxf aspnetcore-runtime-2.2.1-linux-arm.tar.gz -C $HOME/dotnet
On top of that I wasn’t able to run it on rpi without dotnet publish -r linux-arm since I get FileNotFoundExceptions and runtime isn’t able to find System.Device.Gpio.
The result of the program:
pi@raspberrypi:~/publish $ dotnet test_dht22.dll
Hello DHT!
Temperature: NaN °C, Humidity: NaN %
Temperature: NaN °C, Humidity: NaN %
Temperature: NaN °C, Humidity: NaN %
I tried other library versions and sometimes got values like 123e-308.
Do I do something wrong or the project isn’t ready yet for real usage (only people with deep .net knowledge can use it)?
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 32 (24 by maintainers)
@joperezr Very happy to help out so please let me know if there is anything I can do! Im not 100% confident that the PR will solve the issue totally, think it will solve for the incorrect measurements when data is returned but cant see anything that will solve the reliability of data being returned.
I looked into the source code and found the following issue,
Reading the bytes from the DHT22 seems to be correct but translating the bytes to humidity/temperature seems to be the issue.
DhtSensor.cs line 297 results in a temperature of 1.3°.
When i replace the formula with the following code, the result of the temperature is 25.9°. Which is correct.
The same issue with humidity. DhtSensor.cs line 305 results in a humidity of 9.9%.
The following code gives me a humidity of 59.1% which seems correct.
I have made a pull request: https://github.com/dotnet/iot/pull/469