Skip to content

Instantly share code, notes, and snippets.

@anecdata
Last active October 8, 2022 05:47
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
CircuitPython: Raspberry Pi Pico W + WIZnet W5100S Ethernet Hat
import os
import board
import busio
import digitalio
import time
import adafruit_wiznet5k.adafruit_wiznet5k
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
import adafruit_wiznet5k.adafruit_wiznet5k_socket as wiznet5k_socket
import wifi
import socketpool
import ssl
import adafruit_requests
from adafruit_requests import OutOfRetries
from secrets import secrets
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
#
# Raspberry Pi Pico W
#
led = digitalio.DigitalInOut(board.LED) # CYW0
led.switch_to_output(True)
wifi.radio.connect(secrets['ssid'], secrets['password'])
print("Pico W IP Address:", wifi.radio.ipv4_address)
#
# WIZnet W5100S Ethernet Hat
#
MAC_ADDR_5100S = (0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED)
spi = busio.SPI(board.GP18, MOSI=board.GP19, MISO=board.GP16)
eth_5100S_rst = digitalio.DigitalInOut(board.GP20)
eth_5100S_rst.direction = digitalio.Direction.OUTPUT
eth_5100S_cs = digitalio.DigitalInOut(board.GP17)
eth_5100S = WIZNET5K(spi, eth_5100S_cs, reset=eth_5100S_rst, mac=MAC_ADDR_5100S)
print("W5100S IP Address:", eth_5100S.pretty_ip(eth_5100S.ip_address))
print()
# Initialize a requests object with a Pico W wifi socket
pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())
print("Fetching text via Pico W from", TEXT_URL)
with requests.get(TEXT_URL) as r:
print(r.text)
# Initialize a requests object with an ethernet socket and interface
adafruit_requests.set_socket(wiznet5k_socket, eth_5100S)
print("Fetching text via W5100S from", TEXT_URL)
with adafruit_requests.get(TEXT_URL) as r:
print(r.text)
print("Done!")
@anecdata
Copy link
Author

anecdata commented Oct 8, 2022

board

@anecdata
Copy link
Author

anecdata commented Oct 8, 2022

Adafruit CircuitPython 8.0.0-beta.1-20-g66f84f5f9 on 2022-10-07; Raspberry Pi Pico W with rp2040
>>> 
soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Pico W IP Address: 192.168.6.205
W5100S IP Address: 192.168.6.250

Fetching text via Pico W from http://wifitest.adafruit.com/testwifi/index.html
This is a test of Adafruit WiFi!
If you can read this, its working :)
Fetching text via W5100S from http://wifitest.adafruit.com/testwifi/index.html
This is a test of Adafruit WiFi!
If you can read this, its working :)
Done!

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