[SOLVED/WORKAROUND]Unable to get DHT22-example to work

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
jonaslorander
Posts: 10
Joined: Tue Nov 08, 2016 12:33 pm

[SOLVED/WORKAROUND]Unable to get DHT22-example to work

Post by jonaslorander » Thu Nov 17, 2016 9:08 pm

Hi,

I'm trying to use this code on a NodeMCU board with ESP-12E:
import dht
import machine
import time
d = dht.DHT22(machine.Pin(4, machine.Pin.IN, machine.Pin.PULL_UP))
while True:
time.sleep(5)
d.measure()
d.temperature(), d.humidity()

But all I get is this error on the d.measure() line:
OSError: [Errno 110] ETIMEDOUT

I have got it to work once on my ESP, it was running fine for 30 minutes before I put it away for the day. The next day when I was starting to code again it did not work anymore. And havn't done since, and that is a couple of weeks ago.

I've testet in on both v1.8.3 and 1.8.6 with the same issues. I've tested four different inputs and three different DHT22 sensors (all three of them work flawlessly on a Raspberry Pi using Adafruits DHT-library for Python).

Have anyone got the DHT-library to work? When I search the forums all I see is people that have problems with the DHT but no real solutions. If you didn't get it to work, does anyone have any other library to use?

Thanks in advance!

Regards,
Jonas
Last edited by jonaslorander on Wed Dec 14, 2016 6:04 am, edited 1 time in total.

User avatar
Roberthh
Posts: 1729
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Unable to get DHT22-example to work

Post by Roberthh » Fri Nov 18, 2016 10:24 am

I tried this code sequence from the examples with an DHT22 compatible AM2302:

Code: Select all

d = dht.DHT22(machine.Pin(4))
d.measure()
d.temperature() # eg. 23.6 (°C)
d.humidity()    # eg. 41.3 (% RH)
But since it worked for you for a while, that might not help. Check whether you might have changed something else in your set-up.

jonaslorander
Posts: 10
Joined: Tue Nov 08, 2016 12:33 pm

Re: Unable to get DHT22-example to work

Post by jonaslorander » Fri Nov 18, 2016 7:03 pm

Thanks for you reply.
I tested a few other pins, and pin 13 seems to be working, for now.

So, at the moment, this works:

Code: Select all

import machine, dht
d = dht.DHT22(machine.Pin(13))
d.measure()
d.temperature(), d.humidity()
And then I tried pin 4 again, and it works too now. Pin 15 does not work.

Well well, lets hope it will work a bit longer now :)

jonaslorander
Posts: 10
Joined: Tue Nov 08, 2016 12:33 pm

Re: Unable to get DHT22-example to work

Post by jonaslorander » Fri Nov 18, 2016 8:36 pm

Ok, now this is interesting.
After adding the DHT code to my main program, that sends the values over MQTT, it stopped working again. If I ran the main.py on boot it would not work, but if I run import main it does...!

So I started playing around. And what I have now for this test, is this code in main.py:

Code: Select all

import dht
import machine
import time

print("Starting DHT22.")
d = dht.DHT22(machine.Pin(4)) #, machine.Pin.IN, machine.Pin.PULL_UP))
while True:
    time.sleep(5)
    print("Measuring.")
    d.measure()
    print("Temperature: %3.1f °C" % d.temperature())
    print("   Humidity: %3.2f %% RH" % d.humidity())
Running it on boot gives this output:

Code: Select all

▒#5 ets_task(40100164, 3, 3fff8398, 4)
Connecting to myapxxxxx
Network config: ('192.168.0.179', '255.255.255.0', '192.168.0.1', '192.168.0.1')
WebREPL daemon started on ws://192.168.4.1:8266
WebREPL daemon started on ws://192.168.0.179:8266
Started webrepl in normal mode
Starting DHT22.
Measuring.
Traceback (most recent call last):
  File "main.py", line 10, in <module>
  File "dht.py", line 13, in measure
OSError: [Errno 110] ETIMEDOUT

MicroPython v1.8.6-7-gefd0927 on 2016-11-10; ESP module with ESP8266
Type "help()" for more information.
>>> 
And running it with import main gives this output:

Code: Select all

>>> import main
Starting DHT22.
Measuring.
Temperature: 25.6 °C
   Humidity: 30.70 % RH
Measuring.
Temperature: 25.6 °C
   Humidity: 31.20 % RH
Measuring.
Temperature: 25.6 °C
   Humidity: 31.20 % RH
Measuring.
Temperature: 25.6 °C
   Humidity: 31.10 % RH
Measuring.
Temperature: 25.6 °C
   Humidity: 31.10 % RH
Measuring.
Temperature: 25.6 °C
   Humidity: 31.00 % RH
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "main.py", line 8, in <module>
KeyboardInterrupt:
>>>
What is going on here? Can anyone help me out here. I have no clue of why this behavior is there. I have tried with delays up to 10 seconds before calling the measure() method of the DHT, but no difference.

vikebo
Posts: 14
Joined: Sun Mar 15, 2015 8:15 pm
Location: Norway

Re: Unable to get DHT22-example to work

Post by vikebo » Tue Dec 13, 2016 10:28 pm

I tried the DHT11 example on a Wemos D1 mini and got the same timeout error. Found out the silkscreen for D2 and D4 had switched places, when connecting the sensor to the correct pin it works.

No idea why your results change when importing main. Have you tried to use the pin for something else than the DHT22 to see if you get the same results from boot and importing main? Maybe something simple like setting or clearing the pin can give a clue? What if you add a time.sleep() with a few seconds in boot.py before using the DHT22?

Another thing I noticed is that "tab-completion" and "help" doesn't mention measure:
>>> d.<TAB>
__module__ humidity temperature __qualname__

v1.8.6-7-gefd0927

jonaslorander
Posts: 10
Joined: Tue Nov 08, 2016 12:33 pm

Re: Unable to get DHT22-example to work

Post by jonaslorander » Wed Dec 14, 2016 5:52 am

I've made a few more discoveries, as you can read in my issue report at Github: https://github.com/micropython/micropython/issues/2651 (forgot to mention it here).

To summarize, it seems that it only is the first call to measure() (yeah, I've noticed that it is missing from the methods of DHT22 as well) after boot that fails. To solve this I made a simple while loop with a try except, like this:

Code: Select all

import dht
import machine
import time

print("Starting DHT22.")
d = dht.DHT22(machine.Pin(4))

while True:
    print("Measuring.")
    
    retry = 0
    while retry < 3:
        try:
            d.measure()
            break
        except:
            retry = retry + 1
            print(".", end = "")

    print("")

    if retry < 3:
        print("Temperature: %3.1f °C" % d.temperature())
        print("   Humidity: %3.1f %% RH" % d.humidity())

    time.sleep(5)
And this works great! It gives the following output:

Code: Select all

Connecting to myapxxxxx
Network config: ('192.168.0.116', '255.255.255.0', '192.168.0.1', '192.168.0.1')
WebREPL daemon started on ws://192.168.4.1:8266
WebREPL daemon started on ws://192.168.0.116:8266
Started webrepl in normal mode
Starting DHT22.
Measuring.
.
Temperature: 25.2 °C
   Humidity: 25.0 % RH
Measuring.

Temperature: 25.2 °C
   Humidity: 25.3 % RH
Measuring.

Temperature: 25.2 °C
   Humidity: 25.1 % RH
Measuring.

Temperature: 25.2 °C
   Humidity: 24.8 % RH
Traceback (most recent call last):
  File "main.py", line 33, in <module>
KeyboardInterrupt: 

MicroPython v1.8.6-7-gefd0927 on 2016-11-10; ESP module with ESP8266
Type "help()" for more information.
>>> 
I've tried to use the DHT on different pins, and I've tried a DS18B20 on atleast one of the same pins with no issues. Perhaps trying it would work if one were to change the state of the pin to something "better" before initializing the DHT...?

Post Reply