Part 2 - Connect to a WiFi Network
To connect your Pi Pico W to a network, you must provide a network name (SSID) and password. However, directly using sensitive data like WiFi passwords in your code is risky. Therefore, creating a separate file to hold your personal keys and passwords outside your code.py
file is a good idea. This way, you can share your code without revealing sensitive data to others.
-
Begin by clicking the
+
icon labeled New in the top toolbar of Mu Editor. -
Copy and paste the following code to the new file, replacing
network-name
with the name of your WiFi network andnetwork-passwd
with your network’s password.secrets = { "ssid": "network-name", "password": "network-passwd" }
-
Click the Save button in the toolbar and save the file on your
CIRCUITPY
drive using the namesecrets.py
. -
Enter your
code.py
file and replace the code you wrote in part one with the code below, then hit Save.import time import wifi import socketpool import ipaddress # Get WiFi details from your secrets.py file from secrets import secrets # Connect to WiFi using credentials from the secrets file wifi.radio.connect(secrets["ssid"], secrets["password"]) pool = socketpool.SocketPool(wifi.radio) print() print("WiFi connection established.") print("My MAC addres is:", ":".join("{:02X}".format(byte) for byte in wifi.radio.mac_address)) print("My IP address is", wifi.radio.ipv4_address) print() print("Pinging google.com at IP address 8.8.4.4") while True: ipv4 = ipaddress.ip_address("8.8.4.4") response = wifi.radio.ping(ipv4) if response is None: print(f"Ping failed. No response.") else: print(f"Ping successful. Response time: {response} ms") time.sleep(2)
-
Open Mu’s
Serial Monitor
to verify that the microcontroller successfully connects to the chosen network and can get a response fromgoogle.com
. -
Note the differences from the code you encountered in part one of this tutorial:
- An
import
statement for your newly createdsecrets.py
file has been added. This gives the code access to the network name (SSID) and password. - The content of the
while True:
loop has been replaced: It now pings google.com and reports the response time. This confirms internet connectivity for your microcontroller.
- An