kopia lustrzana https://github.com/tayfunulu/WiFiManager
Merge cdfb0e815c
into 8e28327daf
commit
203df44650
225
wifimgr.py
225
wifimgr.py
|
@ -9,6 +9,109 @@ ap_authmode = 3 # WPA2
|
||||||
|
|
||||||
NETWORK_PROFILES = 'wifi.dat'
|
NETWORK_PROFILES = 'wifi.dat'
|
||||||
|
|
||||||
|
html_head = """
|
||||||
|
<head>
|
||||||
|
<title>WiFi Manager</title>
|
||||||
|
<style>
|
||||||
|
body {
|
||||||
|
margin: 0px 20px;
|
||||||
|
font-family: "Roboto", "Helvetica", "Arial", sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
ul {
|
||||||
|
list-style-type: none;
|
||||||
|
padding: 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
li {
|
||||||
|
margin: 0px -10px;
|
||||||
|
padding: 10px 10px 10px 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
a, h1, h2, h3 {
|
||||||
|
color: #333;
|
||||||
|
text-decoration: none;
|
||||||
|
}
|
||||||
|
|
||||||
|
p > a {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
|
a > li,
|
||||||
|
li[onclick] {
|
||||||
|
background-color: #ffffff00;
|
||||||
|
transition: background-color .5s;
|
||||||
|
}
|
||||||
|
|
||||||
|
a:hover li, a:active li,
|
||||||
|
li:hover[onclick], li:active[onclick] {
|
||||||
|
background-color: #ddd;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1, form {
|
||||||
|
position: relative;
|
||||||
|
max-width: 750px;
|
||||||
|
width: 100%;
|
||||||
|
margin: auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
h1 {
|
||||||
|
margin: 10px auto;
|
||||||
|
}
|
||||||
|
|
||||||
|
h2 {
|
||||||
|
margin: 10px 0px -10px 0px;
|
||||||
|
}
|
||||||
|
|
||||||
|
select:-moz-focusring {
|
||||||
|
color: transparent;
|
||||||
|
text-shadow: 0 0 0 #000;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
position: relative;
|
||||||
|
max-width: 750px;
|
||||||
|
width: 100%;
|
||||||
|
margin: auto;
|
||||||
|
padding: 10px 10px 10px 10px;
|
||||||
|
display: block;
|
||||||
|
text-align: left;
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
font-size: 1em;
|
||||||
|
background-color: #ffffff00;
|
||||||
|
transition: background-color .5s;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover, button:active {
|
||||||
|
background-color: #ddd;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
#submit {
|
||||||
|
border: none;
|
||||||
|
background: transparent;
|
||||||
|
padding: 0px;
|
||||||
|
font-size: 1em;
|
||||||
|
}
|
||||||
|
|
||||||
|
.monospace {
|
||||||
|
font-family: 'Roboto Mono', monospace;
|
||||||
|
}
|
||||||
|
|
||||||
|
.fixedWidth {
|
||||||
|
width: 100px;
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
|
||||||
|
.placeholder {
|
||||||
|
padding-bottom: 20px;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
"""
|
||||||
|
|
||||||
wlan_ap = network.WLAN(network.AP_IF)
|
wlan_ap = network.WLAN(network.AP_IF)
|
||||||
wlan_sta = network.WLAN(network.STA_IF)
|
wlan_sta = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
|
@ -120,62 +223,58 @@ def handle_root(client):
|
||||||
wlan_sta.active(True)
|
wlan_sta.active(True)
|
||||||
ssids = sorted(ssid.decode('utf-8') for ssid, *_ in wlan_sta.scan())
|
ssids = sorted(ssid.decode('utf-8') for ssid, *_ in wlan_sta.scan())
|
||||||
send_header(client)
|
send_header(client)
|
||||||
|
client.sendall("<html>")
|
||||||
|
client.sendall(html_head)
|
||||||
client.sendall("""\
|
client.sendall("""\
|
||||||
<html>
|
<body>
|
||||||
<h1 style="color: #5e9ca0; text-align: center;">
|
|
||||||
<span style="color: #ff0000;">
|
|
||||||
Wi-Fi Client Setup
|
|
||||||
</span>
|
|
||||||
</h1>
|
|
||||||
<form action="configure" method="post">
|
<form action="configure" method="post">
|
||||||
<table style="margin-left: auto; margin-right: auto;">
|
<h1>
|
||||||
<tbody>
|
Wi-Fi Client Setup
|
||||||
|
</h1>
|
||||||
|
<h2>
|
||||||
|
SSIDs (click to select)
|
||||||
|
</h2>
|
||||||
|
<ul>
|
||||||
""")
|
""")
|
||||||
while len(ssids):
|
while len(ssids):
|
||||||
ssid = ssids.pop(0)
|
ssid = ssids.pop(0)
|
||||||
client.sendall("""\
|
client.sendall("""\
|
||||||
<tr>
|
<a onclick="document.getElementById('ssid').value = '{ssid}';" href="javascript:void(0)">
|
||||||
<td colspan="2">
|
|
||||||
<input type="radio" name="ssid" value="{0}" />{0}
|
|
||||||
</td>
|
|
||||||
</tr>
|
|
||||||
""".format(ssid))
|
|
||||||
client.sendall("""\
|
|
||||||
<tr>
|
|
||||||
<td>Password:</td>
|
|
||||||
<td><input name="password" type="password" /></td>
|
|
||||||
</tr>
|
|
||||||
</tbody>
|
|
||||||
</table>
|
|
||||||
<p style="text-align: center;">
|
|
||||||
<input type="submit" value="Submit" />
|
|
||||||
</p>
|
|
||||||
</form>
|
|
||||||
<p> </p>
|
|
||||||
<hr />
|
|
||||||
<h5>
|
|
||||||
<span style="color: #ff0000;">
|
|
||||||
Your ssid and password information will be saved into the
|
|
||||||
"%(filename)s" file in your ESP module for future usage.
|
|
||||||
Be careful about security!
|
|
||||||
</span>
|
|
||||||
</h5>
|
|
||||||
<hr />
|
|
||||||
<h2 style="color: #2e6c80;">
|
|
||||||
Some useful infos:
|
|
||||||
</h2>
|
|
||||||
<ul>
|
|
||||||
<li>
|
<li>
|
||||||
Original code from <a href="https://github.com/cpopp/MicroPythonSamples"
|
{ssid}
|
||||||
target="_blank" rel="noopener">cpopp/MicroPythonSamples</a>.
|
|
||||||
</li>
|
</li>
|
||||||
<li>
|
</a>
|
||||||
This code available at <a href="https://github.com/tayfunulu/WiFiManager"
|
""".format(ssid=ssid))
|
||||||
target="_blank" rel="noopener">tayfunulu/WiFiManager</a>.
|
client.sendall("""\
|
||||||
|
<li onclick="document.getElementById('ssid').focus()">
|
||||||
|
<span class="fixedWidth">SSID</span>
|
||||||
|
<input id="ssid" name="ssid" type="text"/>
|
||||||
|
</li>
|
||||||
|
<li onclick="document.getElementById('password').focus()">
|
||||||
|
<span class="fixedWidth">Password</span>
|
||||||
|
<input id="password" name="password" type="password"/>
|
||||||
|
</li>
|
||||||
|
<li onclick="document.getElementById('submit').click()">
|
||||||
|
<input id=submit type="submit" value="Submit" />
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
<div class="placeholder"></div>
|
||||||
|
<h2>
|
||||||
|
Infos:
|
||||||
|
</h2>
|
||||||
|
<p>
|
||||||
|
Your ssid and password information will be saved into the "{filename}" file in your ESP module for future usage. Be careful about security!
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
Original code from <a href="https://github.com/cpopp/MicroPythonSamples" target="_blank" rel="noopener">cpopp/MicroPythonSamples</a>.
|
||||||
|
</p>
|
||||||
|
<p>
|
||||||
|
This code available at <a href="https://github.com/tayfunulu/WiFiManager" target="_blank" rel="noopener">tayfunulu/WiFiManager</a>.
|
||||||
|
</p>
|
||||||
|
</form>
|
||||||
|
</body>
|
||||||
</html>
|
</html>
|
||||||
""" % dict(filename=NETWORK_PROFILES))
|
""".format(filename=NETWORK_PROFILES))
|
||||||
client.close()
|
client.close()
|
||||||
|
|
||||||
|
|
||||||
|
@ -200,17 +299,14 @@ def handle_configure(client, request):
|
||||||
if do_connect(ssid, password):
|
if do_connect(ssid, password):
|
||||||
response = """\
|
response = """\
|
||||||
<html>
|
<html>
|
||||||
<center>
|
{html_head}
|
||||||
<br><br>
|
<body>
|
||||||
<h1 style="color: #5e9ca0; text-align: center;">
|
<h1>
|
||||||
<span style="color: #ff0000;">
|
ESP successfully connected to WiFi network "<span class="monospace">{ssid}</span>"
|
||||||
ESP successfully connected to WiFi network %(ssid)s.
|
|
||||||
</span>
|
|
||||||
</h1>
|
</h1>
|
||||||
<br><br>
|
</body>
|
||||||
</center>
|
|
||||||
</html>
|
</html>
|
||||||
""" % dict(ssid=ssid)
|
""".format(html_head=html_head, ssid=ssid)
|
||||||
send_response(client, response)
|
send_response(client, response)
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
wlan_ap.active(False)
|
wlan_ap.active(False)
|
||||||
|
@ -227,19 +323,15 @@ def handle_configure(client, request):
|
||||||
else:
|
else:
|
||||||
response = """\
|
response = """\
|
||||||
<html>
|
<html>
|
||||||
<center>
|
{html_head}
|
||||||
<h1 style="color: #5e9ca0; text-align: center;">
|
<body>
|
||||||
<span style="color: #ff0000;">
|
<h1>
|
||||||
ESP could not connect to WiFi network %(ssid)s.
|
ESP could not connect to WiFi network "<span class="monospace">{ssid}</span>"
|
||||||
</span>
|
|
||||||
</h1>
|
</h1>
|
||||||
<br><br>
|
<button onclick="history.back()">Go back</button>
|
||||||
<form>
|
</body>
|
||||||
<input type="button" value="Go back!" onclick="history.back()"></input>
|
|
||||||
</form>
|
|
||||||
</center>
|
|
||||||
</html>
|
</html>
|
||||||
""" % dict(ssid=ssid)
|
""".format(html_head=html_head, ssid=ssid)
|
||||||
send_response(client, response)
|
send_response(client, response)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
@ -284,8 +376,7 @@ def start(port=80):
|
||||||
client, addr = server_socket.accept()
|
client, addr = server_socket.accept()
|
||||||
print('client connected from', addr)
|
print('client connected from', addr)
|
||||||
try:
|
try:
|
||||||
client.settimeout(5.0)
|
client.settimeout(15.0)
|
||||||
|
|
||||||
request = b""
|
request = b""
|
||||||
try:
|
try:
|
||||||
while "\r\n\r\n" not in request:
|
while "\r\n\r\n" not in request:
|
||||||
|
|
Ładowanie…
Reference in New Issue