2017-10-22 19:49:26 +00:00
|
|
|
import network
|
|
|
|
import socket
|
|
|
|
import ure
|
|
|
|
import time
|
|
|
|
|
|
|
|
wlan_ap = network.WLAN(network.AP_IF)
|
|
|
|
wlan_sta = network.WLAN(network.STA_IF)
|
|
|
|
|
2017-12-11 14:06:47 +00:00
|
|
|
# SSID/Password for setup
|
2017-12-11 13:38:41 +00:00
|
|
|
ssid_name = "WifiManager"
|
2017-11-19 10:02:15 +00:00
|
|
|
ssid_password = "tayfunulu"
|
|
|
|
|
2017-10-22 19:49:26 +00:00
|
|
|
server_socket = None
|
|
|
|
|
2017-12-11 13:38:41 +00:00
|
|
|
|
2017-10-22 19:49:26 +00:00
|
|
|
def do_connect(ntwrk_ssid, netwrk_pass):
|
2017-12-11 13:38:41 +00:00
|
|
|
sta_if = network.WLAN(network.STA_IF)
|
|
|
|
sta_if.active(True)
|
|
|
|
if not sta_if.isconnected():
|
2017-12-11 14:06:47 +00:00
|
|
|
print('Trying to connect to %s...' % ntwrk_ssid)
|
2017-12-11 13:38:41 +00:00
|
|
|
sta_if.active(True)
|
|
|
|
sta_if.connect(ntwrk_ssid, netwrk_pass)
|
|
|
|
a = 0
|
|
|
|
while not sta_if.isconnected() | (a > 99):
|
|
|
|
time.sleep(0.1)
|
|
|
|
a += 1
|
|
|
|
print('.', end='')
|
|
|
|
if sta_if.isconnected():
|
2017-12-11 14:06:47 +00:00
|
|
|
print('\nConnected. Network config: ', sta_if.ifconfig())
|
2017-12-11 13:38:41 +00:00
|
|
|
return (True)
|
|
|
|
else:
|
2017-12-11 14:06:47 +00:00
|
|
|
print('\nFailed. Not Connected to: ' + ntwrk_ssid)
|
2017-12-11 13:38:41 +00:00
|
|
|
return (False)
|
|
|
|
|
2017-10-22 19:49:26 +00:00
|
|
|
|
|
|
|
def send_response(client, payload, status_code=200):
|
2017-12-11 13:38:41 +00:00
|
|
|
client.sendall("HTTP/1.0 {} OK\r\n".format(status_code))
|
|
|
|
client.sendall("Content-Type: text/html\r\n")
|
|
|
|
client.sendall("Content-Length: {}\r\n".format(len(payload)))
|
|
|
|
client.sendall("\r\n")
|
|
|
|
|
|
|
|
if len(payload) > 0:
|
|
|
|
client.sendall(payload)
|
|
|
|
|
2017-10-22 19:49:26 +00:00
|
|
|
|
|
|
|
def handle_root(client):
|
2017-12-11 13:38:41 +00:00
|
|
|
global wlan_sta
|
|
|
|
response_header = """
|
2017-10-22 19:49:26 +00:00
|
|
|
<html><h1 style="color: #5e9ca0; text-align: center;"><span style="color: #ff0000;">Wi-Fi Client Setup</span></h1>
|
|
|
|
<form action="configure" method="post">
|
|
|
|
<table style="margin-left: auto; margin-right: auto;">
|
|
|
|
<tbody><tr><td>Wifi Name</td>
|
|
|
|
<td style="text-align: center;"><select id="ssid" name="ssid">
|
|
|
|
"""
|
2017-12-11 13:38:41 +00:00
|
|
|
wlan_sta.active(True)
|
|
|
|
|
|
|
|
response_variable = ""
|
|
|
|
for ssid, *_ in wlan_sta.scan():
|
|
|
|
response_variable += '<option value="{0}">{0}</option>'.format(ssid.decode("utf-8"))
|
2017-10-22 19:49:26 +00:00
|
|
|
|
2017-12-11 13:38:41 +00:00
|
|
|
response_footer = """
|
2017-10-22 19:49:26 +00:00
|
|
|
</select></td></tr>
|
|
|
|
<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 save "passwd.dat" file inside of your esp module to use next time... be careful for security !!!</span></h5>
|
|
|
|
<hr />
|
|
|
|
<h2 style="color: #2e6c80;">Some useful infos:</h2>
|
|
|
|
<ul>
|
|
|
|
<li>Wi-Fi Client for Micropython GitHub from <a href="https://github.com/cpopp/MicroPythonSamples" target="_blank" rel="noopener">cpopp</a></li>
|
|
|
|
<li>My github adress <a href="https://github.com/tayfunulu" target="_blank" rel="noopener">tayfunulu</a></li>
|
|
|
|
</ul>
|
|
|
|
</html>
|
|
|
|
"""
|
2017-12-11 13:38:41 +00:00
|
|
|
send_response(client, response_header + response_variable + response_footer)
|
|
|
|
|
2017-10-22 19:49:26 +00:00
|
|
|
|
|
|
|
def handle_configure(client, request):
|
2017-12-11 13:38:41 +00:00
|
|
|
match = ure.search("ssid=([^&]*)&password=(.*)", request)
|
|
|
|
|
|
|
|
if match is None:
|
|
|
|
send_response(client, "Parameters not found", status_code=400)
|
|
|
|
return (False)
|
|
|
|
# version 1.9 compatibility
|
|
|
|
try:
|
|
|
|
ssid = match.group(1).decode("utf-8").replace("%3F", "?").replace("%21", "!")
|
|
|
|
password = match.group(2).decode("utf-8").replace("%3F", "?").replace("%21", "!")
|
|
|
|
except:
|
|
|
|
ssid = match.group(1).replace("%3F", "?").replace("%21", "!")
|
|
|
|
password = match.group(2).replace("%3F", "?").replace("%21", "!")
|
|
|
|
|
|
|
|
if len(ssid) == 0:
|
|
|
|
send_response(client, "SSID must be provided", status_code=400)
|
|
|
|
return (False)
|
|
|
|
|
|
|
|
if do_connect(ssid, password):
|
|
|
|
response_footer = """
|
2017-10-22 19:49:26 +00:00
|
|
|
<html>
|
|
|
|
<center><br><br>
|
2017-12-11 13:38:41 +00:00
|
|
|
<h1 style="color: #5e9ca0; text-align: center;"><span style="color: #ff0000;">:) YES, Wi-Fi Configured to """ + ssid + """</span></h1>
|
2017-10-22 19:49:26 +00:00
|
|
|
<br><br>"""
|
2017-12-11 13:38:41 +00:00
|
|
|
send_response(client, response_footer)
|
|
|
|
try:
|
|
|
|
fo = open("passwd.dat", "r")
|
|
|
|
ex_data = fo.read()
|
|
|
|
fo.close()
|
|
|
|
fo = open("passwd.dat", "w")
|
|
|
|
ex_data = ssid + ";" + password + "\n" + ex_data
|
|
|
|
fo.write(ex_data)
|
|
|
|
fo.close()
|
|
|
|
except:
|
|
|
|
fo = open("passwd.dat", "w")
|
|
|
|
fo.write(ssid + ";" + password + "\n")
|
|
|
|
fo.close()
|
|
|
|
return (True)
|
|
|
|
else:
|
|
|
|
response_footer = """
|
2017-10-22 19:49:26 +00:00
|
|
|
<html>
|
|
|
|
<center>
|
2017-12-11 13:38:41 +00:00
|
|
|
<h1 style="color: #5e9ca0; text-align: center;"><span style="color: #ff0000;">Wi-Fi Not Configured to """ + ssid + """</span></h1>
|
2017-10-22 19:49:26 +00:00
|
|
|
<br><br>
|
|
|
|
<form>
|
|
|
|
<input type="button" value="Go back!" onclick="history.back()"></input>
|
|
|
|
</form></center></html>
|
|
|
|
"""
|
2017-12-11 13:38:41 +00:00
|
|
|
send_response(client, response_footer)
|
|
|
|
return (False)
|
|
|
|
|
2017-10-22 19:49:26 +00:00
|
|
|
|
|
|
|
def handle_not_found(client, url):
|
2017-12-11 13:38:41 +00:00
|
|
|
send_response(client, "Path not found: {}".format(url), status_code=404)
|
|
|
|
|
2017-10-22 19:49:26 +00:00
|
|
|
|
|
|
|
def stop():
|
2017-12-11 13:38:41 +00:00
|
|
|
global server_socket
|
|
|
|
|
|
|
|
if server_socket:
|
|
|
|
server_socket.close()
|
2017-10-22 19:49:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def start(port=80):
|
2017-12-11 13:38:41 +00:00
|
|
|
addr = socket.getaddrinfo('0.0.0.0', port)[0][-1]
|
|
|
|
|
|
|
|
global server_socket
|
|
|
|
global wlan_sta
|
|
|
|
global wlan_ap
|
|
|
|
global ssid_name
|
|
|
|
global ssid_password
|
|
|
|
stop()
|
|
|
|
|
|
|
|
wlan_sta.active(True)
|
|
|
|
wlan_ap.active(True)
|
|
|
|
|
|
|
|
wlan_ap.config(essid=ssid_name, password=ssid_password)
|
|
|
|
|
|
|
|
server_socket = socket.socket()
|
|
|
|
server_socket.bind(addr)
|
|
|
|
server_socket.listen(1)
|
|
|
|
|
2017-12-11 14:06:47 +00:00
|
|
|
print('Connect to WiFi ssid ' + ssid_name + ', default password: ' + ssid_password)
|
|
|
|
print('and access the ESP via your favorite web browser at 192.168.4.1.')
|
|
|
|
print('Listening on:', addr)
|
2017-12-11 13:38:41 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
|
|
|
|
if wlan_sta.isconnected():
|
|
|
|
client.close
|
|
|
|
return (True)
|
|
|
|
|
|
|
|
client, addr = server_socket.accept()
|
|
|
|
client.settimeout(5.0)
|
|
|
|
|
|
|
|
print('client connected from', addr)
|
|
|
|
|
|
|
|
request = b""
|
|
|
|
try:
|
|
|
|
while not "\r\n\r\n" in request:
|
|
|
|
request += client.recv(512)
|
|
|
|
except OSError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
print("Request is: {}".format(request))
|
|
|
|
if "HTTP" not in request:
|
|
|
|
# skip invalid requests
|
|
|
|
client.close()
|
|
|
|
continue
|
|
|
|
|
|
|
|
# version 1.9 compatibility
|
|
|
|
try:
|
|
|
|
url = ure.search("(?:GET|POST) /(.*?)(?:\\?.*?)? HTTP", request).group(1).decode("utf-8").rstrip("/")
|
|
|
|
except:
|
|
|
|
url = ure.search("(?:GET|POST) /(.*?)(?:\\?.*?)? HTTP", request).group(1).rstrip("/")
|
|
|
|
print("URL is {}".format(url))
|
|
|
|
|
|
|
|
if url == "":
|
|
|
|
handle_root(client)
|
|
|
|
elif url == "configure":
|
|
|
|
handle_configure(client, request)
|
|
|
|
else:
|
|
|
|
handle_not_found(client, url)
|
|
|
|
|
|
|
|
client.close()
|