Connect Ethernet Cable
Make sure you have your Ethernet FeatherWing or Ethernet Shield firmly plugged into your hardware, and an Ethernet cable connected to your router or switch.
# 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!")
Save the code.py file and open the REPL.
If you don't get an IP address, check you have a green link light, and that your Ethernet is going out to an internet connected router. You may also have to set up the MAC address to allow it access, check with your system admin if you're not sure.
In order, the example code:
Initializes the Ethernet chipset over SPI using the SPI port and the CS pin.
cs = digitalio.DigitalInOut(board.D10) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) eth = WIZNET5K(spi_bus, cs)
Tells the requests
library the socket, and interface we'll be using. The interface is set to an eth
object. This is a little bit of a hack, but it lets us use requests
like CPython does.
requests.set_socket(socket, eth)
Verifies the Ethernet hardware was found, checks the chip version and MAC address.
print("Chip Version:", eth.chip) print("MAC Address:", [hex(i) for i in eth.mac_address])
Prints out the local IP and attempts to perform an IP address lookup for adafruit.com.
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")))
OK now we're getting to the really interesting part. With a SAMD51 or other large-RAM (well, over 32 KB) device, we can do a lot of neat tricks. Like, for example, we can implement an interface a lot like requests - which makes getting data really really easy
To read in all the text from a web URL, call requests.get
-
print("Fetching text from", TEXT_URL) r = requests.get(TEXT_URL) print('-'*40) print(r.text) print('-'*40) r.close()
Or, if the data is in structured JSON, you can get the json pre-parsed into a Python dictionary that can be easily queried or traversed. (Again, only for nRF52840, M4 and other high-RAM boards)
print() print("Fetching json from", JSON_URL) r = requests.get(JSON_URL) print('-'*40) print(r.json()) print('-'*40) r.close()
Manual Network Configuration
The Wiznet5k library automatically takes care of DHCP and DNS configuration, so you can get your project online quickly. However, there are cases where a network administrator will provide you with an IP address or you may need to manually configure the interface.
This code performs the same Ethernet simpletest as the above section, but allows you to configure your network interface.
# 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 TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html" # Setup your network configuration below IP_ADDRESS = (192, 168, 10, 1) SUBNET_MASK = (255, 255, 0, 0) GATEWAY_ADDRESS = (192, 168, 0, 1) DNS_SERVER = (8, 8, 8, 8) print("Wiznet5k WebClient Test (no DHCP)") cs = digitalio.DigitalInOut(board.D10) spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO) # Initialize ethernet interface without DHCP eth = WIZNET5K(spi_bus, cs, is_dhcp=False) # Set network configuration eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER) # 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()
You will need to manually set your network configuration:
-
Set
IP_ADDRESS
to your desired IP address. -
Set
SUBNET_MASK
to the router's subnet mask -
Set
GATEWAY_ADDRESS
to the router's IP address. -
Set
DNS_SERVER
to the DNS server you'd like to use. We're using 8.8.8.8 which is Google's Public DNS.
# Setup your network configuration below IP_ADDRESS = (192, 168, 10, 1) SUBNET_MASK = (255, 255, 0, 0) GATEWAY_ADDRESS = (192, 168, 0, 1) DNS_SERVER = (8, 8, 8, 8)
When the Ethernet interface is initialized, we'll disable the automatic DHCP process.
# Initialize ethernet interface without DHCP eth = WIZNET5K(spi_bus, cs, is_dhcp=False)
Then, we'll set up the network configuration by passing the ifconfig
property a tuple containing the IP address, subnet mask, gateway address and DNS server.
# Set network configuration eth.ifconfig = (IP_ADDRESS, SUBNET_MASK, GATEWAY_ADDRESS, DNS_SERVER)
Troubleshooting
If you are experiencing issues, customers have found success with connecting the RESET pin of the FeatherWing to a DigitalIO pin on their Feather.
For example, for a Raspberry Pi Pico, you would declare a reset
pin and then pass it to the WIZNET5K
class:
... reset = digitalio.DigitalInOut(board.GP20) eth = WIZNET5K(spi_bus, cs, reset, is_dhcp=False)
Page last edited April 01, 2025
Text editor powered by tinymce.