The release of Raspberry Pi Pico W brings with it an interesting opportunity . In the past, if we wanted to connect the Raspberry Pi to the world, we needed one of the larger models. Raspberry Pi Zero 2 W and Raspberry Pi 4 was often forced to collect data. The Raspberry Pi 4 is a little power hungry, the Zero 2 W is a little better, but still overkill for a simple data project.
With the advent of the Raspberry Pi Pico W, we have low power consumption. , a microcontroller with a smart Wi-Fi chip, in a Pico form factor and only $6!
So, where do we start? How can we get our Raspberry Pi Pico W online and where can we find interesting data to collect? Let us help you get the most out of your $6 Raspberry Pi Pico W.
Getting your Raspberry Pi Pico W online
The Raspberry Pi Pico W comes with an Infineon CYW43439 2.4GHz Wi-Fi chip and a built-in antenna. This means we get good Wi-Fi reception without having to run a lot of wires. We use the latest version of MicroPython for Pico W because it offers the easiest way to get online and do interesting projects.
1. Set up your Raspberry Pi Pico W following the instructions manual getting started. You will need to install MicroPython on Pico W before proceeding.
2. Open a blank document in Thonny editor.
3. Create an object named SSID and store the SSID of your Wi-Fi access point in it.
SSID = "YOUR WIFI AP"
4. Create an object named PASSWORD and store your Wi-Fi password.
PASSWORD = "TRUSTNO1"
5. Save the file on your Raspberry Pi Pico W as secrets.py. By storing our sensitive data in a secrets file, we can freely share the project’s code with friends or on the Internet. Just remember not to share the secrets file either.
6. Click New File to create a new blank document.
7. Import three modules: 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 networkimport secretsimport time
8.Create a wlan object to create a connection from our code to the Pico W wireless chip.We use this connection to run commands that will connect and test our Wi-Fi connection.
wlan = network .WLAN(network.STA_IF)
9. Turn on your Raspberry Pi Pico W’s Wi-Fi.
10. Connect to the router using the SSID and PASSWORD stored in the secrets.py file.
11. Print the status of the connection to the Python shell. It will print True if the connection is established and False if the connection failed.
12. Click Save and then select “Raspberry Pi Pico”. Save the file as Wi-Fi.py on Raspberry Pi Pico W.
13. Click Run to run the code.
14. Search the Python shell for True or False. True means we’re connected.
Complete Code Listing
import networkimport secretsimport timewlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(secrets.SSID, secrets.PASSWORD)print(wlan.isconnected() )< /code>
Using Raspberry Pi Pico W with external data
Now that we have an Internet connection, we will use it with public datasets to pull data from external sources and display it on the Pico W. This example, we are going to use Op en Notify ’s “How many people are in space right now” data set. Here are the numbers and names of all the astronauts currently on the International Space Station.
We are going to adapt our previous code example, 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, which will open the astronauts object, and look for the key ‘number’. The value associated with this key is then stored in the number object.
number = astronauts['number']
4. Create a for loop that will iterate over the number of people on the International Space Station. This value can change as astronauts come and go, so we use real-time data instead of hardcoding the value.
for i in range(number):
5. Type the name of each astronaut on the International Space Station using the row of data-specific keys. Our ‘astronauts’ has many keys, but we are interested in ‘people’, meaning “i” will increase each time the loop iterates and it selects each person from the list embedded in the dataset. We then use another key “name” to get the name of that astronaut.
6. Save the code and when you’re ready, click “Run” to run the code.
7. The names of all International Space Station astronauts will appear in the Python shell. Note that “True” still appears, confirming that our internet connection has been established.
Complete Code Listing
import networkimport secretsimport timeimport urequestswlan = 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'])