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:
|
||||
if wlan_sta.isconnected():
|
||||
return True
|
||||
|
||||
connected = False
|
||||
try:
|
||||
# ESP connecting to WiFi takes time, wait a bit and try again:
|
||||
time.sleep(3)
|
||||
if not wlan_sta.isconnected():
|
||||
# inside passwd file, there is a list of WiFi networks (CSV format)
|
||||
with open("passwd.dat") as f:
|
||||
lines = f.readlines()
|
||||
if wlan_sta.isconnected():
|
||||
return True
|
||||
|
||||
# Read known network profiles from file
|
||||
profiles = networkconfig.read_profiles()
|
||||
|
||||
# Search WiFis in range
|
||||
ssids_found = wlan_sta.scan()
|
||||
networks = wlan_sta.scan()
|
||||
|
||||
# matching...
|
||||
for line in lines:
|
||||
ssid, password = line.strip("\n").split(";")
|
||||
for ssid_found in ssids_found:
|
||||
if ssid in ssid_found[0]:
|
||||
print("OK. WiFi found.")
|
||||
if networkconfig.do_connect(ssid, password):
|
||||
return True
|
||||
|
||||
if not wlan_sta.isconnected():
|
||||
if networkconfig.start():
|
||||
return True
|
||||
AUTHMODE = {0: "open", 1: "WEP", 2: "WPA-PSK", 3: "WPA2-PSK", 4: "WPA/WPA2-PSK"}
|
||||
for ssid, bssid, channel, rssi, authmode, hidden in sorted(networks, key=lambda x: x[3], reverse=True):
|
||||
ssid = ssid.decode('utf-8')
|
||||
encrypted = authmode > 0
|
||||
print("ssid: %s chan: %d rssi: %d authmode: %s" % (ssid, channel, rssi, AUTHMODE.get(authmode, '?')))
|
||||
if encrypted:
|
||||
if ssid in profiles:
|
||||
password = profiles[ssid]
|
||||
connected = networkconfig.do_connect(ssid, password)
|
||||
else:
|
||||
return True
|
||||
print("skipping unknown encrypted network")
|
||||
else: # open
|
||||
connected = networkconfig.do_connect(ssid, None)
|
||||
if connected:
|
||||
break
|
||||
|
||||
except OSError:
|
||||
# start web server for connection manager:
|
||||
if networkconfig.start():
|
||||
return True
|
||||
pass
|
||||
|
||||
return False
|
||||
# start web server for connection manager:
|
||||
if not connected:
|
||||
connected = networkconfig.start()
|
||||
|
||||
return connected
|
||||
|
||||
|
||||
if check_connection():
|
||||
|
|
|
@ -12,6 +12,27 @@ ssid_password = "tayfunulu"
|
|||
|
||||
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):
|
||||
sta_if = network.WLAN(network.STA_IF)
|
||||
|
@ -86,7 +107,7 @@ def handle_root(client):
|
|||
<h5>
|
||||
<span style="color: #ff0000;">
|
||||
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!
|
||||
</span>
|
||||
</h5>
|
||||
|
@ -105,7 +126,7 @@ def handle_root(client):
|
|||
</li>
|
||||
</ul>
|
||||
</html>
|
||||
""")
|
||||
""" % dict(filename=NETWORK_PROFILES))
|
||||
send_response(client, "\n".join(response))
|
||||
|
||||
|
||||
|
@ -143,13 +164,11 @@ def handle_configure(client, request):
|
|||
""" % dict(ssid=ssid)
|
||||
send_response(client, response)
|
||||
try:
|
||||
with open("passwd.dat", "r") as f:
|
||||
ex_data = f.read()
|
||||
except:
|
||||
ex_data = ""
|
||||
ex_data = "%s;%s\n" % (ssid, password) + ex_data
|
||||
with open("passwd.dat", "w") as f:
|
||||
f.write(ex_data)
|
||||
profiles = read_profiles()
|
||||
except OSError:
|
||||
profiles = {}
|
||||
profiles[ssid] = password
|
||||
write_profiles(profiles)
|
||||
return True
|
||||
else:
|
||||
response = """\
|
||||
|
|
Ładowanie…
Reference in New Issue