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.
d.measure() causes error
Re: d.measure() causes error
The DHT sensors have a limit of how fast you can read values from them. If you try too fast, you get the error.
Re: d.measure() causes error
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?
Re: d.measure() causes error
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.