-
Notifications
You must be signed in to change notification settings - Fork 9
/
wifi_helper.py
487 lines (402 loc) · 15.7 KB
/
wifi_helper.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
"""
WiFi Helper
Connect to specified network(s) or create an accesspoint
"""
# import ubinascii
import json
from machine import machine
from . import network
import time
from collections import namedtuple
from time_helper import TimeHelper
# not natively supported on micropython, see lib/typing.py
from typing import (List, NamedTuple, Union)
class WifiHelper(object):
"""docstring for WifiHelper"""
def __init__(self):
self._scan_info = [
'ssid', 'bssid', 'channel', 'RSSI', 'authmode', 'hidden'
]
self._auth_modes = {
0: "open",
1: "WEP",
2: "WPA-PSK",
3: "WPA2-PSK",
4: "WPA/WPA2-PSK"
}
self._network_list = list()
self._station = network.WLAN(network.STA_IF)
@staticmethod
def _do_connect(station: network.WLAN,
ssid: str,
password: str,
timeout: int) -> bool:
"""
Establish the network connection.
:param station: The network object
:type station: network.WLAN
:param ssid: The SSID of the network to connect to
:type ssid: str
:param password: The password of the network
:type password: str
:param timeout: Seconds to establish a connection to the network
:type timeout: int, optional
:returns: Result of connection
:rtype: bool
"""
is_successfull = False
print('Connect to network "{}" with password "{}"'.
format(ssid, '*' * 8))
# WiFi connection remains after a soft reset
if machine.reset_cause() == machine.SOFT_RESET:
print('Soft reset, checking existing WiFi connection')
if station.isconnected():
print('System already connected')
is_successfull = True
return is_successfull
try:
station.disconnect()
time.sleep(250 / 1000)
station.connect(ssid, password)
except Exception as e:
print('Failed to connect due to: {}'.format(e))
return is_successfull
# get current system timestamp in seconds since 01/01/2000
now = time.time()
# wait for connection no longer than the specified timeout
while (time.time() < (now + timeout)):
if station.isconnected():
is_successfull = True
return is_successfull
else:
pass
time.sleep(100 / 1000)
return is_successfull
@staticmethod
def connect(ssid: Union[None, List[str], str] = None,
password: Union[None, List[str], str] = None,
networks: Union[dict, None] = None,
timeout: int = 5,
reconnect: bool = False) -> bool:
"""
Connect to the configured network
:param ssid: The SSID of the network to connect to
:type ssid: list or str
:param password: The password of the network
:type password: list or str
:param networks: Networks and passwords
:type networks: dict, optional
:param timeout: Seconds to establish a connection to the network
:type timeout: int, optional
:param reconnect: Reconnect/disconnect from active connection
:type reconnect: bool, optional
:returns: Result of connection
:rtype: bool
"""
is_connected = False
# configure the WiFi as station mode (client)
station = network.WLAN(network.STA_IF)
# activate WiFi if not yet enabled
if not station.active():
station.active(True)
if station.isconnected():
current_network = station.config('essid')
print('Already connected to "{}"'.format(current_network))
if reconnect:
station.disconnect()
print('Disconnected from "{}"'.format(current_network))
else:
is_connected = True
th = TimeHelper()
th.sync_time()
print(station.ifconfig())
return is_connected
# get current system timestamp in seconds since 01/01/2000
now = time.time()
if ((type(ssid) is str) and (type(password) is str)):
# user provided string of single network to connect to
print('Connect by single network and password')
is_connected = WifiHelper._do_connect(station=station,
ssid=ssid,
password=password,
timeout=timeout)
print('Connected to {}: {}'.format(ssid, is_connected))
elif ((type(ssid) is list) and
(type(password) is list)):
# user provided list of networks to connect to
print('Connect by list of networks and passwords')
for idx, s in enumerate(ssid):
is_connected = WifiHelper._do_connect(station=station,
ssid=s,
password=password[idx],
timeout=timeout)
print('Connected to {}: {}'.format(s, is_connected))
if is_connected:
break
elif ((networks is not None) and
(type(networks) is dict)):
# user provided dict of networks and passwords
print('Connect by dict of networks and passwords')
for ssid, password in networks.items():
is_connected = WifiHelper._do_connect(station=station,
ssid=ssid,
password=password,
timeout=timeout)
print('Connected to {}: {}'.format(ssid, is_connected))
if is_connected:
break
else:
print('SSID and/or password neither list nor string')
print('Stopped trying to connect to network after {} seconds'.
format(time.time() - now))
if is_connected:
print('Connection successful')
th = TimeHelper()
th.sync_time()
else:
print('Connection timeout of failed to connect')
print('Please check configured SSID and password')
print(station.ifconfig())
# return True if connection has been established
return is_connected
@property
def isconnected(self) -> bool:
"""
Return whether device is connected as client to a network
:returns: Result of connection status
:rtype: bool
"""
# configure the WiFi as station mode (client)
station = self.station
return station.isconnected()
@property
def station(self):
"""
Return WiFi network station aka client interface object
:returns: WiFi network station aka client interface object
:rtype: WLAN
"""
return self._station
@staticmethod
def create_ap(ssid: str,
password: str = '',
channel: int = 11,
timeout: int = 5) -> bool:
"""
Create an Accesspoint
:param ssid: The SSID of the network to create
:type ssid: str
:param password: The password of the accesspoint
:type password: str, optional
:param channel: The channel of the accesspoint
:type channel: int, optional
:param timeout: Seconds to create an accesspoint
:type timeout: int, optional
:returns: Result of connection
:rtype: bool
"""
is_successfull = True
# configure the WiFi as accesspoint mode (server)
accesspoint = network.WLAN(network.AP_IF)
# activate accesspoint if not yet enabled
if not accesspoint.active():
accesspoint.active(True)
# check for open AccessPoint configuration
if len(password) > 7:
# WPA and WPA2 passwords can range from 8 to 63 characters
_authmode = network.AUTH_WPA_WPA2_PSK
else:
# in case a to short/long password has been given, set it to empty
_authmode = network.AUTH_OPEN
if len(password):
print('Invalid WPA/WPA2 password')
password = ''
print('Create AccessPoint "{}" with password "{}"'.
format(ssid, password))
accesspoint.config(essid=ssid,
authmode=_authmode,
password=password,
channel=channel)
# get current system timestamp in seconds since 01/01/2000
now = time.time()
# wait for success no longer than the specified timeout
while (time.time() < (now + timeout)):
if accesspoint.active():
is_successfull = True
break
else:
pass
print('Stopped trying to setup AccessPoint after {} seconds'.
format(time.time() - now))
if is_successfull:
print('AccessPoint setup successful')
else:
print('Connection timeout, failed to setup AccessPoint')
print(accesspoint.ifconfig())
return is_successfull
@property
def scan_info(self) -> list:
"""
Get WiFi scan informations.
:returns: WiFi scan properties
:rtype: list
"""
return self._scan_info
@property
def auth_modes(self) -> dict:
"""
Get authentication modes.
:returns: Supported authentication modes
:rtype: dict
"""
return self._auth_modes
def scan_networks(self) -> None:
"""
Scan for available WiFi networks and return found networks.
:param sort_key: The sort key
:type sort_key: str, optional
:returns: The WiFi networks
:rtype: List[dict]
"""
station = self.station
# activate WiFi if not yet enabled
if not station.active():
station.active(True)
# scan for available networks
found_networks = list()
try:
found_networks = station.scan()
if not len(found_networks):
station.active(False)
except RuntimeError:
print('RuntimeError during scan')
# no access points were found.
# RuntimeError: Wifi Unknown Error 0x0102
except Exception as e:
print('Unknown exception: {}'.format(e))
# create dict based on scan_info and found networks
self._network_list = [
dict(zip(self.scan_info, x)) for x in found_networks
]
for net in self._network_list:
net['quality'] = self.dbm_to_quality(dBm=net['RSSI'])
if 'authmode' in net:
try:
net['authmode'] = self.auth_modes[net['authmode']]
except KeyError:
# print('{} is unknown authmode'.format(net['authmode']))
pass
except Exception:
pass
if 'bssid' in net:
try:
net['bssid'] = net['bssid']
except Exception:
pass
@property
def networks(self) -> List[NamedTuple]:
"""
Get list of available networks
:returns: List of NamedTuple networks
:rtype: List[NamedTuple]
"""
return [namedtuple('net', list(net.keys()))(*net.values()) for net in self._network_list] # noqa: E501
def get_wifi_networks_sorted(self,
rescan: bool = False,
scan_if_empty: bool = False,
as_json: bool = False,
sort_key: str = 'RSSI') -> Union[List[dict],
str]:
"""
Get the available WiFi networks in sorted order.
:param rescan: Flag to scan before returning result
:type rescan: bool
:param scan_if_empty: Flag to scan if network list is empty
:type scan_if_empty: bool
:param as_json: Flag to return data as json string
:type as_json: bool
:param sort_key: The sort key
:type sort_key: str
:returns: The available WiFi networks in sorted order.
:rtype: Union[List[dict], str]
"""
if (scan_if_empty and not len(self.networks)) or rescan:
self.scan_networks()
if not (sort_key in self.scan_info):
sort_key = 'RSSI'
# sort list by specified sort_key
sorted(self._network_list, key=lambda k: k[sort_key])
if as_json:
return json.dumps(self._network_list)
else:
return self._network_list
@staticmethod
def dbm_to_quality(dBm: int) -> int:
"""
Convert dBm to quality
:param dBm: The dBm [-100, -50]
:type dBm: int
:returns: Quality index in percent [0, 100]
:rtype: int
"""
# https://stackoverflow.com/questions/15797920/how-to-convert-wifi-signal-strength-from-quality-percent-to-rssi-dbm/30585711
"""
quality = 0
if dBm <= -100:
# very bad signal
quality = 0
elif dBm >= -50:
quality = 100
else:
quality = 2 * (dBm + 100)
return quality
"""
return min(max(2 * (dBm + 100), 0), 100)
@staticmethod
def quality_to_dbm(quality: int) -> int:
"""
Convert quality to dBm
:param quality: The quality [0, 100]
:type quality: int
:returns: dBm in range [-100, -50]
:rtype: int
"""
# https://stackoverflow.com/questions/15797920/how-to-convert-wifi-signal-strength-from-quality-percent-to-rssi-dbm/30585711
"""
dBm = 0
if quality <= 0:
# very bad signal
dBm = -100
elif quality >= 100:
dBm = -50
else:
dBm = (quality / 2) - 100
return dBm
"""
return min(max((quality / 2) - 100, -100), -50)
@property
def ifconfig_client(self):
"""
Get current network interface parameters of the client
:returns: WiFi or general network informations
:rtype: NamedTuple
"""
empty_config = ('0.0.0.0', '0.0.0.0', '0.0.0.0', '0.0.0.0')
_ifconfig = namedtuple('ifconfig', ('ip', 'subnet', 'gateway', 'dns'))
station = self.station
if station.active() and station.isconnected():
return _ifconfig(*station.ifconfig())
else:
return _ifconfig(*empty_config)
@property
def ifconfig_ap(self):
"""
Get current network interface parameters of the accesspoint
:returns: WiFi or general network informations
:rtype: NamedTuple
"""
_ifconfig = namedtuple('ifconfig', ('ip', 'subnet', 'gateway', 'dns'))
ap = network.WLAN(network.AP_IF)
return _ifconfig(*ap.ifconfig())