Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 4497

General • Pi Pico W - DHT11 - wrong number of pulses ONLY after 2 reads

$
0
0
Hi

I'm having an odd problem with a Pi Pico W reading a DHT11 sensor. I'm running a small web server on the pico to report temp and humidity whenever the page is requested.

At the start of the program I read the DHT11 once. Then, once an http request is made, I read it again and report it to the requesting connection. This works ok. However, if I try and read it again I get the dreaded "expected 84 pulses, only got 82". This only happens on the 3rd read of the DHT11. I've tried adding all sort of delays, cleaning up any garbage, checking it's not running out of RAM but just can't figure out what the problem might be.

I have got the hardware soldered onto strip board so the connections should be good.

Source code:

Code:

import networkimport socketimport timefrom machine import Pin, I2C, reset#from dht import DHT11, InvalidChecksum#import dhtfrom dht11_new import DHT11import utime as timeimport gcssid = 'redacted'password = 'redacted'wlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(ssid, password)pin = Pin(0, Pin.OUT, Pin.PULL_DOWN)sensor = DHT11(pin)t  = sensor.temperatureh = sensor.humidityprint("Temperature: {}".format(sensor.temperature))print("Humidity: {}".format(sensor.humidity))html = """<!DOCTYPE html>   <html>   <link rel="icon" href="data:;base64,=">   <head> <title>Pico W</title> </head>        <body>         <h1>            Temp %s         </h1>         <h1>            Humidity %s         </h1>        </body>    </html>""" # Wait for connect or failmax_wait = 2while max_wait > 0:    if wlan.status() < 0 or wlan.status() >= 3:        break    max_wait -= 1    print('waiting for connection...')    time.sleep(1)# Handle connection errorif wlan.status() != 3:    raise RuntimeError('network connection failed')else:    print('connected')    status = wlan.ifconfig()    print( 'ip = ' + status[0] ) # Open socketaddr = socket.getaddrinfo('0.0.0.0', 80)[0][-1] s = socket.socket()s.bind(addr)s.listen(1)time.sleep(3)print('listening on', addr) # Listen for connectionswhile True:    try:        cl, addr = s.accept()        print('client connected from', addr)        time.sleep(0.5)        request = cl.recv(2048)        print(request)        #gc.collect()        #print(gc.mem_free())        if len(request)<=3:              print('empty request - ignore')            cl.close()                    else:            #request = str(request)            pin = Pin(0, Pin.OUT, Pin.PULL_DOWN)            sensor = DHT11(pin)            #time.sleep(3)            t  = sensor.temperature            h = sensor.humidity            print("Temperature: {}".format(sensor.temperature))            print("Humidity: {}".format(sensor.humidity))            response = html % (t, h)            print(t, h)            cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')            cl.send(response)            print('response sent')            cl.close()            print('cl closed. Wait 2s')        time.sleep(2)        print('cl closed')        gc.collect()    except OSError as e:        cl.close()        print('connection closed')

Statistics: Posted by KennyRonin — Tue Jan 30, 2024 3:37 pm — Replies 4 — Views 86



Viewing all articles
Browse latest Browse all 4497

Trending Articles