-
Notifications
You must be signed in to change notification settings - Fork 0
/
wifi.py
45 lines (36 loc) · 1.09 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 network
import socket
import time
import re
import config
import machine
def wait_wlan(wlan):
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)
if wlan.status() != 3:
print("Failed setting up wifi, will restart in 30 seconds")
time.sleep(30)
machine.reset()
def setup_ap():
wlan = network.WLAN(network.AP_IF)
wlan.config(essid=config.WIFI_SSID, password=config.WIFI_PASSWORD)
wlan.active(True)
wait_wlan(wlan)
print('set up access point:', config.WIFI_SSID, 'with ip = ', wlan.ifconfig()[0])
return wlan
def connect_wlan():
wlan = network.WLAN(network.STA_IF)
wlan.active(False)
wlan.disconnect()
wlan.active(True)
wlan.connect(config.WIFI_SSID, config.WIFI_PASSWORD)
wait_wlan(wlan)
print('connected to wifi:', config.WIFI_SSID, 'with ip = ', wlan.ifconfig()[0])
return wlan
def run():
wlan = connect_wlan() if not config.WIFI_AP_MODE else setup_ap()