Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active October 2, 2023 02:59
Show Gist options
  • Save anecdata/fbc57e96c8328127b791ebaf4a1a8e8c to your computer and use it in GitHub Desktop.
Save anecdata/fbc57e96c8328127b791ebaf4a1a8e8c to your computer and use it in GitHub Desktop.
CircuitPython WIZnet Ethernet Tester
import os
import board
import busio
import digitalio
import time
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as socket
import adafruit_wiznet5k.adafruit_wiznet5k
import adafruit_requests
MAC_ADDR = (0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
def eth_hard_reset(eth_rst, wait):
eth_rst.value = False
time.sleep(wait)
eth_rst.value = True
def eth_soft_reset(eth):
eth.sw_reset()
LED = True
if "EVB-Pico" in os.uname().machine: # W5100S-EVB-Pico or W5500-EVB-Pico
SPI0_SCK = board.GP18
SPI0_TX = board.GP19
SPI0_RX = board.GP16
SPI0_CSn = board.GP17
W5x00_RSTn = board.GP20
eth_rst = digitalio.DigitalInOut(W5x00_RSTn)
eth_rst.direction = digitalio.Direction.OUTPUT
print("Hard reset...")
eth_hard_reset(eth_rst, 1)
spi = busio.SPI(SPI0_SCK, MOSI=SPI0_TX, MISO=SPI0_RX)
eth_cs = digitalio.DigitalInOut(SPI0_CSn)
elif "Teensy" in os.uname().machine: # Teensy 4.0 w/Feather Adapter
eth_rst = None
spi = busio.SPI(board.SCK, MISO=board.MISO, MOSI=board.MOSI)
eth_cs = digitalio.DigitalInOut(board.A6)
LED = False # microcontroller.pin.GPIO_B0_03 board.D13 board.LED board.SCK
elif "Feather" in os.uname().machine:
eth_rst = None
spi = board.SPI()
if "RP2040" in os.uname().machine:
eth_cs = digitalio.DigitalInOut(board.D25)
else: # M4, FeatherS2
eth_cs = digitalio.DigitalInOut(board.A5)
if LED:
led = digitalio.DigitalInOut(board.LED)
led.direction = digitalio.Direction.OUTPUT
print("Init...")
while True:
# https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/issues/75
try:
eth = WIZNET5K(spi, eth_cs, reset=eth_rst, mac=MAC_ADDR)
break
except TypeError as e:
print(f"TypeError: {e}")
# eth._debug = True
print("Library Version:", adafruit_wiznet5k.adafruit_wiznet5k.__version__)
print("Chip Version:", eth.chip)
print("MAC Address:", ":".join("%02X" % _ for _ in eth.mac_address))
print("IP Address:", eth.pretty_ip(eth.ip_address))
print("ifconfig...")
print("IP Address: ", eth.pretty_ip(eth.ifconfig[0]))
print("Subnet Mask:", eth.pretty_ip(eth.ifconfig[1]))
print("Gateway: ", eth.pretty_ip(eth.ifconfig[2]))
print("DNS Server:", eth.ifconfig[3])
adafruit_requests.set_socket(socket, eth)
while True:
if LED: led.value = not led.value
print("Link Status:", eth.link_status)
# https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k/issues/75
try:
eth.maintain_dhcp_lease()
except TypeError as e:
print(f"TypeError: {e}")
print("Fetching text from", TEXT_URL)
start = time.monotonic()
with adafruit_requests.get(TEXT_URL) as r:
print(r.status_code, r.reason, r.headers)
print(r.text)
print(f"{time.monotonic() - start}s")
time.sleep(15)
@anecdata
Copy link
Author

anecdata commented Dec 8, 2022

  • Adafruit Feather M4 + Adafruit Ethernet FeatherWing
  • Adafruit Feather RP2040 + Adafruit Ethernet FeatherWing
  • UnexpectedMaker FeatherS2 + Adafruit Ethernet FeatherWing
  • Teensy 4.0 + Feather Adapter + Adafruit Ethernet FeatherWing
  • WIZnet W5100S-EVB-Pico (or Pico w/ WIZnet W5100S hat)
  • WIZnet W5500-EVB-Pico

@anecdata
Copy link
Author

anecdata commented Dec 8, 2022

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