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

Troubleshooting • Accept hanging on Pico W python

$
0
0
Hi,

I have developed an application that uses a Pico W to measure some temperatures and serve then to clients via a web page. This works fine at first but I have found that if the websever is not called frequently it hangs on the socket accept call. By hangs I mean the client sends a page request but the PicoW never exits the accept function. In order to try and simplify the code I have looked at the example: https://www.raspberrypi.com/news/how-to ... pi-pico-w/ and find that exhibits exactly the same behavior. At first making requests works fine, but if left unused for a while (the time seems inconsistent as far as I can tell but several minutes minimum) it will stop responding to clients.

It seems to me tha the accept function never appears to time out using the default socket setup. If I put a call such as s.settimeout(10) before s.bind() then the Accept call times out after 10 seconds (I chose 10 merely as Firefox has a defaul 10 second timeout when requesting a web page I believe). The client then appears to work for longer periods , however after a while it can take several repeated page requests before it responds after which it will respond quickly to subsequent requests if made with only a short gap between. I have looked for socket.getdefaulttimeout() and socket.gettimeout() but these don't appear to be support on the Pico W.

I have also found that by putting <meta http-equiv="refresh" content="60"> into the header section of the web page (this causes a refresh to be sent to the server every 60 seconds) the accept function never appears to hang (Tried over about 3 hrs) and always responds to

Code:

requests quickly.

.I am usnig: MicroPython v1.23.0 on 2024-06-02; Raspberry Pi Pico W with RP2040

Below is the slightly modified example code if anyone would like to see if they can reproduce the issue (put your own wifi details in!)
This looks like the accept function is not performing properly after a while, but I can't work out the details can anyone shed any light?

Code:

 import networkimport socketimport timefrom machine import Pinled = Pin(15, Pin.OUT)ssid = 'SSID'password = 'PASSWORD'wlan = network.WLAN(network.STA_IF)wlan.active(True)network.hostname('PicoWTest.local')wlan.connect(ssid, password)html = """<!DOCTYPE html><html>    <head> <title>Pico W</title> </head>    <body> <h1>Pico W</h1>        <p>%s</p>    </body></html>"""max_wait = 10while max_wait > 0:    if wlan.status() < 0 or wlan.status() >= 3:        break    max_wait -= 1    print('waiting for connection...')    time.sleep(1)if wlan.status() != 3:    raise RuntimeError('Network connection failed ' & wlan.status)else:    print('connected')    status = wlan.ifconfig()    print( 'ip = ' + status[0] )addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]s = socket.socket()s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.settimeout(10)s.bind(addr)s.listen(1)print('listening on', addr)# Listen for connectionscount = 0while True:    try:        print ('Prior to accept')        count = count + 1        print ('count = ' + str(count))        cl, addr = s.accept()        print('client connected from', addr)        request = cl.recv(1024)        print(request)        print ('count = ' + str(count))        count = 0        request = str(request)        led_on = request.find('/light/on')        led_off = request.find('/light/off')        print( 'led on = ' + str(led_on))        print( 'led off = ' + str(led_off))        if led_on == 6:            print("led on")            led.value(1)            stateis = "LED is ON"        if led_off == 6:            print("led off")            led.value(0)            stateis = "LED is OFF"        response = html % stateis        cl.send('HTTP/1.0 200 OK\r\nContent-type: text/html\r\n\r\n')        cl.send(response)        cl.close()    except OSError as e:        cl.close()        print('Connection closed Error: ' + str(e))

Statistics: Posted by countrypaul — Sat Sep 07, 2024 5:02 pm — Replies 0 — Views 13



Viewing all articles
Browse latest Browse all 3466

Trending Articles