kopia lustrzana https://github.com/tayfunulu/WiFiManager
refactor main code, separate profile code
also: - go through available wifi networks in sorted order, by rssi - differentiate open from encrypted networks - some debug printspull/3/head
rodzic
f991513ea6
commit
8674d7c4da
51
main.py
51
main.py
|
@ -10,37 +10,44 @@ def check_connection():
|
||||||
# First check if there already is any connection:
|
# First check if there already is any connection:
|
||||||
if wlan_sta.isconnected():
|
if wlan_sta.isconnected():
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
connected = False
|
||||||
try:
|
try:
|
||||||
# ESP connecting to WiFi takes time, wait a bit and try again:
|
# ESP connecting to WiFi takes time, wait a bit and try again:
|
||||||
time.sleep(3)
|
time.sleep(3)
|
||||||
if not wlan_sta.isconnected():
|
if wlan_sta.isconnected():
|
||||||
# inside passwd file, there is a list of WiFi networks (CSV format)
|
return True
|
||||||
with open("passwd.dat") as f:
|
|
||||||
lines = f.readlines()
|
# Read known network profiles from file
|
||||||
|
profiles = networkconfig.read_profiles()
|
||||||
|
|
||||||
# Search WiFis in range
|
# Search WiFis in range
|
||||||
ssids_found = wlan_sta.scan()
|
networks = wlan_sta.scan()
|
||||||
|
|
||||||
# matching...
|
AUTHMODE = {0: "open", 1: "WEP", 2: "WPA-PSK", 3: "WPA2-PSK", 4: "WPA/WPA2-PSK"}
|
||||||
for line in lines:
|
for ssid, bssid, channel, rssi, authmode, hidden in sorted(networks, key=lambda x: x[3], reverse=True):
|
||||||
ssid, password = line.strip("\n").split(";")
|
ssid = ssid.decode('utf-8')
|
||||||
for ssid_found in ssids_found:
|
encrypted = authmode > 0
|
||||||
if ssid in ssid_found[0]:
|
print("ssid: %s chan: %d rssi: %d authmode: %s" % (ssid, channel, rssi, AUTHMODE.get(authmode, '?')))
|
||||||
print("OK. WiFi found.")
|
if encrypted:
|
||||||
if networkconfig.do_connect(ssid, password):
|
if ssid in profiles:
|
||||||
return True
|
password = profiles[ssid]
|
||||||
|
connected = networkconfig.do_connect(ssid, password)
|
||||||
if not wlan_sta.isconnected():
|
|
||||||
if networkconfig.start():
|
|
||||||
return True
|
|
||||||
else:
|
else:
|
||||||
return True
|
print("skipping unknown encrypted network")
|
||||||
|
else: # open
|
||||||
|
connected = networkconfig.do_connect(ssid, None)
|
||||||
|
if connected:
|
||||||
|
break
|
||||||
|
|
||||||
except OSError:
|
except OSError:
|
||||||
# start web server for connection manager:
|
pass
|
||||||
if networkconfig.start():
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
# start web server for connection manager:
|
||||||
|
if not connected:
|
||||||
|
connected = networkconfig.start()
|
||||||
|
|
||||||
|
return connected
|
||||||
|
|
||||||
|
|
||||||
if check_connection():
|
if check_connection():
|
||||||
|
|
|
@ -12,6 +12,27 @@ ssid_password = "tayfunulu"
|
||||||
|
|
||||||
server_socket = None
|
server_socket = None
|
||||||
|
|
||||||
|
# list of WiFi networks (CSV format: ssid,password)
|
||||||
|
NETWORK_PROFILES = 'passwd.dat'
|
||||||
|
|
||||||
|
|
||||||
|
def read_profiles():
|
||||||
|
with open(NETWORK_PROFILES) as f:
|
||||||
|
lines = f.readlines()
|
||||||
|
profiles = {}
|
||||||
|
for line in lines:
|
||||||
|
ssid, password = line.strip("\n").split(";")
|
||||||
|
profiles[ssid] = password
|
||||||
|
return profiles
|
||||||
|
|
||||||
|
|
||||||
|
def write_profiles(profiles):
|
||||||
|
lines = []
|
||||||
|
for ssid, password in profiles.items():
|
||||||
|
lines.append("%s;%s\n" % (ssid, password))
|
||||||
|
with open(NETWORK_PROFILES, "w") as f:
|
||||||
|
f.write(''.join(lines))
|
||||||
|
|
||||||
|
|
||||||
def do_connect(ssid, password):
|
def do_connect(ssid, password):
|
||||||
sta_if = network.WLAN(network.STA_IF)
|
sta_if = network.WLAN(network.STA_IF)
|
||||||
|
@ -86,7 +107,7 @@ def handle_root(client):
|
||||||
<h5>
|
<h5>
|
||||||
<span style="color: #ff0000;">
|
<span style="color: #ff0000;">
|
||||||
Your ssid and password information will be saved into the
|
Your ssid and password information will be saved into the
|
||||||
"passwd.dat" file in your ESP module for future usage.
|
"%(filename)s" file in your ESP module for future usage.
|
||||||
Be careful about security!
|
Be careful about security!
|
||||||
</span>
|
</span>
|
||||||
</h5>
|
</h5>
|
||||||
|
@ -105,7 +126,7 @@ def handle_root(client):
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
</html>
|
</html>
|
||||||
""")
|
""" % dict(filename=NETWORK_PROFILES))
|
||||||
send_response(client, "\n".join(response))
|
send_response(client, "\n".join(response))
|
||||||
|
|
||||||
|
|
||||||
|
@ -143,13 +164,11 @@ def handle_configure(client, request):
|
||||||
""" % dict(ssid=ssid)
|
""" % dict(ssid=ssid)
|
||||||
send_response(client, response)
|
send_response(client, response)
|
||||||
try:
|
try:
|
||||||
with open("passwd.dat", "r") as f:
|
profiles = read_profiles()
|
||||||
ex_data = f.read()
|
except OSError:
|
||||||
except:
|
profiles = {}
|
||||||
ex_data = ""
|
profiles[ssid] = password
|
||||||
ex_data = "%s;%s\n" % (ssid, password) + ex_data
|
write_profiles(profiles)
|
||||||
with open("passwd.dat", "w") as f:
|
|
||||||
f.write(ex_data)
|
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
response = """\
|
response = """\
|
||||||
|
|
Ładowanie…
Reference in New Issue