The release of the Raspberry Pi Pico W presents an interesting opportunity. Back in the day, if we wanted to connect a Raspberry Pi to the world, we needed one of the larger models. The Raspberry Pi Zero 2 Wand Raspberry Pi 4 were often forced to collect data. The Raspberry Pi 4 is a bit of a power hog, the Zero 2 W is a bit better, but still overkill for a simple information project.
With the arrival of the Raspberry Pi Pico W, we have a low-power microcontroller with a competent Wi-Fi chip, in the Pico form factor and only $6!
So where do we start? How do we get our Raspberry Pi Pico W online and where can we find interesting data to collect? Let us guide you through getting the most out of your $6 Raspberry Pi Pico W.
Getting the Raspberry Pi Pico W Online
The Raspberry Pi Pico W comes with an Infineon CYW43439 2.4GHz WiFi chip and built-in antenna. This means that we get a good WiFi reception without the need for many cables. We’re using the latest MicroPython release for the Pico W because it’s the easiest way to get online and do fun projects.
1. Set up your Raspberry Pi Pico W by using our . to follow getting started guide† You must install MicroPython on your Pico W before you can proceed.
2. Open the Thony editor for a blank document.
3. Create an object called SSID and store the SSID of your Wi-Fi access point in it.
SSID = "YOUR WIFI AP"
4. Create an object called PASSWORD and save your WiFi password.
PASSWORD = "TRUSTNO1"
5. Save the file on the Raspberry Pi Pico W as secrets.py By storing our sensitive data in a secret file, we can freely share the project code with friends or online. Don’t forget not to share the secret file either.
6. Click New File to create a new blank document.
7. Import three modules of code, network, secrets and time. These three modules allow our Pico to connect to a Wi-Fi network, use the data stored in secrets.py and add a pause to the code.
import network import secrets import time
8. Create an object, wlan, to make a connection from our code to the Pico W wireless chip. We use this connection to issue commands that connect and check our Wi-Fi connection.
wlan = network.WLAN(network.STA_IF)
9. Turn on the WiFi of the Raspberry Pi Pico W.
10. Connect to your router using the SSID and PASSWORD stored in the secrets.py file.
11. Print the connection status to the Python shell. Prints True if the connection is successful and False if the connection has failed.
12. Click Save and then select “Raspberry Pi Pico”. Save the file as Wi-Fi.py on the Raspberry Pi Pico W.
13. Click Run to start the code.
14. Search the Python Shell for True or False. True means we are connected.
Full code list
import network import secrets import time wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(secrets.SSID, secrets.PASSWORD) print(wlan.isconnected())
Using the Raspberry Pi Pico W with external data
Now that we have an internet connection, we will use it with publicly available datasets to pull data from external sources and display it on the Pico W. For this example, we’re going to use Open Notify’s “How many people are in the room now” dataset. It lists the number and names of all the astronauts currently on the International Space Station.
We’re going to modify our previous sample code, Wi-Fi.py.
1. Add a line after “import time” and import the urequests module. This module allows us to work with network requests such as HTTP and JSON.
astronauts = urequests.get("http://api.open-notify.org/astros.json").json()
3. Create an object, number, that opens the astronaut object and search for the key ‘number’. The value associated with that key is then stored in the number object.
number = astronauts['number']
4. Create a for loop that iterates for the number of people on the International Space Station. This value can change as astronauts come and go, so instead of hard-coding a value, we’re using the live data.
for i in range(number):
5. Print the name of each astronaut on the International Space Station using a set of keys that target the specific data. Our dictionary ‘astronauts’ has many keys, but we are interested in the ‘people’, the value of ‘i’ will increase each time the loop goes around, and it selects each person from a list embedded in the dataset. We then use another key, ‘name’, to get that astronaut’s name.
6. Save the code and when you are done, click Run to start the code.
7. The names of all the astronauts on the International Space Station appear in the Python Shell. Note that “True” still appears, confirming that our internet connection has been established.
Full code list
import network import secrets import time import urequests wlan = network.WLAN(network.STA_IF) wlan.active(True) wlan.connect(secrets.SSID, secrets.PASSWORD) print(wlan.isconnected()) astronauts = urequests.get("http://api.open-notify.org/astros.json").json() number = astronauts['number'] for i in range(number): print(astronauts['people'][i]['name'])