winlinvip / SimpleDHT

Simple, Stable and Fast Arduino Temp & Humidity Sensors for DHT11 and DHT22. http://learn.adafruit.com/dht
MIT License
145 stars 61 forks source link

First read always fail with error 101 #13

Closed N0ury closed 7 years ago

N0ury commented 7 years ago

Hi,

I'm using code from sample with DHT22:

if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000); return; }

It always fails the first time. It's annoying because I use esp8266 in deepmode. So the code, as it's executed after reset, is always in a first execution.

I've changed my code to double the read with a delay before the second:

if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { delay(1000); if ((err = dht22.read2(pinDHT22, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Serial.print("Read DHT22 failed, err="); Serial.println(err);delay(2000); return; } }

It works but it's ugly and takes more time (more power is consumed). What can I do else ? I don't have a DHT11 to test.

Thanks in advance.

Noury

winlinvip commented 7 years ago

Why does this happend?

N0ury commented 7 years ago

I don't know why this happens. That's why I need help.

N0ury commented 7 years ago

I have lookup more deeply with a logical analyzer. The issue happens at startup only, not after every reset. The esp8266 starts too fast. It sends the start signal (low), but the dht22 doesn't reply. It's not ready to do this. A simple delay after power up solves the problem. One second is not sufficient, two is better. The library is not involved at all.

winlinvip commented 7 years ago

👍 Thanks for your work.