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

MicroPython • Re: pico-to-pico via intenral router: accept () issue

$
0
0
Thanks. I really appreciate the advice. I sent a few more hours tonight working on it. My latest theory (will probably will get debunked tomorrow) is that when the client fails to connect to the server's listening socket, that socket never gets closed. Since it is running as main.py, the only thing I can do is pull the power for the device. Now I am think the router itself still thinks that socket is still alive ? But why would the router care since it is a server socket on the Pico? Anyway, when power is restored and the socket.accept() statement is reached the next time, the server will just sit there and not accept a new client connection most of the time.

Obviously it is all still a bit above my thinking right now. I have tweaked the code to add a timeout on the socket and also try to close the socket with a finally statement. Things are working better, but still not solid enough to actually rely on for general purpose apps. See server update below:

Code:

from machine import Pinfrom time import sleepimport networkimport socketimport secretsimport sysled = machine.Pin('LED', machine.Pin.OUT)#sequencing logic below:#show that pico is active!led.on()sleep(1)led.off()try:    # ------------------  CONNECT -----------------    print("Connect to router...")    # Define SSID and password for the access point    ssid = secrets.my_SSID    password = secrets.my_word      wlan = network.WLAN(network.STA_IF)      wlan.active(True)    wlan.connect(ssid, password)         # Wait until it is active       while wlan.isconnected() == False:        print('Waiting for connection...')        sleep(1)    print("Connected to router")    ip = wlan.ifconfig()[0]    print(f'Server connected on {ip}')    print("WLAN,ip completed")    led.on()    sleep(3)    led.off()    sleep(1)      # Open a socket    address = (ip, 80)   #port 80    print("Server address = ", address)    connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)    connection.settimeout(60)    print("preparing connection for bind...")    connection.bind(address)      print("Now listening for client on this connection:  ",connection)    connection.listen(1)      print("connection completed. Ready to serve")        # ------------------ SERVE CLIENT  ----------------------       while True:        try:            client = connection.accept()[0]             print("Client connection accepted")            led.on()            #Receive request from client            request = client.recv(1024)            print("Data received: ")            request = str(request)            print(f'Request received: {request}')            #send back response:            response = "OK"            client.send(response.encode('UTF-8'))            client.close()            print("-"*30)        except KeyboardInterrupt:            print("Keyboard interrupt observed")            break    except Exception as e:    print("Failure to connect.  Exiting..")    print("error: ",e)finally:    connection.close()    client.close()    print("Finally: connection closed")    led.off()    sys.exit()    #machine.reset()

Statistics: Posted by dondondon — Sun Mar 03, 2024 8:13 am



Viewing all articles
Browse latest Browse all 4907

Trending Articles