|
1 def connectWiFi(): |
|
2 import esp32 |
|
3 import network |
|
4 from time import sleep |
|
5 |
|
6 try: |
|
7 nvs = esp32.NVS("wifi_creds") |
|
8 buf = bytearray(1024) |
|
9 size = nvs.get_blob("ssid", buf) |
|
10 ssid = buf[:size].decode() |
|
11 size = nvs.get_blob("password", buf) |
|
12 password = buf[:size].decode() |
|
13 |
|
14 wifi = network.WLAN(network.STA_IF) |
|
15 wifi.active(False) |
|
16 wifi.active(True) |
|
17 wifi.connect(ssid, password) |
|
18 max_wait = 140 |
|
19 print("Connecting WiFi to '{0}'...".format(ssid)) |
|
20 while max_wait and wifi.status() == network.STAT_CONNECTING: |
|
21 max_wait -= 1 |
|
22 sleep(0.1) |
|
23 print("Connection status:", wifi.isconnected()) |
|
24 except: |
|
25 pass |
|
26 |
|
27 connectWiFi() |