kopia lustrzana https://github.com/tayfunulu/WiFiManager
applied pycharm "reformat file"
fixes: - indentation: tabs -> 4 spaces - spaces around operators - other pep8 issuespull/3/head
rodzic
582cc2acbc
commit
f11fcbf606
39
main.py
39
main.py
|
@ -1,27 +1,30 @@
|
|||
import network
|
||||
import networkconfig
|
||||
import time
|
||||
|
||||
wlan_sta = network.WLAN(network.STA_IF)
|
||||
|
||||
|
||||
def do_connect(ntwrk_ssid, netwrk_pass):
|
||||
sta_if = network.WLAN(network.STA_IF)
|
||||
sta_if.active(True)
|
||||
if not sta_if.isconnected():
|
||||
print('try to connect : '+ntwrk_ssid+' network...')
|
||||
print('try to connect : ' + ntwrk_ssid + ' network...')
|
||||
sta_if.connect(ntwrk_ssid, netwrk_pass)
|
||||
a=0
|
||||
while not sta_if.isconnected() | (a > 99) :
|
||||
a = 0
|
||||
while not sta_if.isconnected() | (a > 99):
|
||||
time.sleep(0.1)
|
||||
a+=1
|
||||
a += 1
|
||||
print('.', end='')
|
||||
if sta_if.isconnected():
|
||||
print('\nConnected. Network config:', sta_if.ifconfig())
|
||||
return (True)
|
||||
else :
|
||||
print('\nProblem. Not Connected to :'+ntwrk_ssid)
|
||||
else:
|
||||
print('\nProblem. Not Connected to :' + ntwrk_ssid)
|
||||
return (False)
|
||||
|
||||
def check_connection ():
|
||||
|
||||
def check_connection():
|
||||
global wlan_sta
|
||||
# Firstly check is there any connection
|
||||
if wlan_sta.isconnected():
|
||||
|
@ -40,12 +43,12 @@ def check_connection ():
|
|||
|
||||
# matching...
|
||||
for ipass in data:
|
||||
ssid_list= ipass.strip("\n").split(";")
|
||||
ssid_list = ipass.strip("\n").split(";")
|
||||
|
||||
for i in ssids_found:
|
||||
if ssid_list[0] in i[0]:
|
||||
print ("OK. WiFi is Founded")
|
||||
if do_connect(ssid_list[0],ssid_list[1]):
|
||||
print("OK. WiFi is Founded")
|
||||
if do_connect(ssid_list[0], ssid_list[1]):
|
||||
return (True)
|
||||
|
||||
if not wlan_sta.isconnected():
|
||||
|
@ -61,17 +64,13 @@ def check_connection ():
|
|||
|
||||
return (False)
|
||||
|
||||
if check_connection ():
|
||||
|
||||
if check_connection():
|
||||
|
||||
# Main Code is here
|
||||
print ("ESP OK")
|
||||
# to import your code;
|
||||
# import sample_mqtt.py
|
||||
print("ESP OK")
|
||||
# to import your code;
|
||||
# import sample_mqtt.py
|
||||
|
||||
else:
|
||||
print ("There is something wrong")
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
print("There is something wrong")
|
||||
|
|
|
@ -7,30 +7,32 @@ wlan_ap = network.WLAN(network.AP_IF)
|
|||
wlan_sta = network.WLAN(network.STA_IF)
|
||||
|
||||
# setup for ssid
|
||||
ssid_name= "WifiManager"
|
||||
ssid_name = "WifiManager"
|
||||
ssid_password = "tayfunulu"
|
||||
|
||||
server_socket = None
|
||||
|
||||
|
||||
def do_connect(ntwrk_ssid, netwrk_pass):
|
||||
sta_if = network.WLAN(network.STA_IF)
|
||||
sta_if.active(True)
|
||||
if not sta_if.isconnected():
|
||||
print('try to connect : '+ntwrk_ssid+' network...')
|
||||
print('try to connect : ' + ntwrk_ssid + ' network...')
|
||||
sta_if.active(True)
|
||||
sta_if.connect(ntwrk_ssid, netwrk_pass)
|
||||
a=0
|
||||
while not sta_if.isconnected() | (a > 99) :
|
||||
a = 0
|
||||
while not sta_if.isconnected() | (a > 99):
|
||||
time.sleep(0.1)
|
||||
a+=1
|
||||
a += 1
|
||||
print('.', end='')
|
||||
if sta_if.isconnected():
|
||||
print('\nConnected. Network config:', sta_if.ifconfig())
|
||||
return (True)
|
||||
else :
|
||||
print('\nProblem. Not Connected to :'+ntwrk_ssid)
|
||||
else:
|
||||
print('\nProblem. Not Connected to :' + ntwrk_ssid)
|
||||
return (False)
|
||||
|
||||
|
||||
def send_response(client, payload, status_code=200):
|
||||
client.sendall("HTTP/1.0 {} OK\r\n".format(status_code))
|
||||
client.sendall("Content-Type: text/html\r\n")
|
||||
|
@ -40,6 +42,7 @@ def send_response(client, payload, status_code=200):
|
|||
if len(payload) > 0:
|
||||
client.sendall(payload)
|
||||
|
||||
|
||||
def handle_root(client):
|
||||
global wlan_sta
|
||||
response_header = """
|
||||
|
@ -76,6 +79,7 @@ def handle_root(client):
|
|||
"""
|
||||
send_response(client, response_header + response_variable + response_footer)
|
||||
|
||||
|
||||
def handle_configure(client, request):
|
||||
match = ure.search("ssid=([^&]*)&password=(.*)", request)
|
||||
|
||||
|
@ -84,12 +88,11 @@ def handle_configure(client, request):
|
|||
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","!")
|
||||
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","!")
|
||||
|
||||
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)
|
||||
|
@ -99,20 +102,20 @@ def handle_configure(client, request):
|
|||
response_footer = """
|
||||
<html>
|
||||
<center><br><br>
|
||||
<h1 style="color: #5e9ca0; text-align: center;"><span style="color: #ff0000;">:) YES, Wi-Fi Configured to """+ssid+"""</span></h1>
|
||||
<h1 style="color: #5e9ca0; text-align: center;"><span style="color: #ff0000;">:) YES, Wi-Fi Configured to """ + ssid + """</span></h1>
|
||||
<br><br>"""
|
||||
send_response(client, response_footer)
|
||||
try:
|
||||
fo = open("passwd.dat","r")
|
||||
fo = open("passwd.dat", "r")
|
||||
ex_data = fo.read()
|
||||
fo.close()
|
||||
fo = open("passwd.dat","w")
|
||||
ex_data = ssid+";"+password+"\n"+ex_data
|
||||
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 = open("passwd.dat", "w")
|
||||
fo.write(ssid + ";" + password + "\n")
|
||||
fo.close()
|
||||
|
||||
return (True)
|
||||
|
@ -120,24 +123,27 @@ def handle_configure(client, request):
|
|||
response_footer = """
|
||||
<html>
|
||||
<center>
|
||||
<h1 style="color: #5e9ca0; text-align: center;"><span style="color: #ff0000;">Wi-Fi Not Configured to """+ssid+"""</span></h1>
|
||||
<h1 style="color: #5e9ca0; text-align: center;"><span style="color: #ff0000;">Wi-Fi Not Configured to """ + ssid + """</span></h1>
|
||||
<br><br>
|
||||
<form>
|
||||
<input type="button" value="Go back!" onclick="history.back()"></input>
|
||||
</form></center></html>
|
||||
"""
|
||||
send_response(client, response_footer )
|
||||
send_response(client, response_footer)
|
||||
return (False)
|
||||
|
||||
|
||||
def handle_not_found(client, url):
|
||||
send_response(client, "Path not found: {}".format(url), status_code=404)
|
||||
|
||||
|
||||
def stop():
|
||||
global server_socket
|
||||
|
||||
if server_socket:
|
||||
server_socket.close()
|
||||
|
||||
|
||||
def start(port=80):
|
||||
addr = socket.getaddrinfo('0.0.0.0', port)[0][-1]
|
||||
|
||||
|
@ -151,14 +157,13 @@ def start(port=80):
|
|||
wlan_sta.active(True)
|
||||
wlan_ap.active(True)
|
||||
|
||||
wlan_ap.config(essid=ssid_name,password=ssid_password)
|
||||
|
||||
wlan_ap.config(essid=ssid_name, password=ssid_password)
|
||||
|
||||
server_socket = socket.socket()
|
||||
server_socket.bind(addr)
|
||||
server_socket.listen(1)
|
||||
|
||||
print('Connect to Wifi ssid :'+ssid_name+' , Default pass: '+ssid_password)
|
||||
print('Connect to Wifi ssid :' + ssid_name + ' , Default pass: ' + ssid_password)
|
||||
print('And connect to esp via your favorite web browser (like 192.168.4.1)')
|
||||
print('listening on', addr)
|
||||
|
||||
|
|
Ładowanie…
Reference in New Issue