Improve webinterface

pull/16/head
TilCreator 2018-05-29 22:19:01 +02:00
rodzic 9d3094862a
commit 0370235697
1 zmienionych plików z 161 dodań i 69 usunięć

Wyświetl plik

@ -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;
}
h3 {
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)
@ -119,62 +222,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>
<h3>
SSIDs (click to select)
</h3>
<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 = '{0}';" href="javascript:void(0)">
<td colspan="2"> <li>
<input type="radio" name="ssid" value="{0}" />{0} {0}
</td> </li>
</tr> </a>
""".format(ssid)) """.format(ssid))
client.sendall("""\ client.sendall("""\
<tr> <li onclick="document.getElementById('ssid').focus()">
<td>Password:</td> <span class="fixedWidth">SSID</span>
<td><input name="password" type="password" /></td> <input id="ssid" name="ssid" type="text"/>
</tr>
</tbody>
</table>
<p style="text-align: center;">
<input type="submit" value="Submit" />
</p>
</form>
<p>&nbsp;</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>
Original code from <a href="https://github.com/cpopp/MicroPythonSamples"
target="_blank" rel="noopener">cpopp/MicroPythonSamples</a>.
</li> </li>
<li> <li onclick="document.getElementById('password').focus()">
This code available at <a href="https://github.com/tayfunulu/WiFiManager" <span class="fixedWidth">Password</span>
target="_blank" rel="noopener">tayfunulu/WiFiManager</a>. <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 "{}" 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(NETWORK_PROFILES))
client.close() client.close()
@ -196,17 +295,14 @@ def handle_configure(client, request):
if do_connect(ssid, password): if do_connect(ssid, password):
response = """\ response = """\
<html> <html>
<center> {}
<br><br> <body>
<h1 style="color: #5e9ca0; text-align: center;"> <h1>
<span style="color: #ff0000;"> ESP successfully connected to WiFi network "<span class="monospace">{}</span>"
ESP successfully connected to WiFi network %(ssid)s.
</span>
</h1> </h1>
<br><br> </body>
</center>
</html> </html>
""" % dict(ssid=ssid) """.format(html_head, ssid)
send_response(client, response) send_response(client, response)
try: try:
profiles = read_profiles() profiles = read_profiles()
@ -218,19 +314,15 @@ def handle_configure(client, request):
else: else:
response = """\ response = """\
<html> <html>
<center> {}
<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">{}</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, ssid)
send_response(client, response) send_response(client, response)
return False return False
@ -265,7 +357,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: