adafruit / Adafruit_CircuitPython_Wiznet5k

Pure-Python interface for WIZNET 5k Ethernet modules
Other
15 stars 37 forks source link

Convert to SocketPool #159

Closed justmobilize closed 6 months ago

justmobilize commented 6 months ago

For use with: https://github.com/adafruit/Adafruit_CircuitPython_ConnectionManager/pull/11

Converting from the old adafruit_wiznet5k/adafruit_wiznet5k_socket.py to adafruit_wiznet5k/adafruit_wiznet5k_socketpool.py to remove set_interface and work more like a built-in SocketPool.

This also allows one to use multiple WIZNET5K boards at the same time.

General example:

import board
import busio
import digitalio
import adafruit_requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"

cs = digitalio.DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
radio = WIZNET5K(spi_bus, cs)
pool = socketpool.SocketPool(radio)
requests = adafruit_requests.Session(pool)
print(requests.get(TEXT_URL).text)

or with the new ConnectionManager

import board
import busio
import digitalio
import adafruit_connection_manager
import adafruit_requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
from adafruit_wiznet5k import adafruit_wiznet5k_socketpool

TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"

cs = digitalio.DigitalInOut(board.D10)
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
radio = WIZNET5K(spi_bus, cs)
pool = adafruit_connection_manager.get_radio_socketpool(radio)
assert isinstance(pool, adafruit_wiznet5k_socketpool.SocketPool)
requests = adafruit_requests.Session(pool)
print(requests.get(TEXT_URL).text)
justmobilize commented 6 months ago

@anecdata re-done with all the merges (including the SSL one). Would you be willing to test this again for me?

justmobilize commented 6 months ago

I apologize for the hard review. With moving a few things at the top, it isn't a simple white space change with it going into the class. I promise I didn't change anything.

anecdata commented 6 months ago

This looks good, working for me with...

Hardware config: https://gist.github.com/anecdata/c708dd9e5a0e0b582f01f27d24fc3aab?permalink_comment_id=5004766#gistcomment-5004766

Code: https://gist.github.com/anecdata/f2e10134bb23be029d42f36662043451

Verified at the router that the sessions are cycling through the three ethernet IP addresses in sync with the code.

addendum: HTTPS is also working across all three ethernets. Not sure if it matters, but SSL works using either separate ssl.create_default_context() for each ethernet, or using a single shared ssl.create_default_context().

dhalbert commented 6 months ago

Same q I asked re ESP32SPI. If this is an incompatible change, then we'll need to change any Guide examples we have. If not, no problem.