-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
wlan_wrapper.py
43 lines (38 loc) · 1.24 KB
/
wlan_wrapper.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
import time
import network
wlan = None
def restart_wifi(ssid, key, hostname, timeout=None):
global wlan
if wlan is None:
return False
wlan.active(False)
time.sleep(1)
wlan.active(True)
wlan.connect(ssid, key)
start_time = time.time()
wlan.config(dhcp_hostname=hostname)
while not wlan.isconnected():
if timeout is not None:
if time.time()-start_time > timeout:
return False
print('network config:', wlan.ifconfig())
print('dhcp hostname', wlan.config('dhcp_hostname'))
return True
def init_wifi(ssid, key, hostname, timeout=None):
global wlan
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
while wlan.status() != network.STAT_IDLE:
print('Waiting ifup for wlan'.format(wlan.status()))
wlan.config(dhcp_hostname=hostname)
if not wlan.isconnected():
print('connecting to network...')
wlan.connect(ssid, key)
start_time = time.time()
while not wlan.isconnected():
if timeout is not None:
if time.time()-start_time > timeout:
return False
print('network config:', wlan.ifconfig())
print('dhcp hostname', wlan.config('dhcp_hostname'))
return True