DHT22 on main.py returns with ETIMEDOUT

All ESP32 boards running MicroPython.
Target audience: MicroPython users with an ESP32 board.
Post Reply
pccminister
Posts: 7
Joined: Wed Mar 04, 2020 8:44 am

DHT22 on main.py returns with ETIMEDOUT

Post by pccminister » Thu Mar 12, 2020 1:20 am

I tried to run this code on main.py:

Code: Select all

import machine
import dht
import time

dh = dht.DHT22(machine.Pin(4))
while True:
  
  dh.measure()
  time.sleep(2)
  tem = dh.temperature()
  hum = dh.humidity()
  print(hum , tem)
and got this error:

OSError: [Errno 110] ETIMEDOUT

when i run main.py after that error it runs OK.
how can i make it run main.py automatically after boot?

palivoda
Posts: 13
Joined: Thu Feb 13, 2020 9:42 pm

Re: DHT22 on main.py returns with ETIMEDOUT

Post by palivoda » Thu Mar 12, 2020 10:46 pm

You can put your read block in to try-except block, e.g:

Code: Select all

try:
  # read DHT22 here
except:
  print("Cant read DHT22")

pccminister
Posts: 7
Joined: Wed Mar 04, 2020 8:44 am

Re: DHT22 on main.py returns with ETIMEDOUT

Post by pccminister » Thu Mar 12, 2020 11:31 pm

palivoda wrote:
Thu Mar 12, 2020 10:46 pm
You can put your read block in to try-except block, e.g:

Code: Select all

try:
  # read DHT22 here
except:
  print("Cant read DHT22")
so I did this and it worked:

Code: Select all

import machine
import dht
import time

dh = dht.DHT22(machine.Pin(4))
   
try:
    while True:
           
        dh.measure()
        time.sleep(2)
        tem = dh.temperature()
        hum = dh.humidity()
        print(hum , tem)
   
except OSError:
        print("cant read")
        while True:
           
            dh.measure()
            time.sleep(2)
            tem = dh.temperature()
            hum = dh.humidity()
            print(hum , tem)
but I think there should be a better way!

palivoda
Posts: 13
Joined: Thu Feb 13, 2020 9:42 pm

Re: DHT22 on main.py returns with ETIMEDOUT

Post by palivoda » Fri Mar 13, 2020 12:22 pm

Sure:

Code: Select all

import machine, dht, time, sys
dh = dht.DHT22(machine.Pin(4))
while True:
	try:
		dht.measure()
        	time.sleep(2)
		tem = dh.temperature()
		hum = dh.humidity()
		print('Temp:', hum, 'C\tHumidity', tem, '%')
		time.sleep(1)
	except OSError as e:
		print("Cant read:", e)
		sys.print_exception(e)
Can you show output of the code? I use DHT22 without errors on first run.
Maybe there is something else in boot.py or main.py that causes the error.

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

Re: DHT22 on main.py returns with ETIMEDOUT

Post by jonaslorander » Fri Mar 13, 2020 12:42 pm

I had this same issue with a ESP8266 a few years back.
You can see my solution here:
viewtopic.php?f=16&t=2668#p16457

pccminister
Posts: 7
Joined: Wed Mar 04, 2020 8:44 am

Re: DHT22 on main.py returns with ETIMEDOUT

Post by pccminister » Fri Mar 13, 2020 9:57 pm

palivoda wrote:
Fri Mar 13, 2020 12:22 pm
Sure:

Code: Select all

import machine, dht, time, sys
dh = dht.DHT22(machine.Pin(4))
while True:
	try:
		dht.measure()
        	time.sleep(2)
		tem = dh.temperature()
		hum = dh.humidity()
		print('Temp:', hum, 'C\tHumidity', tem, '%')
		time.sleep(1)
	except OSError as e:
		print("Cant read:", e)
		sys.print_exception(e)
Can you show output of the code? I use DHT22 without errors on first run.
Maybe there is something else in boot.py or main.py that causes the error.
I ran your code and this is the result:

Code: Select all

Cant read: [Errno 110] ETIMEDOUT
Traceback (most recent call last):
  File "main.py", line 5, in <module>
  File "dht.py", line 16, in measure
OSError: [Errno 110] ETIMEDOUT
Temp: 18.2 C	Humidity 34.2 %
Temp: 17.1 C	Humidity 32.5 %
Temp: 17.2 C	Humidity 32.5 %
Temp: 17.1 C	Humidity 32.5 %
Temp: 16.9 C	Humidity 32.5 %
Temp: 16.8 C	Humidity 32.5 %
Temp: 16.8 C	Humidity 32.5 %

pccminister
Posts: 7
Joined: Wed Mar 04, 2020 8:44 am

Re: DHT22 on main.py returns with ETIMEDOUT

Post by pccminister » Fri Mar 13, 2020 10:00 pm

but you know that there are some tiny fixes about your code that should be applied!

there is a new Q:

why your code ran OK and my code needed me to repeat my Loop?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: DHT22 on main.py returns with ETIMEDOUT

Post by jimmo » Mon Mar 16, 2020 4:05 am

pccminister wrote:
Fri Mar 13, 2020 10:00 pm
why your code ran OK and my code needed me to repeat my Loop?
I think it's because their code looks like this:

Code: Select all

while True:
  try:
    .. measure
  except:
    .. ignore error
whereas yours looks like:

Code: Select all

try:
  while True:
    .. measure
except:
  ...

i.e. the try/except needs to be around _only_ the measure, not the whole loop.

palivoda
Posts: 13
Joined: Thu Feb 13, 2020 9:42 pm

Re: DHT22 on main.py returns with ETIMEDOUT

Post by palivoda » Tue Mar 17, 2020 1:46 pm

pccminister wrote:
Fri Mar 13, 2020 10:00 pm
but you know that there are some tiny fixes about your code that should be applied!

there is a new Q:

why your code ran OK and my code needed me to repeat my Loop?
try-except does not solve your problem - error on first read, but allow to ignore any read error and continue code exection.
Check something like this - http://www.blog.pythonlibrary.org/2012/ ... -handling/

I could only guess why you have error on first read:
1) check you dont do anything with this pin in boot.py
2) try to add time.sleep(2) right before "while True" line.
3) change sensor
4) check wires and resistors - http://cactus.io/hookups/sensors/temper ... ity-sensor
5) use osciloscope or data logger to see whats going on the data wire

Post Reply