|
1 try: |
|
2 import secrets |
|
3 |
|
4 def connectWiFi(): |
|
5 import network |
|
6 from time import sleep |
|
7 |
|
8 print("Connecting WiFi to '{0}' ...".format(secrets.WIFI_SSID)) |
|
9 wifi = network.WLAN(network.STA_IF) |
|
10 wifi.active(False) |
|
11 wifi.active(True) |
|
12 wifi.connect(secrets.WIFI_SSID, secrets.WIFI_KEY if secrets.WIFI_KEY else None) |
|
13 max_wait = 140 |
|
14 while max_wait: |
|
15 if wifi.status() < 0 or wifi.status() >= 3: |
|
16 break |
|
17 max_wait -= 1 |
|
18 sleep(0.1) |
|
19 if wifi.isconnected(): |
|
20 print("WiFi connected:", wifi.ifconfig()) |
|
21 else: |
|
22 print("WiFi connection failed") |
|
23 |
|
24 connectWiFi() |
|
25 except ImportError: |
|
26 print("WiFi secrets are kept in 'secrets.py', please add them there!") |