-
Notifications
You must be signed in to change notification settings - Fork 1
/
wifi.py
45 lines (34 loc) · 1.3 KB
/
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
37
38
39
40
41
42
43
44
45
import gc
import network
import time
from conf import settings
W_INTERFACE = network.WLAN(network.STA_IF)
def connect(retries=None):
"""
Starts the WLAN interface and makes a connection.
"""
STAT_MAP = {
network.STAT_IDLE: "Not currently connected",
network.STAT_CONNECTING: "Connecting",
network.STAT_WRONG_PASSWORD: "Incorrect password",
network.STAT_NO_AP_FOUND: "No access point found",
network.STAT_ASSOC_FAIL: "Connection failed",
network.STAT_HANDSHAKE_TIMEOUT: "Connection failed",
network.STAT_BEACON_TIMEOUT: "Connection failed",
network.STAT_GOT_IP: "Connection successful",
}
W_INTERFACE.active(True)
# Only make a few attempts to connect
W_INTERFACE.config(reconnects=retries or 3)
W_INTERFACE.connect(settings.WIFI_SSID, settings.WIFI_PASSWORD)
print("Connecting to WiFi...", end="")
while W_INTERFACE.status() == network.STAT_CONNECTING:
time.sleep(1)
print(".", end="")
status = W_INTERFACE.status()
print(STAT_MAP.get(status, "Unknown error: {}".format(status)))
if status != network.STAT_GOT_IP or not W_INTERFACE.isconnected():
W_INTERFACE.disconnect()
W_INTERFACE.active(False)
gc.collect()
# TODO: tests, handle disconnects, reconnect etc