-
-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Unable to read DHT22-sensor on boot #2651
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I can't reproduce this error. Using the exact main.py script above works ok for me with both STA and AP interfaces running, as well as WebREPL. @jonaslorander what is in your boot.py file? |
Thats odd. Perhaps there is something wrong with my NodeMCU...? I will try another one this weekend. Or try deleting boot.py first. This is my boot.py: # This file is executed on every boot (including wake-boot from deepsleep)
#import esp
#esp.osdebug(None)
import gc
import webrepl
gc.collect()
def do_connect():
import network
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
if not wlan.isconnected():
confwifi = open('wifis.conf').readlines()
aps = wlan.scan()
for cw in confwifi:
conf_ssid, conf_pass = cw.split(' , ')
conf_ssid = conf_ssid.strip()
conf_pass = conf_pass.strip()
for ap in aps:
ap_ssid = ap[0].decode("utf-8")
if ap_ssid == conf_ssid:
print('Connecting to %s' % ap_ssid)
wlan.connect(conf_ssid, conf_pass)
while not wlan.isconnected():
pass
break
if wlan.isconnected():
break
if wlan.isconnected():
print('Network config:', wlan.ifconfig())
else:
print('Unable to find any known networks.')
wlan.active(False)
do_connect()
webrepl.start() |
Removing my boot.py made no difference. But I made another discovery. If I reset my NodeMCU board with the hardware reset button the I get the error above. But if I do a soft reset with |
I found a way around the issue. Encapsulating the This is the code I have working: 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) The output upon a hard reset is this:
With this I can get going on my original application. I don't know if I should leave this open or not. A team member can close the issue if they think it is solved. Since no one else seems to be able to reproduce it, it might be isolated to something at my end... |
I was having the same problem. Solution for me was actually quite simple. According to the DHT22 Datasheet it's the sensing period is ~2s. So I figured the sensor might just not be ready to give measurements right after startup. Putting a time.sleep(5) before d.measure() solved it for me. Just posting this to save someone else from wasting a couple of hours. ;). |
This problem occurs even when I have the sensor powered up for a long time and reset the ESP. But, perhaps the pins are toggling or something at startup that might trigger a reading from the DHT... I will be sticking with my workaround for now :) |
I am having a similar problem! Will try the loop suggestion. Thanks for sharing this! I am using a Lolin NodeMCU board - V2. |
I've had the same problem with an ESP32, and even with the workaround I didn't make it. It was working, then it was failing sometimes, and finally errors were too frequent. In my case reviewing the wiring did the trick. I guess there was a bad connection in the data pin. |
Seems likely to be related to hardware problems. Proposing to close, as there is no reproducible issue. |
There are good working drivers for DHT22 |
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I was having issues reading a DHT22-sensor in my main.py program. After some ivestigation I noticed that it only appear upon boot of my ESP8266 (NodeMCU hardware). I'm running MicroPython 1.8.6.
So for testing purposes I have this code in main.py:
When restarting my ESP the code will run automagically as it is in main.py, this is the output:
But if I run the code manually once the ESP has booted with
import main
I get this (expected) result:What is the problem here? Is the boot process not really completed when main.py executes? I've tried having a delay on 1 to 20 seconds before the
while True:
loop with no success. Isn't it possible to read a DHT-sensor upon boot?The text was updated successfully, but these errors were encountered: