From 8674d7c4da93a84bd49da9d066a0321368794a0e Mon Sep 17 00:00:00 2001 From: Thomas Waldmann Date: Tue, 12 Dec 2017 00:34:09 +0100 Subject: [PATCH] refactor main code, separate profile code also: - go through available wifi networks in sorted order, by rssi - differentiate open from encrypted networks - some debug prints --- main.py | 55 +++++++++++++++++++++++++++--------------------- networkconfig.py | 37 ++++++++++++++++++++++++-------- 2 files changed, 59 insertions(+), 33 deletions(-) diff --git a/main.py b/main.py index 8a97cd0..5074b0d 100644 --- a/main.py +++ b/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() - # Search WiFis in range - ssids_found = 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 - else: + if wlan_sta.isconnected(): return True + # Read known network profiles from file + profiles = networkconfig.read_profiles() + + # Search WiFis in range + networks = wlan_sta.scan() + + 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: + 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(): diff --git a/networkconfig.py b/networkconfig.py index 65dfbdc..816a0cb 100644 --- a/networkconfig.py +++ b/networkconfig.py @@ -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):
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!
@@ -105,7 +126,7 @@ def handle_root(client): - """) + """ % 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 = """\