-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect.py
39 lines (31 loc) · 1011 Bytes
/
connect.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
import network
import time
import machine
from lib.env import SSID, PASSWORD
led = machine.Pin('LED', machine.Pin.OUT)
def connect_to_wifi():
try:
wlan = network.WLAN(network.STA_IF)
wlan.disconnect()
time.sleep(2)
wlan.active(True)
wlan.connect(SSID, PASSWORD)
print("Connecting ...")
while not wlan.isconnected():
led.on()
print("Waiting for Connection ...")
time.sleep(0.75)
led.off()
print(wlan.status())
if wlan.status() <= -1:
raise Exception("Unable to connect to an Access Point.")
time.sleep(0.25)
print(f"Connected on: {wlan.ifconfig()[0]}")
led.off()
except Exception as e:
print(f"An unexpected error has occured: {e}")
def disconnect_wifi():
wlan = network.WLAN(network.STA_IF)
wlan.disconnect()
print(wlan.status())
print("Disconnected from WiFi.")