Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active March 27, 2024 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anecdata/b8f8471daa98201903f9f6a976ae5dc3 to your computer and use it in GitHub Desktop.
Save anecdata/b8f8471daa98201903f9f6a976ae5dc3 to your computer and use it in GitHub Desktop.
Dual-TLS-Client (ESP32-S3 + WIZnet 5500)
# SPDX-FileCopyrightText: 2024 anecdata
#
# SPDX-License-Identifier: MIT
# Adafruit CircuitPython 9.0.0-beta.2-21-gc91b20fd96 on 2024-03-05; Adafruit Feather ESP32-S3 TFT with ESP32S3
# https://github.com/adafruit/circuitpython/pull/8954
# https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/tree/core-compatible-socket-type-numbers
import time
import os
import ssl
import board
import busio
import digitalio
import wifi
import socketpool
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as eth_pool
import adafruit_requests
URL = "https://httpbin.org/get"
time.sleep(3) # wait for serial
# ETH
spi = board.SPI()
eth_cs = digitalio.DigitalInOut(board.A5) # custom Adafruit Ethernet FeatherWing
eth_radio = WIZNET5K(spi, eth_cs, debug=False)
eth_ip = eth_radio.pretty_ip(eth_radio.ip_address)
print(f"WIZnet IP Address... {eth_ip}")
eth_pool.set_interface(eth_radio)
eth_ssl_context = ssl.create_default_context()
eth_requests = adafruit_requests.Session(eth_pool, eth_ssl_context)
# WIFI
ssid = os.getenv("WIFI_SSID")
password = os.getenv("WIFI_PASSWORD")
print("Connecting to wifi...", end=" ")
wifi_radio = wifi.radio
while not wifi_radio.connected:
wifi_radio.connect(ssid, password)
print(wifi_radio.ipv4_address)
wifi_pool = socketpool.SocketPool(wifi_radio)
wifi_ssl_context = ssl.create_default_context()
wifi_requests = adafruit_requests.Session(wifi_pool, wifi_ssl_context)
# make the request with each "radio"
for r in (eth_requests, wifi_requests):
print("-" * 40)
print(f"Fetching from {URL} via {r}")
with r.get(URL) as resp:
print(f"{resp.text}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment