The release of the Raspberry Pi Pico W brings with it an interesting opportunity. In the past, if we wanted to connect a Raspberry Pi to the world, we’d need one of the larger models. The Raspberry Pi Zero 2WAnd Raspberry Pi 4 they were often forced to perform data collection tasks. The Raspberry Pi 4 is a bit of a power hog, the Zero 2W is a bit better but still overkill for a simple informational 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 for just $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 walk you through how to get the most out of your $6 Raspberry Pi Pico W.
Get the Raspberry Pi Pico W online
The Raspberry Pi Pico W comes with an Infineon CYW43439 2.4GHz Wi-Fi chip and an integrated antenna. This means that we get good Wi-Fi reception without the need for a lot of cables. We are using the latest version of MicroPython for Pico W as it offers the easiest means to get online and make fun projects.
1. Set up your Raspberry Pi Pico W following our getting started guide. You will need to install MicroPython on your Pico W before you can proceed further.
2. Open the Thonny editor on 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 store your Wi-Fi password.
PASSWORD = "TRUSTNO1"
5. Save the file on 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. Just remember not to share the secrets file as well.
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 create a connection from our code to the Pico W wireless chip. We use this connection to issue commands that will connect and control our Wi-Fi connection.
wlan = network.WLAN(network.STA_IF)
9. Turn on the Raspberry Pi Pico W Wi-Fi.
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. This will print True if connected and False if the connection fails.
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 launch the code.
14. Search 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 Raspberry Pi Pico W with external data
Now that we have an internet connection, we’ll use it with publicly available datasets to pull data from external sources and visualize it on Pico W. We’ll use Open Notify’s “How many people are in space right now” for this example. data set. This has the numbers and names of all astronauts currently on the International Space Station.
We will adapt 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 item, number, which will open the astronauts item, and search for the ‘number’ key. The value linked to that key is then stored in the number object.
number = astronauts['number']
4. Create a for loop that will iterate for the number of people on the International Space Station. This value might change as astronauts come and go, so rather than hard-coding a value we use real-time data.
for i in range(number):
5. Print the name of each astronaut on the International Space Station using a series of keys that target specific data. Our dictionary ‘astronauts’ has many keys, but we are interested in ‘people’, the value of ‘i’ will increase each time the loop goes round and 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’re ready click Run to launch the code.
7. The names of all astronauts on the International Space Station will 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'])