-
Notifications
You must be signed in to change notification settings - Fork 24
/
wifi_manager.py
295 lines (249 loc) · 10.2 KB
/
wifi_manager.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
# Author: Igor Ferreira
# License: MIT
# Version: 2.1.0
# Description: WiFi Manager for ESP8266 and ESP32 using MicroPython.
import machine
import network
import socket
import re
import time
class WifiManager:
def __init__(self, ssid = 'WifiManager', password = 'wifimanager', reboot = True, debug = False):
self.wlan_sta = network.WLAN(network.STA_IF)
self.wlan_sta.active(True)
self.wlan_ap = network.WLAN(network.AP_IF)
# Avoids simple mistakes with wifi ssid and password lengths, but doesn't check for forbidden or unsupported characters.
if len(ssid) > 32:
raise Exception('The SSID cannot be longer than 32 characters.')
else:
self.ap_ssid = ssid
if len(password) < 8:
raise Exception('The password cannot be less than 8 characters long.')
else:
self.ap_password = password
# Set the access point authentication mode to WPA2-PSK.
self.ap_authmode = 3
# The file were the credentials will be stored.
# There is no encryption, it's just a plain text archive. Be aware of this security problem!
self.wifi_credentials = 'wifi.dat'
# Prevents the device from automatically trying to connect to the last saved network without first going through the steps defined in the code.
self.wlan_sta.disconnect()
# Change to True if you want the device to reboot after configuration.
# Useful if you're having problems with web server applications after WiFi configuration.
self.reboot = reboot
self.debug = debug
def connect(self):
if self.wlan_sta.isconnected():
return
profiles = self.read_credentials()
for ssid, *_ in self.wlan_sta.scan():
ssid = ssid.decode("utf-8")
if ssid in profiles:
password = profiles[ssid]
if self.wifi_connect(ssid, password):
return
print('Could not connect to any WiFi network. Starting the configuration portal...')
self.web_server()
def disconnect(self):
if self.wlan_sta.isconnected():
self.wlan_sta.disconnect()
def is_connected(self):
return self.wlan_sta.isconnected()
def get_address(self):
return self.wlan_sta.ifconfig()
def write_credentials(self, profiles):
lines = []
for ssid, password in profiles.items():
lines.append('{0};{1}\n'.format(ssid, password))
with open(self.wifi_credentials, 'w') as file:
file.write(''.join(lines))
def read_credentials(self):
lines = []
try:
with open(self.wifi_credentials) as file:
lines = file.readlines()
except Exception as error:
if self.debug:
print(error)
pass
profiles = {}
for line in lines:
ssid, password = line.strip().split(';')
profiles[ssid] = password
return profiles
def wifi_connect(self, ssid, password):
print('Trying to connect to:', ssid)
self.wlan_sta.connect(ssid, password)
for _ in range(100):
if self.wlan_sta.isconnected():
print('\nConnected! Network information:', self.wlan_sta.ifconfig())
return True
else:
print('.', end='')
time.sleep_ms(100)
print('\nConnection failed!')
self.wlan_sta.disconnect()
return False
def web_server(self):
self.wlan_ap.active(True)
self.wlan_ap.config(essid = self.ap_ssid, password = self.ap_password, authmode = self.ap_authmode)
server_socket = socket.socket()
server_socket.close()
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
server_socket.bind(('', 80))
server_socket.listen(1)
print('Connect to', self.ap_ssid, 'with the password', self.ap_password, 'and access the captive portal at', self.wlan_ap.ifconfig()[0])
while True:
if self.wlan_sta.isconnected():
self.wlan_ap.active(False)
if self.reboot:
print('The device will reboot in 5 seconds.')
time.sleep(5)
machine.reset()
self.client, addr = server_socket.accept()
try:
self.client.settimeout(5.0)
self.request = b''
try:
while True:
if '\r\n\r\n' in self.request:
# Fix for Safari browser
self.request += self.client.recv(512)
break
self.request += self.client.recv(128)
except Exception as error:
# It's normal to receive timeout errors in this stage, we can safely ignore them.
if self.debug:
print(error)
pass
if self.request:
if self.debug:
print(self.url_decode(self.request))
url = re.search('(?:GET|POST) /(.*?)(?:\\?.*?)? HTTP', self.request).group(1).decode('utf-8').rstrip('/')
if url == '':
self.handle_root()
elif url == 'configure':
self.handle_configure()
else:
self.handle_not_found()
except Exception as error:
if self.debug:
print(error)
return
finally:
self.client.close()
def send_header(self, status_code = 200):
self.client.send("""HTTP/1.1 {0} OK\r\n""".format(status_code))
self.client.send("""Content-Type: text/html\r\n""")
self.client.send("""Connection: close\r\n""")
def send_response(self, payload, status_code = 200):
self.send_header(status_code)
self.client.sendall("""
<!DOCTYPE html>
<html lang="en">
<head>
<title>WiFi Manager</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
</head>
<body>
{0}
</body>
</html>
""".format(payload))
self.client.close()
def handle_root(self):
self.send_header()
self.client.sendall("""
<!DOCTYPE html>
<html lang="en">
<head>
<title>WiFi Manager</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
</head>
<body>
<h1>WiFi Manager</h1>
<form action="/configure" method="post" accept-charset="utf-8">
""".format(self.ap_ssid))
for ssid, *_ in self.wlan_sta.scan():
ssid = ssid.decode("utf-8")
self.client.sendall("""
<p><input type="radio" name="ssid" value="{0}" id="{0}"><label for="{0}"> {0}</label></p>
""".format(ssid))
self.client.sendall("""
<p><label for="password">Password: </label><input type="password" id="password" name="password"></p>
<p><input type="submit" value="Connect"></p>
</form>
</body>
</html>
""")
self.client.close()
def handle_configure(self):
match = re.search('ssid=([^&]*)&password=(.*)', self.url_decode(self.request))
if match:
ssid = match.group(1).decode('utf-8')
password = match.group(2).decode('utf-8')
if len(ssid) == 0:
self.send_response("""
<p>SSID must be providaded!</p>
<p>Go back and try again!</p>
""", 400)
elif self.wifi_connect(ssid, password):
self.send_response("""
<p>Successfully connected to</p>
<h1>{0}</h1>
<p>IP address: {1}</p>
""".format(ssid, self.wlan_sta.ifconfig()[0]))
profiles = self.read_credentials()
profiles[ssid] = password
self.write_credentials(profiles)
time.sleep(5)
else:
self.send_response("""
<p>Could not connect to</p>
<h1>{0}</h1>
<p>Go back and try again!</p>
""".format(ssid))
time.sleep(5)
else:
self.send_response("""
<p>Parameters not found!</p>
""", 400)
time.sleep(5)
def handle_not_found(self):
self.send_response("""
<p>Page not found!</p>
""", 404)
def url_decode(self, url_string):
# Source: https://forum.micropython.org/viewtopic.php?t=3076
# unquote('abc%20def') -> b'abc def'
# Note: strings are encoded as UTF-8. This is only an issue if it contains
# unescaped non-ASCII characters, which URIs should not.
if not url_string:
return b''
if isinstance(url_string, str):
url_string = url_string.encode('utf-8')
bits = url_string.split(b'%')
if len(bits) == 1:
return url_string
res = [bits[0]]
appnd = res.append
hextobyte_cache = {}
for item in bits[1:]:
try:
code = item[:2]
char = hextobyte_cache.get(code)
if char is None:
char = hextobyte_cache[code] = bytes([int(code, 16)])
appnd(char)
appnd(item[2:])
except Exception as error:
if self.debug:
print(error)
appnd(b'%')
appnd(item)
return b''.join(res)