-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwifi_manager.py
234 lines (199 loc) · 8.96 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
# Author: Igor Ferreira
# License: MIT
# Version: 2.0.0
# Description: WiFi Manager for ESP8266 and ESP32 using MicroPython.
import machine
import network
import usocket
import ure
import utime
class WifiManager:
def __init__(self, ssid = 'WifiManager', password = 'wifimanager'):
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.sta_profiles = '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 = False
def connect(self):
if self.wlan_sta.isconnected():
return
profiles = self.__ReadProfiles()
for ssid, *_ in self.wlan_sta.scan():
ssid = ssid.decode("utf-8")
if ssid in profiles:
password = profiles[ssid]
if self.__WifiConnect(ssid, password):
return
print('Could not connect to any WiFi network. Starting the configuration portal...')
self.__WebServer()
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 __WriteProfiles(self, profiles):
lines = []
for ssid, password in profiles.items():
lines.append('{0};{1}\n'.format(ssid, password))
with open(self.sta_profiles, 'w') as myfile:
myfile.write(''.join(lines))
def __ReadProfiles(self):
try:
with open(self.sta_profiles) as myfile:
lines = myfile.readlines()
except OSError:
lines = []
pass
profiles = {}
for line in lines:
ssid, password = line.strip().split(';')
profiles[ssid] = password
return profiles
def __WifiConnect(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='')
utime.sleep_ms(100)
print('\nConnection failed!')
self.wlan_sta.disconnect()
return False
def __WebServer(self):
self.wlan_ap.active(True)
self.wlan_ap.config(essid = self.ap_ssid, password = self.ap_password, authmode = self.ap_authmode)
server_socket = usocket.socket()
server_socket.close()
server_socket = usocket.socket(usocket.AF_INET, usocket.SOCK_STREAM)
server_socket.setsockopt(usocket.SOL_SOCKET, usocket.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.')
utime.sleep(5)
machine.reset()
return
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 OSError:
# It's normal to receive timeout errors in this stage, we can safely ignore them.
pass
if self.request:
url = ure.search('(?:GET|POST) /(.*?)(?:\\?.*?)? HTTP', self.request).group(1).decode('utf-8').rstrip('/')
if url == '':
self.__HandleRoot()
elif url == 'configure':
self.__HandleConfigure()
else:
self.__HandleNotFound()
except Exception:
print('Something went wrong! Reboot and try again.')
return
finally:
self.client.close()
def __SendHeader(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 __SendResponse(self, payload, status_code = 200):
self.__SendHeader(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 __HandleRoot(self):
self.__SendHeader()
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>{0}</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 __HandleConfigure(self):
match = ure.search('ssid=([^&]*)&password=(.*)', self.request)
if match:
ssid = match.group(1).decode('utf-8').replace('%3F', '?').replace('%21', '!').replace('%23', '#')
password = match.group(2).decode('utf-8').replace('%3F', '?').replace('%21', '!')
if len(ssid) == 0:
self.__SendResponse("""<p>SSID must be providaded!</p><p>Go back and try again!</p>""", 400)
elif self.__WifiConnect(ssid, password):
self.__SendResponse("""<p>Successfully connected to</p><h1>{0}</h1><p>IP address: {1}</p>""".format(ssid, self.wlan_sta.ifconfig()[0]))
profiles = self.__ReadProfiles()
profiles[ssid] = password
self.__WriteProfiles(profiles)
utime.sleep(5)
else:
self.__SendResponse("""<p>Could not connect to</p><h1>{0}</h1><p>Go back and try again!</p>""".format(ssid))
utime.sleep(5)
else:
self.__SendResponse("""<p>Parameters not found!</p>""", 400)
utime.sleep(5)
def __HandleNotFound(self):
self.__SendResponse("""<p>Path not found!</p>""", 404)
utime.sleep(5)