Connecting to a WiFi Network
Welcome to WiFi!
In this lab you will connect your Pico W to a WiFi network for the first time. This is the most important skill in all the wireless labs!
Every WiFi network has two pieces of information you need. The first is the network name, called the Service Set Identifier (SSID). The second is the password. You need both to connect.
Setting Up Your WiFi secrets.py File
We never put passwords directly in our main code. Instead, we store them in a separate file called secrets.py. This keeps your password safe. Never share this file or check it into a public repository like GitHub.
Create a file called secrets.py on your Pico W with this content:
1 2 | |
Add secrets.py to your .gitignore file so it is never uploaded to GitHub.
You can then use the values in any program like this:
1 | |
Monty's Tip
Store your WiFi password in secrets.py — never put passwords directly in your main code. Keeping secrets separate is a great habit for every programmer!
Testing Your WiFi Access Point Connection
Here is a simple script to test whether your network name and password are correct. It connects to your WiFi access point. An access point is the router or hub that provides WiFi in your home or classroom.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
What each line does:
import network— loads the MicroPython networking libraryimport secrets— loads your WiFi name and passwordnetwork.WLAN(network.STA_IF)— creates a WiFi connection object in "station" (client) modewlan.active(True)— turns on the WiFi chipsleep(3)— waits 3 seconds for the chip to start upwlan.connect(...)— sends the network name and password to connectwlan.isconnected()— checks if the connection workedwlan.ifconfig()[0]— returns the Internet Protocol (IP) address assigned to your Pico W
Expected output:
1 2 3 4 5 | |
If you see Failure!, check your network name and password in secrets.py. Also make sure you have a strong WiFi signal where you are testing.
Watch Out!
The Pico W only connects to 2.4 GHz WiFi networks. It cannot connect to 5 GHz networks. If your router uses both, make sure your secrets.py has the 2.4 GHz network name.
Waiting for a Valid Access Point Connection
The simple script above uses fixed delays. But sometimes WiFi takes longer or shorter to connect. A better approach is to use a loop that keeps checking until we are connected.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
What each line does:
ticks_ms()— reads the current time in milliseconds, like a stopwatch startwhile not wlan.isconnected()— repeats the loop until we are connectedsleep(1)— waits one second between each checkticks_diff(...)— calculates how many milliseconds passed since we startedwlan.ifconfig()[0]— returns our IP address
First run output (when the chip is cold and needs a few seconds):
1 2 3 4 5 | |
Second run output (the connection is already cached, so it is instant):
1 2 3 4 | |
Error Handling
Sometimes the connection fails. Good programs handle errors gracefully. This version tries to connect for up to 10 seconds and raises an error if it fails.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | |
What each line does:
wlan.status()— returns a number showing the connection state (3 = connected, negative = error)raise RuntimeError(...)— stops the program and shows an error messagewlan.ifconfig()— returns a list: IP address, subnet mask, gateway, and DNS server
The full Transmission Control Protocol / Internet Protocol (TCP/IP) stack runs on your Pico W after connecting. You can ping the Pico W from another computer using the IP address shown.
You Can Do This!
Networking code can look complicated at first. That is completely normal. Once you have connected once, the rest of the wireless labs become much easier!
Great Work!
Your Pico W is now connected to the internet! Next, you will fetch real data from the web using an HTTP request.