-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwlan.py
173 lines (158 loc) · 5.02 KB
/
wlan.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import machine
import time
import os
from network import WLAN
import socket
import binascii
known_nets_dict = {
# 'ssid2': {'pwd': 'password2', 'wlan_config': ('10.0.0.114', '255.255.0.0', '10.0.0.1', '10.0.0.1')}, # (ip, subnet_mask, gateway, DNS_server)
'openwireless.org': {'pwd': '', 'sec': 0},
}
try:
import wlan_config
except:
print('failed to import wlan_config')
w = None
connection = ""
IP = ""
def wlan_init():
global w
if w is not None:
#print('already initialized')
return w
print("Initialise WiFi")
w = WLAN() #antenna=WLAN.EXT_ANT)
try:
w.hostname(binascii.hexlify(machine.unique_id()) + "." + os.uname().sysname + "." + "w")
# hostname is not available in 1.18.2
except:
pass
# print("ap_mac", binascii.hexlify(w.mac().ap_mac))
# print("sta_mac", binascii.hexlify(w.mac().sta_mac))
if ( w.mode() == WLAN.AP ):
print("switch from AP to STA_AP")
w.mode(WLAN.STA_AP)
# original_ssid = w.ssid()
# original_auth = w.auth()
return w
def wlan_isconnected():
return w.isconnected()
def wlan_waitconnected(timeout_s):
if w is None:
print("wlan not initialized")
return False
t = time.ticks_ms()
ct = 0;
# while not w.isconnected():
# ct += 1
# print(".", end="")
# machine.idle() # save power while waiting
# time.sleep_ms(200)
print('Connecting: -', end='')
while True:
if w.isconnected():
print()
break
if timeout_s is not None and time.ticks_ms() - t > (timeout_s * 1000):
print("\nTimeout")
return False
ct += 1
x = ct % 3
if x == 0:
print('\b\\', end='')
elif x == 1:
print('\b/', end='')
elif x == 2:
print('\b-', end='')
time.sleep_ms(100)
print("Connected in", round((time.ticks_ms() - t) /1000,1), "seconds") # , "(", w.ifconfig(), ")")
connection = "WLAN"
ct = 0
IP = w.ifconfig()[0]
while IP == "0.0.0.0":
ct += 1
machine.idle()
IP = w.ifconfig()[0]
time.sleep_ms(100)
print("Connected to", w.ssid(), #net_to_use,
binascii.hexlify(w.bssid()),
"with IP address:", IP, ct)
host = "detectportal.firefox.com"
print(host, socket.getaddrinfo(host, 80)[0][4][0])
return True
def wlan_quick(net = ''):
wlan_init()
if not net:
return wlan_quick('Pycom') or wlan_connect()
else:
# perform a quick connect, ie no scan
if w.isconnected():
print('already connected ({})'.format(w.ssid()))
return True
print("Quick connect", net)
k = known_nets_dict[net]
sec = k['sec']
pwd = k['pwd']
w.connect(net, ( sec, pwd ) )
return wlan_waitconnected(timeout_s=10)
def wlan_connect(timeout_s = 20):
global connection, IP, w
wlan_init()
if w.isconnected():
print("currently connected ... disconnecting")
w.disconnect()
while w.isconnected():
time.sleep_ms(100)
print("Scanning for wifi networks")
available_nets_list = w.scan()
available_ssids_set = frozenset([n.ssid for n in available_nets_list])
known_ssids_set = frozenset([key for key in known_nets_dict])
# make the intersection
usable_ssids_set = available_ssids_set & known_ssids_set
print("available:", len(available_ssids_set))
print("known:", len(known_ssids_set))
print("usable:", len(usable_ssids_set))
if (len(usable_ssids_set) == 0):
print("No usable network found")
print("known", known_ssids_set)
print("available", available_ssids_set)
else:
try:
net_to_use = usable_ssids_set.pop()
print("net_to_use", net_to_use)
net_properties = known_nets_dict[net_to_use]
pwd = net_properties['pwd']
sec = [e.sec for e in available_nets_list if e.ssid == net_to_use][0]
if 'wlan_config' in net_properties:
w.ifconfig(config=net_properties['wlan_config'])
#print("connect", net_to_use, sec) # , pwd)
w.connect(net_to_use, (sec, pwd))
return wlan_waitconnected(timeout_s)
except Exception as e:
print("Error while trying to connect to Wlan:", e)
return False
def wlan_ip():
wlan_init()
return w.ifconfig()[0]
def wlan_gw():
wlan_init()
return w.ifconfig()[2]
def wlan_ifconfig():
wlan_init()
c = w.ifconfig()# (ip, subnet_mask, gateway, DNS_server)
print("ifconfig", c)
return c
def wlan_deinit():
w.deinit()
if __name__ == "__main__":
import binascii
import os
uid = binascii.hexlify(machine.unique_id())
name = os.uname().sysname.lower() + '-' + uid.decode("utf-8")[-4:]
print(os.uname().sysname, uid, name, "main.py")
print("sys", os.uname().sysname)
print("unique_id", binascii.hexlify(machine.unique_id()))
wlan_connect()
if False:
wlan_quick()
machine.reset()