Wireless is wonderful, but sometimes you want the strong reliability of a wired connection. If your project is going to be part of a permanent installation, you may want to add Ethernet wired networking to your project.
Ethernet is incredibly easy to use - there's no network configuration or device pairing. Just plug a standard Ethernet cable into an Ethernet FeatherWing or Ethernet Shield and use the CircuitPython Wiznet5k library for quick and reliable networking.
Setup
First make sure you are running the latest version of Adafruit CircuitPython for your board.
Next you'll need to install the necessary libraries to use the hardware--carefully follow the steps to find and install these libraries from Adafruit's CircuitPython library bundle matching your version of CircuitPython. The Wiznet5k Library requires at least CircuitPython version 4.0.0. The latest version is recommended.
Before continuing, make sure your board's lib folder has at least the following files and folders copied over:
- adafruit_wiznet5k
- adafruit_bus_device
- adafruit_requests.mpy
- adafruit_connection_manager
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-License-Identifier: MIT import board import busio import digitalio import adafruit_connection_manager import adafruit_requests from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K print("Wiznet5k WebClient Test") TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" JSON_URL = "http://api.coindesk.com/v1/bpi/currentprice/USD.json" # For Adafruit Ethernet FeatherWing cs = digitalio.DigitalInOut(board.D10) # For Particle Ethernet FeatherWing # cs = digitalio.DigitalInOut(board.D5) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) # Initialize ethernet interface with DHCP eth = WIZNET5K(spi_bus, cs) # Initialize a requests session pool = adafruit_connection_manager.get_radio_socketpool(eth) ssl_context = adafruit_connection_manager.get_radio_ssl_context(eth) requests = adafruit_requests.Session(pool, ssl_context) 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")) ) # eth._debug = True print("Fetching text from", TEXT_URL) r = requests.get(TEXT_URL) print("-" * 40) print(r.text) print("-" * 40) r.close() print() print("Fetching json from", JSON_URL) r = requests.get(JSON_URL) print("-" * 40) print(r.json()) print("-" * 40) r.close() print("Done!")
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries # SPDX-FileCopyrightText: 2021 Adam Cummick # # SPDX-License-Identifier: MIT import board import busio import digitalio from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool print("Wiznet5k SimpleServer Test") # For Adafruit Ethernet FeatherWing cs = digitalio.DigitalInOut(board.D10) # For Particle Ethernet FeatherWing # cs = digitalio.DigitalInOut(board.D5) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) # Initialize ethernet interface eth = WIZNET5K(spi_bus, cs, is_dhcp=True) # Initialize a socket for our server pool = socketpool.SocketPool(eth) server = pool.socket() # Allocate socket for the server server_ip = eth.pretty_ip(eth.ip_address) # IP address of server server_port = 50007 # Port to listen on server.bind((server_ip, server_port)) # Bind to IP and Port server.listen() # Begin listening for incoming clients while True: print(f"Accepting connections on {server_ip}:{server_port}") conn, addr = server.accept() # Wait for a connection from a client. print(f"Connection accepted from {addr}, reading exactly 1024 bytes from client") with conn: data = conn.recv(1024) if data: # Wait for receiving data print(data) conn.send(data) # Echo message back to client print("Connection closed")
# SPDX-FileCopyrightText: 2023 Tim C for Adafruit Industries # SPDX-License-Identifier: MIT import board import digitalio from adafruit_httpserver import Server, Request, Response from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K import adafruit_wiznet5k.adafruit_wiznet5k_socketpool as socketpool print("Wiznet5k HTTPServer Test") # For Adafruit Ethernet FeatherWing cs = digitalio.DigitalInOut(board.D10) # For Particle Ethernet FeatherWing # cs = digitalio.DigitalInOut(board.D5) spi_bus = board.SPI() # Initialize ethernet interface with DHCP eth = WIZNET5K(spi_bus, cs) # Create a socket pool pool = socketpool.SocketPool(eth) # initialize the server server = Server(pool, "/static", debug=True) @server.route("/") def base(request: Request): """ Serve a default static plain text message. """ return Response(request, "Hello from the CircuitPython HTTP Server!") server.serve_forever(str(eth.pretty_ip(eth.ip_address)))
Network Time Protocol (NTP) Example
The following code looks for a WiFi capable chip. If it doesn't find one, it looks for the WizNet 5k library and sets up the microcontroller for an Ethernet SPI connection.
This code uses the adafruit_ntp
and adafruit_connection_manager
modules.
# SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries # SPDX-FileCopyrightText: 2024 anecdata for Adafruit Industries # # SPDX-License-Identifier: Unlicense """Print out time based on NTP, using connection manager""" import adafruit_connection_manager import adafruit_ntp # determine which radio is available try: import wifi import os # adjust method to get credentials as necessary... wifi_ssid = os.getenv("CIRCUITPY_WIFI_SSID") wifi_password = os.getenv("CIRCUITPY_WIFI_PASSWORD") radio = wifi.radio while not radio.connected: radio.connect(wifi_ssid, wifi_password) except ImportError: import board from digitalio import DigitalInOut from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K # adjust with busio.SPI() as necessary... spi = board.SPI() # adjust pin for the specific board... eth_cs = DigitalInOut(board.D10) radio = WIZNET5K(spi, eth_cs) # get the socket pool from connection manager socket = adafruit_connection_manager.get_radio_socketpool(radio) # adjust tz_offset for locale, only ping NTP server every hour ntp = adafruit_ntp.NTP(socket, tz_offset=-5, cache_seconds=3600) print(ntp.datetime)
Text editor powered by tinymce.