asyncio.start_server issue with Wiznet W5500 #17964
Replies: 3 comments 2 replies
-
When I try to use uasyncio.start_server() on the EVB-W5500-PICO2 with the RP2350 and firmware v1.26, it crashes with OSError: [Errno 107] ENOTCONN. The same code works on the older RP2040-based board. The error basically means the socket is being treated as “not connected” when asyncio tries to use it. The problem doesn’t come from the code itself but from the way the Wiznet W5500 Ethernet driver integrates with uasyncio. The W5500 sockets work fine in normal blocking mode, so if you call socket(), bind(), listen(), and accept() directly you can get connections just fine. The trouble is that uasyncio.start_server() expects the network driver to provide socket operations that work with the event loop, especially the ability to poll or select on the socket. The Wiznet driver doesn’t implement that fully, which is why asyncio fails when trying to set up a server. So it looks more like a limitation in the Wiznet driver’s async support than an issue with asyncio or with the user code. here are two possible solutions import uasyncio as asyncio --- Ethernet Setup ---SPI0 = SPI(0, baudrate=5_000_000) --- Server Loop ---async def server():
async def handle_client(conn): asyncio.run(server()) This works today with Wiznet. No crash. Wrapper Solution (Keep handle(r, w)) import uasyncio as asyncio --- Ethernet Setup ---SPI0 = SPI(0, baudrate=5_000_000) --- Stream Wrappers ---class StreamReader:
class StreamWriter:
--- Your Original Style Handler ---async def handle(reader, writer): --- Async Server (manual accept loop + wrappers) ---async def server():
asyncio.run(server()) Now you can use the same async def handle(r, w): ... style you wanted, without asyncio.start_server() blowing up. |
Beta Was this translation helpful? Give feedback.
-
@Thildaugg it's interesting that you could get the asyncio.start_server() working on the previous Wiznet board - would this imply an issure with the interaction between W5500, RP2350 and v1.26 specifically? I assumed the SPI comms and handling would not have altered. I was hoping to use Microdot with this solution, but clearly I will have to wait for some further development with the Rp2350 and Wiznet. Thank you again for taking the time to respond the way you did - it has been very helpful. |
Beta Was this translation helpful? Give feedback.
-
@Thildaugg A few queries about the robustness of this: async def read(self, n=256):
while True:
try:
return self.conn.recv(n)
except OSError:
await asyncio.sleep(0.01)
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I am using a Wiznet dev board (EVB-W5500-PICO2) which essentially upgrades the previous EDVB-W5500-PICO from using a RP2040 MCU to the newer RP2350. I have tried both boards and am finding with the latest firmware v1.26, I am unable to get asyncio.start_server() to work. It results in a hard crash.
################################################
from machine import SPI, Pin
import network, uasyncio as asyncio
SPI0 = SPI(0, baudrate=5_000_000)
NIC = network.WIZNET5K(SPI0, Pin(17), Pin(20))
NIC.active(True)
if NIC.ifconfig()[0] == "0.0.0.0":
NIC.ifconfig("dhcp")
IP = NIC.ifconfig()[0]
print("[net] up:", NIC.ifconfig())
async def handle(r, w):
await r.read(256)
w.write(b"HTTP/1.0 200 OK\r\nContent-Length:2\r\n\r\nOK")
await w.drain()
async def main():
host = IP
srv = await asyncio.start_server(handle, host, 8080)
while True:
await asyncio.sleep(1)
asyncio.run(main())
################################################
This is the result:
################################################
File "asyncio/core.py", line 1, in run
File "asyncio/core.py", line 1, in run_until_complete
File "asyncio/core.py", line 1, in run_until_complete
File "", line 21, in main
File "asyncio/stream.py", line 1, in start_server
OSError: [Errno 107] ENOTCONN
################################################
Am I doing something obviously wrong here? Is there an issue with using the W5500 with asyncio?
I have searched in the forum but could not find anything applicable. Thanks in advance.
Beta Was this translation helpful? Give feedback.
All reactions