d.measure() causes error

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
ctrlf
Posts: 5
Joined: Mon Oct 10, 2016 1:18 pm

d.measure() causes error

Post by ctrlf » Mon Oct 10, 2016 1:25 pm

I am using ESP8266 as an access point and upon request it returns values from sensors. My d.measure() function causes error:

[code]Traceback (most recent call last):
File "<stdin>", line 32, in <module>
File "dht.py", line 13, in measure
OSError: [Errno 110] ETIMEDOUT

This is my code:

import dht
import machine
d = dht.DHT22(machine.Pin(2))
adc = machine.ADC(0)

html = """<!DOCTYPE html>
<html>
<head> <title>ESP8266 Weather Station</title> </head>
<body> <h1>ESP8266 Weather Station</h1>
<table border="1"> <tr><th>Pin</th><th>Value</th></tr> %s </table>
</body>
</html>
"""

import socket
addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]

s = socket.socket()
s.bind(addr)
s.listen(1)

print('listening on', addr)

while True:
cl, addr = s.accept()
print('client connected from', addr)
cl_file = cl.makefile('rwb', 0)
while True:
line = cl_file.readline()
if not line or line == b'\r\n':
break
d.measure()
rows = ['<tr><td>%s</td><td>%d</td></tr><tr><td>%s</td><td>%d</td></tr><tr><td>%s</td><td>%d</td></tr>' % ('Temperature (C)', d.temperature(), 'Humidity', d.humidity(), 'Ambient light (%)', adc.read()/10.24)]
response = html % '\n'.join(rows)
cl.send(response)
cl.close()[/code]

It works only on the first run, then it immediately crashes. If I put d.measure() outside the while it runs fine but the measurements don't refresh.

User avatar
deshipu
Posts: 1388
Joined: Thu May 28, 2015 5:54 pm

Re: d.measure() causes error

Post by deshipu » Mon Oct 10, 2016 7:51 pm

The DHT sensors have a limit of how fast you can read values from them. If you try too fast, you get the error.

ctrlf
Posts: 5
Joined: Mon Oct 10, 2016 1:18 pm

Re: d.measure() causes error

Post by ctrlf » Mon Oct 10, 2016 9:03 pm

You are correct. I added a delay between the command and it worked better but it didn't solve my problem. Instead of happening the second time for sure now happens randomly, but it still does. Do you know that limit?

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: d.measure() causes error

Post by chrisgp » Tue Oct 11, 2016 5:03 am

According to the docs:
The DHT11 can be called no more than once per second and the DHT22 once every two seconds for most accurate results.

Post Reply