Last active
April 24, 2024 03:29
-
-
Save anecdata/c708dd9e5a0e0b582f01f27d24fc3aab to your computer and use it in GitHub Desktop.
Multi-TLS-client (WIZapalooza): Espressif-based wifi Feather MCU + WIZnet W5100S, W5500, W6100
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# SPDX-FileCopyrightText: 2024 anecdata | |
# | |
# SPDX-License-Identifier: MIT | |
# Multi-TLS-client: Espressif-based Feather MCU + WIZnet Ethernets | |
# CircuitPython: https://github.com/adafruit/circuitpython/pull/8954 | |
# WIZnet library: https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/tree/core-compatible-socket-type-numbers | |
import time | |
import os | |
import ssl | |
import board | |
import digitalio | |
import wifi | |
import socketpool | |
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K | |
import adafruit_requests | |
URL = "https://httpbin.org/get" | |
time.sleep(3) # wait for serial | |
def get_eth_requests(radio): | |
import adafruit_wiznet5k.adafruit_wiznet5k_socket as pool | |
pool.set_interface(radio) | |
ssl_context = ssl.create_default_context() | |
requests = adafruit_requests.Session(pool, ssl_context) | |
return requests | |
# ETH | |
spi = board.SPI() | |
cs_pins = (board.A3, board.A4, board.A5) | |
cs = [] | |
radio = [] | |
ip = [] | |
requests = [] | |
for radio_num in range(0, len(cs_pins)): | |
cs.append(digitalio.DigitalInOut(cs_pins[radio_num])) | |
octet = f'{(251 + radio_num):02X}' # give each ETH a unique MAC | |
radio.append(WIZNET5K(spi, cs[radio_num], mac=f'de:ad:be:ef:fe:{octet}')) | |
ip.append(radio[radio_num].pretty_ip(radio[radio_num].ip_address)) | |
print(f"WIZnet #{radio_num} IP Address... {ip[radio_num]}") | |
requests.append(get_eth_requests(radio[radio_num])) | |
# WIFI | |
ssid = os.getenv("WIFI_SSID") | |
password = os.getenv("WIFI_PASSWORD") | |
print("Connecting to wifi... ", end="") | |
while not wifi.radio.connected: | |
wifi.radio.connect(ssid, password) | |
print(f"{wifi.radio.ipv4_address}") | |
wifi_pool = socketpool.SocketPool(wifi.radio) | |
wifi_ssl_context = ssl.create_default_context() | |
requests.append(adafruit_requests.Session(wifi_pool, wifi_ssl_context)) | |
# make the requests | |
for r in requests: | |
print("-" * 40) | |
print(f"Fetching from {URL} via {r}") | |
with r.get(URL) as resp: | |
print(f"{resp.text}") |
Author
anecdata
commented
Mar 29, 2024
Continuation of a series https://gist.github.com/anecdata/8152eba03eefd0a587747e551c683543
Also posted at https://fosstodon.org/@anecdata/112171917417772517
Turns out that for this too work correctly, multiple wiznet libraries or socket modules need to be used. The socket module isn't written as a class that can have multiple instantiations. As is, pool.set_interface(radio)
will always use the same pool.
v2: https://gist.github.com/anecdata/f2e10134bb23be029d42f36662043451
Thanks Justin for identifying the need for distinct socketpools (modules or class instances), and adafruit_wiznet PR#159 to convert the socket pool to a class.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment