iTranslated by AI
Connecting Raspberry Pi Pico W to Wi-Fi with MicroPython
The Raspberry Pi Pico W features a built-in Wi-Fi module, allowing it to connect to the internet with ease.
By connecting to the internet, you can send messages to cloud services like AWS IoT Core, store historical data, and build advanced IoT systems.
MicroPython Code
No special setup is required to connect to Wi-Fi. You simply need to execute the connection code within your MicroPython program. It is that straightforward.
Let's take a look at the MicroPython code.
Simply replace the SSID and PASSWORD in this code, and execute it to connect to your Wi-Fi.
import time
import network # type: ignore
time.sleep(10)
# Wi-Fi
SSID = "<Your SSID>"
PASSWORD = "<Your Password>"
def connect_wifi(ssid: str, password: str) -> network.WLAN:
try:
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connection
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 2:
break
max_wait -= 1
print("Waiting for connection...")
time.sleep(1)
# Check connection status
if wlan.status() != 3:
raise RuntimeError(f"Network connection failed: {wlan.status()}")
else:
print("Connected")
status = wlan.ifconfig()
print("IP address:", status[0])
return wlan
except Exception as e:
print(f"Failed to connect to Wi-Fi: {e}")
raise e
if __name__ == "__main__":
connect_wifi(SSID, PASSWORD)
Since this is simple code, I will explain only the wlan.status() part. wlan.status() returns the following values. The status is normal only when it returns 3; all other values indicate idle, connecting, or error states.
| Value | Constant | Description |
|---|---|---|
| 0 | STAT_IDLE | Idle (connection not attempted) |
| 1 | STAT_CONNECTING | Connecting |
| 2 | STAT_WRONG_PASSWORD | Incorrect password |
| 3 | STAT_GOT_IP | Connection successful, IP address obtained |
| -1 | STAT_NO_AP_FOUND | Specified AP not found |
| -2 | STAT_CONNECT_FAIL | Failed to connect for other reasons |
Wi-Fi Connection Persists After the Program Ends
I noticed after running it that the Wi-Fi connection is maintained even after the MicroPython program has finished execution.
Therefore, if you want to install any modules using the mip command (the MicroPython version of pip), you can follow this process: run the program above -> interrupt it with Ctrl-C -> execute mip.install().
Caution Regarding Storing Wi-Fi Passwords in Plain Text
In the code above, the Wi-Fi password is saved in plain text.
If you are placing the device in a location accessible to the general public, such as outdoors, you need to be mindful of security risks.
As far as I have researched, it is difficult to encrypt and use passwords on the Pico W alone, and it seems necessary to add external modules.
The following two measures are effective:
Summary
Because the Raspberry Pi Pico W has a built-in Wi-Fi module, it can easily connect to the internet, making it extremely useful as a device for IoT systems.
It is an amazing era where you can buy such a device for under 2,000 yen.
Since it is easy to create systems linked with the cloud, please give it a try.
AI・IoT・クラウドを中心に、実務で使える知見や検証結果を発信します。 お仕事のご相談はこちら: bloomblock.net/contact/
Discussion