Created
June 2, 2025 19:00
-
-
Save dglaude/5be9c4728e1db23444a2d2aeca7e51fe to your computer and use it in GitHub Desktop.
My network.py as it is before the "in the weed".
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
# How I use network.py | |
# ... | |
import network | |
socket_pool, ssl_context = network.init_network() | |
# ... |
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
import os | |
import board | |
def init_network(): | |
""" | |
Initializes network components and returns socket_pool and ssl_context. | |
""" | |
stack="Unknown" # Try to identify what network stack to use | |
if board.board_id in ("pyportal"): | |
stack="ESP32SPI" | |
### elif board.board_id in ("lolin_s2_pico"): | |
elif board.board_id in ("adafruit_esp32s3_camera", "lilygo_twatch_2020_v3", "lilygo_tembed_esp32s3"): | |
stack="WIFI" | |
else: # "adafruit_feather_rp2040" | |
featherwing=os.getenv("CIRCUITPY_FEATHERWING") | |
if featherwing in ("AIRLIFT"): | |
stack="ESP32SPI" | |
### elif board.board_id in ("lolin_s2_pico"): | |
elif featherwing in ("WIZNET"): | |
stack="WIZNET" | |
else: # "adafruit_feather_rp2040" | |
print("Unknown CIRCUITPY_FEATHERWING") | |
if stack in ("ESP32SPI"): | |
import adafruit_connection_manager | |
import busio | |
from adafruit_esp32spi import adafruit_esp32spi | |
from digitalio import DigitalInOut | |
ssid=os.getenv("CIRCUITPY_WIFI_SSID") | |
password=os.getenv("CIRCUITPY_WIFI_PASSWORD") | |
# If you are using a board with pre-defined ESP32 Pins: | |
if board.board_id in ("pyportal"): | |
esp32_cs = DigitalInOut(board.ESP_CS) | |
esp32_ready = DigitalInOut(board.ESP_BUSY) | |
esp32_reset = DigitalInOut(board.ESP_RESET) | |
else: # ("adafruit_feather_rp2040") | |
esp32_cs = DigitalInOut(board.D13) | |
esp32_ready = DigitalInOut(board.D11) | |
esp32_reset = DigitalInOut(board.D12) | |
# | |
spi = busio.SPI(board.SCK, board.MOSI, board.MISO) | |
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) | |
# | |
print("Connecting to network with ", stack) | |
while not esp.is_connected: | |
try: | |
esp.connect_AP(ssid, password) | |
except RuntimeError as e: | |
print("Could not connect to AP, retrying") | |
continue | |
print("Connected to Wifi") | |
socket_pool = adafruit_connection_manager.get_radio_socketpool(esp) | |
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp) | |
### elif board.board_id in ("lolin_s2_pico"): | |
### import ssl, socketpool, wifi | |
### | |
### # Connect to WiFi | |
### print(f"Connecting to {os.getenv('CIRCUITPY_WIFI_SSID')}") | |
### wifi.radio.connect(ssid, password) | |
### | |
### socket_pool = (socketpool.SocketPool(wifi.radio),) | |
### ssl_context = (ssl.create_default_context(),) | |
### | |
elif stack in ("WIFI"): | |
import adafruit_connection_manager | |
import wifi | |
ssid=os.getenv("CIRCUITPY_WIFI_SSID") | |
password=os.getenv("CIRCUITPY_WIFI_PASSWORD") | |
# Connect to WiFi | |
print("Connecting to network with ", stack) | |
wifi.radio.connect(ssid, password) | |
# Create a socket pool and ssl_context | |
socket_pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) | |
ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) | |
elif stack in ("WIZNET"): | |
print("In WIZNET") | |
import adafruit_connection_manager | |
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K | |
import digitalio | |
import busio | |
# For Adafruit Ethernet FeatherWing | |
cs = digitalio.DigitalInOut(board.D10) | |
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) | |
# Initialize ethernet interface with DHCP | |
print("eth") | |
eth = WIZNET5K(spi_bus, cs) | |
print(eth) | |
print("pool") | |
socket_pool = adafruit_connection_manager.get_radio_socketpool(eth) | |
print("ssl_context") | |
ssl_context = adafruit_connection_manager.get_radio_ssl_context(eth) | |
print("Chip Version:", eth.chip) | |
print("MAC Address:", [hex(i) for i in eth.mac_address]) | |
print("My IP address is:", eth.pretty_ip(eth.ip_address)) | |
print("IP lookup adafruit.com: %s" % eth.pretty_ip(eth.get_host_by_name("adafruit.com"))) | |
else: | |
print(f"Error: Network stack {stack}.") | |
### ^^^ Board-specific Network code | |
# Uncomment the following lines if you need NTP time synchronization: | |
# import adafruit_ntp | |
# ntp = adafruit_ntp.NTP(socket_pool, tz_offset=TZ_OFFSET + DST) | |
# rtc.RTC().datetime = ntp.datetime | |
# print(f"NTP Time: {rtc.RTC().datetime}") | |
return socket_pool, ssl_context |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment