This should be a simple server/client app, but it is driving me crazy! The goal is to have 2 bare-bones pico-w boards trading data with each other via a mutual connection to my local router. I have spent several days hunting all over the internet for a similar architecture and solution but most items out there deal with html code and a single pico communicating via a website.
The stripped down server code and client code is below. It is basically typical boilerplate code.
Both picos can connect to the local router easily. I can capture the server's IP address (192.168.1.153) below and every time I start the server, it gives me this address (so I am not having an unknown address issue). The server code is running as main.py on the server pico. The client code is started from Thonny (so that I can see the print statements) and runs on the client pico. The issue is that the socket.accept() statement rarely completes. (Maybe 1 out of every 20 times I try this it will work). The client.connect() times out after maybe 20 seconds.
Perhaps someone with 2 spare Pico-Ws can replicate this issue? I am trying to find out WHY that listening socket won't connect. I am not an expert about firewalls, but I don't think it is a fire wall issue since this is all internal to the router?
Also, I have the latest Pico-W firmware installed.
SERVER code:CLIENT CODE:
The stripped down server code and client code is below. It is basically typical boilerplate code.
Both picos can connect to the local router easily. I can capture the server's IP address (192.168.1.153) below and every time I start the server, it gives me this address (so I am not having an unknown address issue). The server code is running as main.py on the server pico. The client code is started from Thonny (so that I can see the print statements) and runs on the client pico. The issue is that the socket.accept() statement rarely completes. (Maybe 1 out of every 20 times I try this it will work). The client.connect() times out after maybe 20 seconds.
Perhaps someone with 2 spare Pico-Ws can replicate this issue? I am trying to find out WHY that listening socket won't connect. I am not an expert about firewalls, but I don't think it is a fire wall issue since this is all internal to the router?
Also, I have the latest Pico-W firmware installed.
SERVER code:
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)try: 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") # Open a socket address = (ip, 80) #port 80 print("Server address = ", address) connection = socket.socket() 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") while True: try: client = connection.accept()[0] print("Client connection accepted") #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") #machine.reset() #turns off access point. sys.exit() except OSError: print("Failure to connect. Exiting..") print(OSError)finally: connection.close() print("Finally: connection closed") sys.exit() #machine.reset()
Code:
from machine import Pinfrom utime import sleepimport networkimport socketimport secretsimport sysled = machine.Pin('LED', machine.Pin.OUT)#sequencing logic below:#show that pico is active!led.on()try: print("Connect to router...") 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'Client connected on {ip}') print("WLAN,ip completed") while True: try: # Open a socket address = (ip, 80) #port 80 print("address = ", address) ai = socket.getaddrinfo('192.168.1.153', 80) # Address of Web Server print("ai : ", ai) addr = ai[0][-1] print("Server address ", addr) s = socket.socket() print("Client socket. Connecting to webserver...") s.connect(addr) #bind socket to server ip address print("Now client and webserver connected") #request the time from the server: request = "request" s.send(request.encode('UTF-8')) print("Request: ", request) #Receive time: response = s.recv(1024) print(f'Response received: {response}') response = response.decode("UTF-8") print("Response = ", response) s.close() print("-"*30) sleep(10) except KeyboardInterrupt: print("Keyboard interrupt observed") #machine.reset() #turns off access point. s.close() sys.exit()except OSError: print("Failure to connect. Exiting..") print(OSError)finally: print("Client socket closing") s.close() sys.exit() #machine.reset()
Statistics: Posted by dondondon — Sat Mar 02, 2024 7:15 pm — Replies 1 — Views 37