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

Dht22 failed to read. I can read it using Python to make sure that the connection is OK. #1153

Closed
bbhxwl opened this issue Aug 11, 2020 · 15 comments · Fixed by #1164
Closed
Labels
bug Something isn't working

Comments

@bbhxwl
Copy link

bbhxwl commented Aug 11, 2020

Dht22 failed to read. I can read it using Python to make sure that the connection is OK.

using (Iot.Device.DHTxx.Dht22 dht = new Iot.Device.DHTxx.Dht22(26, PinNumberingScheme.Logical))
{
while (true)
{
Console.WriteLine($"Temperature: {dht.Temperature.Celsius.ToString("0.0")} ℃, Humidity: { dht.Humidity} %");
Thread.Sleep(1000);
}
}

@bbhxwl bbhxwl added the bug Something isn't working label Aug 11, 2020
@pgrawehr
Copy link
Contributor

What is rhe error you get? I have no Dht22, but I know the Dht11 works fine.

@bbhxwl
Copy link
Author

bbhxwl commented Aug 12, 2020

What is rhe error you get? I have no Dht22, but I know the Dht11 works fine.

I have also tested DHT11, but I can't get any data. Nan, I don't know why.

@Ellerbach
Copy link
Member

Which hardware platform are you using? Which OS?
DHT sensors are very sensitive, depending on the hardware/OS, the managed driver may not work perfectly.

@sokiha
Copy link

sokiha commented Aug 22, 2020

Im having the same problem with all values on a DHT11 sensor being NaN. Im using a CrowPi(Raspberry pi 3 b+) as hardware platform on Raspbian operating system.
I have verified with one of my old projects running .Net Core 3.0 and version 0.1.0-prerelease.19362.4 of the Iot.Device.Bindings package. In this project I get correct temperature and humidity values.
In my new project running .NET Core 5.0 Preview7 and version 1.1.0-prerelease.20276.1 of Iot.Device.Bindings I never get a successful reading with the same code used as in the old project.

@pgrawehr
Copy link
Contributor

Maybe related to #1145?
@sokiha Can you make sure the RaspberryPi3Driver is being used (by explicitly instantiating it)? The fallback SysFs driver may be to slow for the DHT sensor to work.

@bbhxwl
Copy link
Author

bbhxwl commented Aug 22, 2020

Im having the same problem with all values on a DHT11 sensor being NaN. Im using a CrowPi(Raspberry pi 3 b+) as hardware platform on Raspbian operating system.
I have verified with one of my old projects running .Net Core 3.0 and version 0.1.0-prerelease.19362.4 of the Iot.Device.Bindings package. In this project I get correct temperature and humidity values.
In my new project running .NET Core 5.0 Preview7 and version 1.1.0-prerelease.20276.1 of Iot.Device.Bindings I never get a successful reading with the same code used as in the old project.

You are the same as me. I use Python code to make sure that dht22 and DHT11 can be read normally, but if you use net core, you can't read it. They are both Nan

@bbhxwl
Copy link
Author

bbhxwl commented Aug 22, 2020

Maybe related to #1145?
@sokiha Can you make sure the RaspberryPi3Driver is being used (by explicitly instantiating it)? The fallback SysFs driver may be to slow for the DHT sensor to work.

It may be because of the latest system problems, but there are many answers. My English is not good. I don't know which is the correct solution. Can you tell me?

@sokiha
Copy link

sokiha commented Aug 22, 2020

I have got it working now again, both with and without explicitly instantiating RaspberryPi3Driver. It takes some second before the Dht returns correct data. The way to check if there is valid values is, in my opinion, a little bit cumbersome. Dht has a property called IsLastReadSuccessful, to see if last read was successful, but there is only one way to read data and this is to make a call to the Temperature property on the Dht11 object. The Dht11 Temperature device's get property returns

var temp = readBuff[2] + readBuff[3] * 0.1;
return Temperature.FromDegreesCelsius(temp);

The problem is that temp is double.NaN the first couple of seconds, so the last line throws an "System.ArgumentException: 'NaN is not a valid number.'" in the FromDegreesCelsius method without you can make any check for valid values other than wrap the call to the Dht.Temperature get property in a try/catch. In the old implementation before using the new Unit type, it would just return double.NaN and you could just ignore those values with a simple if check. It might also be a little confusing to have the IsLastReadSuccessful property public, unless you know the internals of the class. People could maybe think it was possible to poll on that property, and first make a call to the Temperature property when it was true, because the Temperature property throws an exception if the last read is not successful. But the IsLastReadSuccessful will always remain false until you make the call to the Temperature property. So the Dht11 is working again on my end, with some adjustments to my code.

@Ellerbach
Copy link
Member

@sokiha , good news. And I understand the issue, if the temperature is not correct, you'll get an exception.
For this sensor the Try... pattern has not been implemented. That may help to implement it.
Can you btw share the code you've adjusted to see where it is braking and how you solved it?

@bbhxwl
Copy link
Author

bbhxwl commented Aug 23, 2020

It doesn't seem to work out?

@sokiha
Copy link

sokiha commented Aug 23, 2020

I tested with creating the dht device in the following ways:

var dht = new Dht11(4, gpioController: new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver()));
var dht = new Dht11(4);

In my case both worked the same. I then made some code to read the temperature, this code may need to be called several times until dht.IsLastReadSuccessful is true, with some delay between each call. It could take 5-10 seconds maybe before it was stable, but I did'nt really time it.

try
{
	var temp = dht.Temperature;
}
catch(ArgumentException)
{
 // ignore
}

When it was up and running I used something like this

if (dht.IsLastReadSuccessful)
{
	var temperature = dht.Temperature.DegreesCelsius;
	var humidity = dht.Humidity;
}

But the get properties should probably always be wrapped in a try/catch as the IsLastReadSuccessful only account for the last call not the new one made by the call to the Temperature property. I haven't used this code for anyting serious, just some fun programming exercise with my nephew on the CrowPi.

@bbhxwl
Copy link
Author

bbhxwl commented Aug 23, 2020

I tested with creating the dht device in the following ways:

var dht = new Dht11(4, gpioController: new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver()));
var dht = new Dht11(4);

In my case both worked the same. I then made some code to read the temperature, this code may need to be called several times until dht.IsLastReadSuccessful is true, with some delay between each call. It could take 5-10 seconds maybe before it was stable, but I did'nt really time it.

try
{
	var temp = dht.Temperature;
}
catch(ArgumentException)
{
 // ignore
}

When it was up and running I used something like this

if (dht.IsLastReadSuccessful)
{
	var temperature = dht.Temperature.DegreesCelsius;
	var humidity = dht.Humidity;
}

But the get properties should probably always be wrapped in a try/catch as the IsLastReadSuccessful only account for the last call not the new one made by the call to the Temperature property. I haven't used this code for anyting serious, just some fun programming exercise with my nephew on the CrowPi.

It seems that the current bug is not suitable for development.

@bbhxwl
Copy link
Author

bbhxwl commented Aug 23, 2020

I tested with creating the dht device in the following ways:

var dht = new Dht11(4, gpioController: new GpioController(PinNumberingScheme.Logical, new RaspberryPi3Driver()));
var dht = new Dht11(4);

In my case both worked the same. I then made some code to read the temperature, this code may need to be called several times until dht.IsLastReadSuccessful is true, with some delay between each call. It could take 5-10 seconds maybe before it was stable, but I did'nt really time it.

try
{
	var temp = dht.Temperature;
}
catch(ArgumentException)
{
 // ignore
}

When it was up and running I used something like this

if (dht.IsLastReadSuccessful)
{
	var temperature = dht.Temperature.DegreesCelsius;
	var humidity = dht.Humidity;
}

But the get properties should probably always be wrapped in a try/catch as the IsLastReadSuccessful only account for the last call not the new one made by the call to the Temperature property. I haven't used this code for anyting serious, just some fun programming exercise with my nephew on the CrowPi.

    using (Iot.Device.DHTxx.Dht11 dht = new Iot.Device.DHTxx.Dht11(26, PinNumberingScheme.Logical))
        {
            while (true)
            {
                var vv = dht.Temperature;
                var vvv = dht.Humidity;
                if (dht.IsLastReadSuccessful)
                {
                    Console.WriteLine($"Temperature: {vv.Celsius.ToString("0.0")} ℃, Humidity: { vvv} %");
                    Thread.Sleep(1000);
                }
                
            }
        }

QQ图片20200811015151

@bbhxwl
Copy link
Author

bbhxwl commented Aug 23, 2020

Dht22 acquisition often fails, but when the temperature is obtained, it is also wrong. Why?

Temperature: NaN ℃, Humidity: 76.3 %
Temperature: 3.6 ℃, Humidity: 76.3 %
Temperature: 3.6 ℃, Humidity: 76.3 %
Temperature: NaN ℃, Humidity: 74.9 %
Temperature: 3.6 ℃, Humidity: 76.4 %
Temperature: NaN ℃, Humidity: 75.9 %
Temperature: 3.5 ℃, Humidity: 76.3 %
Temperature: 3.6 ℃, Humidity: 74.9 %
Temperature: 3.6 ℃, Humidity: 75 %
Temperature: 3.6 ℃, Humidity: 76.5 %
Temperature: 3.5 ℃, Humidity: 76.10000000000001 %
Temperature: NaN ℃, Humidity: 76.2 %
Temperature: 3.5 ℃, Humidity: 76.80000000000001 %
Temperature: 3.5 ℃, Humidity: 76.80000000000001 %
Temperature: 3.4 ℃, Humidity: 76.3 %
Temperature: 3.4 ℃, Humidity: 75.2 %
Temperature: NaN ℃, Humidity: 76.3 %
Temperature: 3.3 ℃, Humidity: 75.2 %
Temperature: 3.3 ℃, Humidity: 76.3 %
Temperature: 3.3 ℃, Humidity: 76 %
Temperature: NaN ℃, Humidity: 76.60000000000001 %

@Ellerbach
Copy link
Member

Ellerbach commented Aug 24, 2020

Dht22 acquisition often fails, but when the temperature is obtained, it is also wrong. Why?

Seems like the calculation for DHT22 is wrong. Should be more like:

var temp = (((readBuff[2] & 0x7F) << 8) + readBuff[3] )* 0.1;
if (readBuff[2] & 0x80 == 0x80)
{
    temp = -temp;
}

Can anyone try? Not sure if I have the hardware to test it out.

@ghost ghost locked as resolved and limited conversation to collaborators Oct 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants