How to connect Raspberry Pi Pico W to the Internet using CircuitPython
Get ready to take your Raspberry Pi Pico W to the next level! In this guide, we'll show you a way to connect your board to the vast world of the internet using CircuitPython. With just a few easy steps, you'll be up and running in no time, unlocking a whole new world of possibilities for your DIY projects. So, let's dive in and get connected!
What we need
- Raspberry Pi Pico W board
- Micro USB cable to connect Pi Pico to the computer
Skip this step if you have already installed CircuitPython 8+
Follow the linked guide to install the necessary tools and CircuitPython 8 or greater to your Raspberry Pi Pico W dev board.
CircuitPython 8 allows for easy configuration of environment variables. This guide will demonstrate the use of settings.toml file to set up wifi credentials, namely the name and password.
Create settings.toml file
After installing and preparing CircuitPython, the next step is to create a new file named "settings.toml". Simply add the following data to this file:
# SSID is what you see as your wifi name # Replace MY_WIFI_SSID with your wifi name WIFI_SSID = "MY_WIFI_SSID" # Replace MY_WIFI_PASSWORD with you wifi password WIFI_PASSWORD = "MY_WIFI_PASSWORD"
Time to connect to the wifi
For the first test we will import our settings and try to connect to the wifi.
Add the following code in code.py, this code will connect to the wifi and print your MAC and IP address if successful.
import os, time, sys, ipaddress, wifi print("Connecting to WiFi") try: # Connect to the WIFI, use settings.toml to configure SSID and Password wifi.radio.connect(os.getenv('WIFI_SSID'), os.getenv('WIFI_PASSWORD')) print("Connected to WiFi") except Exception as e: # Handle connection error # For this example we will simply print a message and exit the program print("Failed to connect, adorting.") print("Error:\n", str(e)) sys.exit() # Prints your MAC (Media Access Control) address print("MAC addr:", [hex(i) for i in wifi.radio.mac_address]) # Prints your IP address print("IP address is", wifi.radio.ipv4_address)
You should see your MAC and IP address printed on the shell.
Now that we have connected to the wifi, let's try to see if we can connect and retrieve some data from the Internet.
Fetch something from the Internet
In the following example, we'll be connecting to a test time server that returns UTC time in JSON format. To simplify the process of working with HTTP(S) requests, we'll be utilizing the adafruit_requests external library. With this handy library, sending and receiving data will be a breeze!
Download and copy adafruit_requests.mpy to your Raspberry Pi Pico W board under the lib/ folder.
We will take the previous example from this page and extend it to fetch and print UTC time.
Download all files
Be sure to check out the inline comments for further explanation. If you have any questions or would like clarification on any of the steps, don't hesitate to leave a comment.
Hope you have learned something new today.