Skip to content

Instantly share code, notes, and snippets.

@anecdata
Created April 26, 2024 02:37
Show Gist options
  • Save anecdata/b4ddac2b636e94ab1afe7fd59e21cee0 to your computer and use it in GitHub Desktop.
Save anecdata/b4ddac2b636e94ab1afe7fd59e21cee0 to your computer and use it in GitHub Desktop.
Adafruit_CircuitPython_ConnectionManager PR #11 Test - "Use new SocketPool for ESP32SPI and WIZNET5K"
import time
import os
import traceback
import board
import digitalio
import ssl
import adafruit_connection_manager
import adafruit_requests
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
# General
def get_requests(radio):
pool = adafruit_connection_manager.get_radio_socketpool(radio)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(radio)
requests = adafruit_requests.Session(pool, ssl_context)
return pool, requests
# Native wifi
def setup_native():
import wifi
radio = wifi.radio
pool, requests = get_requests(radio)
return radio, pool, requests
def connect_native(radio):
while not radio.connected:
try:
radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
except Exception as ex:
traceback.print_exception(ex, ex, ex.__traceback__)
ipv4_str = radio.ipv4_address
return ipv4_str
# WIZnet
def setup_eth():
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
cs = digitalio.DigitalInOut(board.A3)
mac = f'de:ad:be:ef:fe:{252:02x}' # need this to avoid DHCP collisions
radio = WIZNET5K(spi, cs, mac=mac, debug=False)
pool, requests = get_requests(radio)
return radio, pool, requests
def connect_eth(radio):
ipv4_str = radio.pretty_ip(radio.ip_address)
return ipv4_str
# ESP32SPI
def setup_esp():
from adafruit_esp32spi import adafruit_esp32spi
esp32_cs = digitalio.DigitalInOut(board.A0)
esp32_reset = digitalio.DigitalInOut(board.A1)
esp32_ready = digitalio.DigitalInOut(board.A2)
radio = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset, debug=False)
pool, requests = get_requests(radio)
return radio, pool, requests
def connect_esp(radio):
while not radio.is_connected:
try:
radio.connect_AP(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
except Exception as ex:
traceback.print_exception(ex, ex, ex.__traceback__)
ipv4_str = radio.pretty_ip(radio.ip_address)
return ipv4_str
time.sleep(3) # wait for serial
spi = board.SPI()
radio_native, pool_native, requests_native = setup_native()
radio_eth, pool_eth, requests_eth = setup_eth()
radio_esp, pool_esp, requests_esp = setup_esp()
while True:
print(f'{"="*25}')
print(f"Fetching from {TEXT_URL} via native wifi {adafruit_connection_manager.get_connection_manager(pool_native)} {connect_native(radio_native)} ", end="")
with requests_native.get(TEXT_URL) as resp:
print(f'{resp.status_code} {resp.reason.decode()}')
time.sleep(5)
print(f"Fetching from {TEXT_URL} via WIZnet {adafruit_connection_manager.get_connection_manager(pool_eth)} {connect_eth(radio_eth)} ", end="")
with requests_eth.get(TEXT_URL) as resp:
print(f'{resp.status_code} {resp.reason.decode()}')
time.sleep(5)
print(f"Fetching from {TEXT_URL} via ESP32SPI {adafruit_connection_manager.get_connection_manager(pool_esp)} {connect_esp(radio_esp)} ", end="")
with requests_esp.get(TEXT_URL) as resp:
print(f'{resp.status_code} {resp.reason.decode()}')
time.sleep(5)
@anecdata
Copy link
Author

anecdata commented Apr 26, 2024

Hardware config:

  • UnexpectedMaker FeatherS3
  • WIZnet 850io (W5500) "BFF"
  • Adafruit Airlift breakout

Software config:

  • Adafruit CircuitPython 9.1.0-beta.1 on 2024-04-19; FeatherS3 with ESP32S3
  • adafruit_connection_manager (PR#11)
  • adafruit_esp32spi (PR#198)
  • 'adafruit_wiznet5k' (PR#159)
  • other libraries are latest release
code.py output:
=========================
Fetching from http://wifitest.adafruit.com/testwifi/index.html via native wifi <ConnectionManager object at 0x3c181980> 192.168.6.184 200 OK
Fetching from http://wifitest.adafruit.com/testwifi/index.html via WIZnet <ConnectionManager object at 0x3c185070> 192.168.6.252 200 OK
Fetching from http://wifitest.adafruit.com/testwifi/index.html via ESP32SPI <ConnectionManager object at 0x3c187690> 192.168.6.37 200 OK
.
.
.
wifi+eth+esp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment