How to connect Raspberry Pi Pico W to the Internet using CircuitPython

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!

Code is licensed under the Open Source MIT License

What we need

  • Raspberry Pi Pico W board
  • Micro USB cable to connect Pi Pico to the computer

Install CircuitPython

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.

How to install MicroPython or CircuitPython
This guide will help you install MicroPython or CircuitPython on any compatible microcontroller (MCU for microcontroller unit) using a simple IDE (Integrated Development Environment) for the beginners in this awesome microcontroller Python world. It’s up to you to decide whether you want to work wi…

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 libraries

Download and copy adafruit_requests.mpy to your Raspberry Pi Pico W board under the lib/ folder.

These libraries are licensed under the Open Source MIT License

Extend code.py

We will take the previous example from this page and extend it to fetch and print UTC time.

import os, time, sys, ipaddress, ssl, wifi, socketpool
# Importing adafruit_requests makes it easy to work with http(s) requests
import adafruit_requests

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)

pool = socketpool.SocketPool(wifi.radio)
requests = adafruit_requests.Session(pool, ssl.create_default_context())

# We will be using a sample from jsontest.com which returns current UTC time as JSON
time_query = 'http://time.jsontest.com/'

while True:
    try:
        time_response = requests.get(url=time_query)
        json_time_response = time_response.json()
        print()
        print('UTC time')
        print(json_time_response)
        time.sleep(10)
    except Exception as e:
        print("Error:\n", str(e))
Complete example code.py

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.