-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwifi.py
37 lines (30 loc) · 835 Bytes
/
wifi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""
Basic WIFI connection test on Raspberry Pi Pico W
"""
import time
import network
# Connection details stored on single space seperated line in a file on Pico format "SSID Password"
f = open("wifi.txt", "r")
wifi_details = f.readlines()
f.close()
ssid = wifi_details[0].split(" ")[0]
password = wifi_details[0].split(" ")[1]
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.connect(ssid, password)
# Wait for connect or fail
print("Connection to", ssid)
max_wait = 10
while max_wait > 0:
if wlan.status() < 0 or wlan.status() >= 3:
break
max_wait -= 1
print("waiting for connection...")
time.sleep(1)
# Handle connection error
if wlan.status() != 3:
raise RuntimeError("network connection failed")
else:
print("connected")
status = wlan.ifconfig()
print("ip = " + status[0])