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

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

$
0
0
My code ran for hours with no problems. Then it wouldn't work at all for hours, hanging on waiting for request, erroring with 'connection reset'. Then it started working again, has worked for hours.

It does seem there are times it can get itself into a mess which sometimes requires careful power cycle sequencing. But, once working, it does seem to keeps working. Tested on Pico W and WIZnet-W5500-EVB-Pico over a wired connection.

To make my Pico W code development easier I have a framework and a build regime which allows me to write code without much clutter. The build regime brings together everything it needs and auto-magically fills in the missing bits so it appears I have a single program which runs on my Pico W with all the framework stuff. That makes it great for using 'mpremote run' which is my favoured tool.

It does however mean there's a lot of stuff in my code to support the framework rather than what it's doing. But it may be useful t compare what I have with your own version.

I have include my 'simple_server' and 'simple_client' which unzipped should just run. It should support use of your existing 'secrets.py'. The server IP is 'x.x.x.199 so you may have to change that if it's problematic on your network. It uses Port 80.

The key parts for the server are 'RegisterTcpServer', 'Simple_Server' and 'Simple_Server_Handler'. For the client, just 'Simple_Client'. I have included those towards the end for others who might just want to get an idea of what I am doing.

My framework and build tools aren't complete so it's a bit hacky at present but is good enough for now. It doesn't yet include timeouts and restarts but that will come, as will support for other stuff.

It did at least identify a bug of omission in my own HTTP server when I ran that and 'simple_client' hung that solid. This networking malarkey isn't easy. Desktop OS developers have had decades to identify gotchas and create workrounds for their libraries, with a depth of expertise to call upon - We are doing it from the ground up, finding those gotchas as we go with little experience to fall back upon.

For the server

Code:

def RegisterTcpServer(name, port, func):  skt = socket.socket()  sockets.append(skt)  servers[skt] = func  skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)  skt.bind(("0.0.0.0", port))  skt.listen(1)  print("  {} on {}:{}".format(name.upper(), IpAddress(), port))requestCount = 0def Simple_Server(simple_skt):  global requestCount  try:    # Got a connection    client, adr = simple_skt.accept()    led.on()    print("Request from {} port {}".format(adr[0], adr[1]))    # Handle the request    Simple_Server_Handler(client)  # Handle exceptions  except Exception as e:    ShowException(e)   # Close the client connection when done  try    : client.close()  except : pass  led.off()def Simple_Server_Handler(client):  global requestCount; requestCount += 1  # Read the request  request = client.recv(1024)  # Determine what was received as a string so we can more easily handle it  received = Readable(request)  # If we got "[Request 0]" we reset our packet count so we will be back  # in sync  if received.lower().find("[request 0]") >= 0:    requestCount = 0  # Form a reply  s = "Received {} : {}".format(requestCount, received)  # Indicate if matched or mismatch  if received.find(" {}]".format(requestCount)) >= 0 : s += " - Okay"  else                                               : s += " - MISMATCH"  # Show the reply  print("  {}".format(s))  # Reply to the client if we need to  if received.lower().startswith("reply to"):    client.sendall(s)
For the client

Code:

requestCount = -1def Simple_Client():  global requestCount  while True:    # Maximise memory availability    ShrinkIfNeeded()    # Make a request every second    time.sleep(0.125)    led.off()    time.sleep(0.875)    led.on()    requestCount += 1    print("Make request {} to Simple Server on {}:{}".format(          requestCount, url, SERVER_PORT))    try:      # Connect to the server      server = socket.getaddrinfo(url, SERVER_PORT)[0][-1]      skt = socket.socket()      skt.connect(server)      # Send a request which asks for a reply      request = "Reply to [Request {}]".format(requestCount)      skt.sendall(request)      # If we asked for a reply then wait for it and report it      if request.lower().startswith("reply to"):        reply = skt.recv(1024)        print("  {}".format(Readable(reply)))    # Handle exceptions and report them    except Exception as e:      ShowException(e)    # Close the request connection when done    try    : skt.close()    except : pass

Statistics: Posted by hippy — Mon Mar 04, 2024 7:03 pm



Viewing all articles
Browse latest Browse all 8609

Trending Articles