Just like the original Raspberry Pi PicoThe new Raspberry Pi Pico 2 doesn’t have Wi-Fi or Bluetooth connectivity. The Pico 2 W is coming, confirmed by Raspberry Pi co-founder and CEO Eben Upton, but that’s still a few months away. What can you do in the meantime? Raspberry Pi Pico W?
Looking back through the archives, I remembered the original guide for adding Wi-Fi to a Raspberry Pi Pico back in 2021. This guidance is a bit out of date now, but Adafruit has updated CircuitPython and its modules to make it even easier to get online.
In this how-to, you will learn how to connect your Raspberry Pi Pico 2 to the Internet. Adafruit AirLift WiFi Feather WingThis board is essentially an ESP32, which is already a powerful Wi-Fi enabled microcontroller, so you might be asking “why?” Because there may be edge cases where you need the power of the new RP2350 over a Wi-Fi connection.
To demonstrate how you can use data from the Internet, we will create a simple and fun project that downloads some humorous “facts” about Chuck Norris, using an API (Application Programming Interface) that formats the returned data into a format called JSON.
Watch now
What you need for this project
- Raspberry Pi Pico 2 (project also works with Raspberry Pi Pico)
- Adafruit Airlift WiFi Featherwing Coprocessor
- 8 x Male to Male Jumper Wires
- Breadboard
Connecting a Raspberry Pi Pico 2 to Adafruit’s Airlift WiFi Featherwing
Adafruit’s Feather series of boards use a different pinout than the Raspberry Pi Pico 2, so you will need to solder header pins on the Raspberry Pi Pico 2 and the Featherwing Airlift. Since you will be using both boards on a breadboard, the included male headers will suffice.
If you are using the Featherwing with an Adafruit Feather board, such as the Feather RP2040, you will need to solder a female header to the top of the microcontroller. These should have come with the Adafruit Airlift WiFi Featherwing. We chose to solder the female header with extra long male header pins so that it would fit on a breadboard.
In the diagram, pay attention to the location of MISO, MOSI, and SCK. The Featherwing pinout has more pins on one side than the other, so check carefully before powering it up.
Raspberry Pi Pico 2 | Adafruit Airlift WiFi Featherwing Coprocessor |
---|---|
VSYS | USB |
ground | Any GND pin |
GPIO 10 | SCK |
GPIO 11 | Moshi |
GPIO 12 | miso |
GPIO 13 | ESPCS |
GPIO 14 | busy |
GPIO 15 | ESPRST |
Installing CircuitPython
Flashing CircuitPython onto your microcontroller is easy – we’ve designed it to be easy. The Raspberry Pi Pico 2 will use an alpha release of CircuitPython, but we don’t recommend deploying it in mission-critical projects as you may encounter issues and crashes – in that case, please wait for the full release.
1. download Latest version of CircuitPython for Pico 2. Please note that at the time of writing it was an alpha release, so it may crash from time to time.
2. Hold down the BOOTSEL button on the Pico and insert the USB cable into the Pico and into your computer.
3. Copy the CircuitPython UF2 files to the RP23502 driveThe drive will be unmounted and disappear while the code is flashed. A new drive, CIRCUITPY, will appear, confirming the flash was successful.
4. download Library Archives This depends on which version of CircuitPython you downloaded. For example, version 9.2.0 is only compatible with CircuitPython 9 libraries.
5. Extract the downloaded contents to a folder.
6. Copy the following files/folders to the lib folder on the CIRCUITPY drive:
/adafruit_bus_device
/adafruit_esp32spi
adafruit_requests.mpy
Writing the project code in CircuitPython
Our project is written in CircuityPython, Adafruit’s version of MicroPython. CircuitPython differs in that it allows easy installation of code modules for use with Adafruit’s various products. We will use Thonny to drive the CircuitPython code that communicates with the Adafruit Featherwing and retrieves data from the web.
1. Download and install Tony.
2. Go to Tools >> Options and select the Interpreter tab.
3. Set the interpreter to CircuitPython (generic) and the port to match your USB-Serial device (Pico 2).[OK]Click You can also try the auto port option to force Thonny to find the port.
4.Clicking STOP will force the interpreter to connect and run a Python shell on your Raspberry Pi Pico 2.
5. To create a project, you import a set of CircuitPython modules.
board: Basic GPIO access
Bushio: Used to create a SPI connection between the Pico 2 and an Adafruit board
digital: Used to get and set the GPIO pin configuration
adafruit_connection manager: Creating a network connection
Ada Fruit Request: A version of Adafruit’s Python requests library used to make web requests with network devices.
Adafruit_esp32spi: Used to communicate with the ESP32 on the Adafruit Featherwing
secret: A module to store Wi-Fi and country details. We will create this later.
import board
import busio
from digitalio import DigitalInOut
import adafruit_connection_manager
import adafruit_requests
from adafruit_esp32spi import adafruit_esp32spi
from secrets import secrets
6. Print a message to the Python shellTell users what the project is about.
print("Raspberry Pi Pico 2: Chuck Norris Joke-Randomizer")
7. Create an object to store URLs for Chuck Norris jokes. This can be the URL of any API or JSON source you want to use.
CHUCK_NORRIS_URL = "https://api.chucknorris.io/jokes/random"
8. Configure GPIO pins Used for SPI connection between Pico 2 and Adafruit Featherwing.
esp32_cs = DigitalInOut(board.GP13)
esp32_ready = DigitalInOut(board.GP14)
esp32_reset = DigitalInOut(board.GP15)
spi = busio.SPI(board.GP10, board.GP11, board.GP12)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
9. Configure the network infrastructure aspects of your code. These are all the behind-the-scenes technologies that make the connection possible.
pool = adafruit_connection_manager.get_radio_socketpool(esp)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
requests = adafruit_requests.Session(pool, ssl_context)
10. The project will print a message informing you that it will attempt to connect to Wi-Fi.
print("Connecting to AP...")
11. While the Adafruit Featherwing’s ESP32 is not connected, the code will attempt to connect using the Wi-Fi AP and password stored in the secrets.py file. You will create this file later.
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
12. Use these lines to handle connection errors. If an error occurs, the code will keep trying to connect.
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
13. If the connection is successful, print the connection details. These two lines print the access point name and the internal IP address of the project.
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
print("My IP address is", esp.ipv4_address,"\n")
14. Print a message Presenting Chuck Norris jokes.
print("Your Random Chuck Norris joke is... ")
15. We use an Adafruit request to get the latest jokes in JSON format and store them inside an object called “r”.
r = requests.get(CHUCK_NORRIS_URL).json()
16. Extract the jokes from the JSON object. JSON is very similar to Python’s dictionary data structure, which uses “keys” to extract “values” from objects. In this case, the keys are called “values”. It’s not a very good choice, as the names can be confusing, but you have no control over that.
joke = r['value']
17. It prints a joke enclosed within a frame made of “-“. You can use the len() command to get the length (the number of characters in the joke string) and use that to create a frame with the correct number of characters.
print("-" * len(joke))
print(joke)
print("-" * len(joke))
18. Save the code as code.py on your Raspberry Pi Pico 2. This will overwrite your existing code.py file and will cause the code to launch automatically every time the Pico 2 is powered on.
Complete code listing for code.py
import board
import busio
from digitalio import DigitalInOut
import adafruit_connection_manager
import adafruit_requests
from adafruit_esp32spi import adafruit_esp32spi
from secrets import secrets
print("Raspberry Pi Pico 2: Chuck Norris Joke-Randomizer")
CHUCK_NORRIS_URL = "https://api.chucknorris.io/jokes/random"
esp32_cs = DigitalInOut(board.GP13)
esp32_ready = DigitalInOut(board.GP14)
esp32_reset = DigitalInOut(board.GP15)
spi = busio.SPI(board.GP10, board.GP11, board.GP12)
esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset)
pool = adafruit_connection_manager.get_radio_socketpool(esp)
ssl_context = adafruit_connection_manager.get_radio_ssl_context(esp)
requests = adafruit_requests.Session(pool, ssl_context)
print("Connecting to AP...")
while not esp.is_connected:
try:
esp.connect_AP(secrets["ssid"], secrets["password"])
except OSError as e:
print("could not connect to AP, retrying: ", e)
continue
print("Connected to", esp.ap_info.ssid, "\tRSSI:", esp.ap_info.rssi)
print("My IP address is", esp.ipv4_address,"\n")
print("Your Random Chuck Norris joke is... ")
r = requests.get(CHUCK_NORRIS_URL).json()
joke = r['value']
print("-" * len(joke))
print(joke)
print("-" * len(joke))
Creating a secret file
The secrets.py file is a handy tool to store your Wi-Fi details and the country specific details you can use to connect to your Wi-Fi. By keeping the secrets separate from the main code, you can share the code.py file without worrying about sharing the secrets.
1. Create a new file on your Raspberry Pi Pico 2.
2. Create an object called “secrets” and Save Wi-Fi SSID, password, and time zone In an object, an object is a Python dictionary. The key identifies what the data stored in the value represents.
secrets = {
'ssid' : 'YOUR SSID',
'password' : 'YOUR PASSWORD',
'timezone' : YOUR TIMEZONE SEE THE URL >> ', # http://worldtimeapi.org/timezones
}
3. Save the file as secrets.py on your Raspberry Pi Pico 2.
Running the project code
Now we are ready to learn some valuable wisdom about Chuck Norris.
1. Switch back to the code.py tab.
2. Click Run >> Run Current Script. (Or press F5 or the green play button) to start the code.
The code runs in a Python shell and after a moment it connects to Wi-Fi and downloads the latest Chuck Norris jokes using a JSON API.