Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active December 5, 2021 16:41
Show Gist options
  • Save anecdata/cdf0d931d8b2dee21775d34d0df69879 to your computer and use it in GitHub Desktop.
Save anecdata/cdf0d931d8b2dee21775d34d0df69879 to your computer and use it in GitHub Desktop.
CircuitPython with 3 Networks: FeatherS2 + Airlift FeatherWing + Ethernet FeatherWing
import board
import busio
import sys
import os
from digitalio import DigitalInOut
import ipaddress
import wifi
import socketpool
import ssl
import espidf
import adafruit_requests
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as wiznet5k_socket
from adafruit_esp32spi import adafruit_esp32spi
import adafruit_esp32spi.adafruit_esp32spi_socket as esp_socket
# local
from secrets import secrets
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
print("""-"""*49, file=sys.stdout)
print(os.uname().machine)
print(os.uname().version)
spi = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
#
# WIZNET5K
#
# eth_cs = digitalio.DigitalInOut(board.D10)
# customized CS pin b/c ESP32 needs GPIO0 available for reboots:
eth_cs = DigitalInOut(board.A5)
eth = WIZNET5K(spi, eth_cs, debug=False)
print("Ethernet IP Address:", eth.pretty_ip(eth.ip_address))
#
# ESP32SPI
#
esp32_cs = DigitalInOut(board.D13) # Feather M4 Red LED
esp32_ready = DigitalInOut(board.D9) # Feather M4 D11
esp32_reset = DigitalInOut(board.D12)
# esp32_gpio0 = DigitalInOut(board.D10) # optional
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
esp.connect_AP(secrets['ssid'], secrets['password'])
print("ESP32SPI IP Address:", esp.pretty_ip(esp.ip_address))
#
# ESP32-S2
#
wifi.radio.connect(secrets['ssid'], secrets['password'])
print("ESP32-S2 IP Address:", wifi.radio.ipv4_address)
print()
# Initialize a requests object with an ethernet socket and interface
adafruit_requests.set_socket(wiznet5k_socket, eth)
print("Fetching text via Ethernet from", TEXT_URL)
r = adafruit_requests.get(TEXT_URL)
print(r.text)
r.close()
# Initialize a requests object with an ESP32SPI wi-fi socket and interface
adafruit_requests.set_socket(esp_socket, esp)
print("Fetching text via ESP32SPI from", TEXT_URL)
r = adafruit_requests.get(TEXT_URL)
print(r.text)
r.close()
# Initialize a requests object with an ESP32-S2 wi-fi socket and interface
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
print("Fetching text via ESP32-S2 from", TEXT_URL)
r = requests.get(TEXT_URL)
print(r.text)
r.close()
print("Done!")
@themaire
Copy link

Thanks for the exemples.

@anecdata
Copy link
Author

anecdata commented Dec 5, 2021

https://twitter.com/anecdat/status/1327472474624323585
3networks

See also:
CircuitPython with 4 Networks: W5100S + W5500 + ESP32-S2 + ESP32SPI
https://gist.github.com/anecdata/8152eba03eefd0a587747e551c683543

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