How to Connect Raspberry Pi Pico W to the Internet
The Raspberry Pi Pico W comes with an Infineon CYW43439 2.4 GHz Wi-Fi chip and onboard antenna. This means we get good Wi-Fi reception without the need for lots of wires. We’re using the latest MicroPython release for the Pico W as it offers the easiest means to get online and do fun projects.
1. Setup your Raspberry Pi Pico W by 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 to a blank document.
3. Create an object called SSID and in it store the SSID of your Wi-Fi access point.
SSID = "YOUR WIFI AP"
4. Create an object called PASSWORD and store your Wi-Fi password.
PASSWORD = "TRUSTNO1"
5. Save the file to the Raspberry Pi Pico W as secrets.py
By storing our sensitive details in a secrets file, we can freely share the project code with friends, or online. Just remember not to share the secrets file too.
6. Click on New File
To create a new blank document.
7. Import three modules of code, network, secrets and time.
These three modules enable our Pico to connect to a Wi-Fi network, use the data stored in secrets.py and to 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 check our Wi-Fi connection.
wlan = network.WLAN(network.STA_IF)
9. Turn on the Raspberry Pi Pico W’s Wi-Fi.
wlan.active(True)
10. Connect to your router using the SSID and PASSWORD stored in the secrets.py file.
wlan.connect(secrets.SSID, secrets.PASSWORD)
11. Print the connection status to the Python shell.
This will print True if connected, and False if the connection failed.
12. Click Save and then select “Raspberry Pi Pico”.
Save the file as Wi-Fi.py to the Raspberry Pi Pico W.
13. Click on Run to start the code.
14. Look in the Python Shell for True or False.
True means we are connected.
Complete Code Listing
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 are going to use Open Notify’s “How many people are in space right now” dataset. This has the number and names of all the astronauts currently on the International Space Station.
We are going to adapt our previous example code, Wi-Fi.py.
1. Add a line after “import time” and import the urequests module.
This module enables us to work with network requests such as HTTP and JSON.
import urequests
2. After print(wlan.isconnected()) add a new line which creates an object “astronauts” and then uses urequests to get the information in a JSON format.
JavaScript Object Notation is an open standard file format which bears a striking resemblance to Python’s Dictionary which uses keys (names) to retrieve values from the object.
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 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 could change as astronauts come and go, so rather than hard coding a value we use the live data.https://vanilla.futurecdn.net/tomshardware/media/img/missing-image.svg
for i in range(number):
5. Print the name of each astronaut on the International Space Station using a series 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 increment each time the loop goes round, and it selects each person from a list embedded in the dataset. We then use another key, ‘name’ to get the name of that astronaut.
print(astronauts['people'][i]['name'])
6. Save the code and when ready click on Run to start the code.
7. The names of all the astronauts on the International Space Station will appear in the Python Shell.
Note that “True” still appears, confirming that our Internet connection is established.
Complete Code Listing
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'])
Read more: How to Connect Raspberry Pi Pico W to the Internet