From d1052ec38a37b6adaf939593ca4f60a50c52f024 Mon Sep 17 00:00:00 2001 From: prehensile Date: Wed, 10 Aug 2016 08:58:37 +0100 Subject: [PATCH 1/3] amended wifi connection for compatibility with list format wifi.json --- stmhal/init-files/bootstrap.py | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/stmhal/init-files/bootstrap.py b/stmhal/init-files/bootstrap.py index dc739f84f9dbf..8b0fdfa1b0418 100644 --- a/stmhal/init-files/bootstrap.py +++ b/stmhal/init-files/bootstrap.py @@ -225,25 +225,39 @@ def download(url, target, expected_hash): label = ugfx.Label(5, 30, ugfx.width() - 10, ugfx.height() - 60, "Please wait") ugfx.Label(5, ugfx.height() - 30, ugfx.width() - 10, 30, "If nothing happens for 2 minutes please press the reset button on the back") -w = {} +config = {} try: if "wifi.json" in os.listdir(): with open("wifi.json") as f: - w = json.loads(f.read()) + config = json.loads(f.read()) except ValueError as e: print(e) +if type(config) is dict: config = [config] + wifi_info = "\nMore information:\nbadge.emfcamp.org/TiLDA_MK3/wifi" -if "ssid" not in w: +if not all( [ ("ssid" in n) for n in config ] ): label.text("Couldn't find a valid wifi.json :(" + wifi_info) while True: pyb.wfi() -label.text("Connecting to '%s'.\nIf this is incorrect, please check your wifi.json%s" % (w["ssid"], wifi_info)) +exc = None +retries = 6 +connected = False n = network.CC3100() -if ("pw" in w) and w["pw"]: - n.connect(w["ssid"], w["pw"], timeout=10) -else: - n.connect(w["ssid"], timeout=10) +while (retries > 0) and (not connected): + for w in config: + label.text("Connecting to '%s'.\nIf this is incorrect, please check your wifi.json%s\nTries left: %d" % (w["ssid"], wifi_info,retries)) + try: + if ("pw" in w) and w["pw"]: + n.connect(w["ssid"], w["pw"], timeout=10) + else: + n.connect(w["ssid"], timeout=10) + except Exception as e: + exc = e + connected = n.is_connected() + if connected: break + retries -= 1 +if (not connected) and (exc is not None): raise( exc ) success = False failure_counter = 0 @@ -290,4 +304,4 @@ def download(url, target, expected_hash): os.sync() label.destroy() -machine.reset() \ No newline at end of file +machine.reset() From 6c911efd850151f3235afae2909d401caf1c8bc5 Mon Sep 17 00:00:00 2001 From: prehensile Date: Fri, 12 Aug 2016 22:24:13 +0100 Subject: [PATCH 2/3] bringing bootstrap.py inline with work from Mk3-Firmware --- stmhal/init-files/bootstrap.py | 44 +++++++++++++++++----------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/stmhal/init-files/bootstrap.py b/stmhal/init-files/bootstrap.py index 8b0fdfa1b0418..0e4fe605587ae 100644 --- a/stmhal/init-files/bootstrap.py +++ b/stmhal/init-files/bootstrap.py @@ -225,39 +225,39 @@ def download(url, target, expected_hash): label = ugfx.Label(5, 30, ugfx.width() - 10, ugfx.height() - 60, "Please wait") ugfx.Label(5, ugfx.height() - 30, ugfx.width() - 10, 30, "If nothing happens for 2 minutes please press the reset button on the back") -config = {} +w = {} try: if "wifi.json" in os.listdir(): with open("wifi.json") as f: - config = json.loads(f.read()) + w = json.loads(f.read()) except ValueError as e: print(e) -if type(config) is dict: config = [config] +if type(w) is dict: + # convert original format json (dict, one network) to new (list, many networks) + w = [w] wifi_info = "\nMore information:\nbadge.emfcamp.org/TiLDA_MK3/wifi" -if not all( [ ("ssid" in n) for n in config ] ): +if not all( [ ("ssid" in config) for config in w ] ): label.text("Couldn't find a valid wifi.json :(" + wifi_info) while True: pyb.wfi() -exc = None -retries = 6 -connected = False n = network.CC3100() -while (retries > 0) and (not connected): - for w in config: - label.text("Connecting to '%s'.\nIf this is incorrect, please check your wifi.json%s\nTries left: %d" % (w["ssid"], wifi_info,retries)) - try: - if ("pw" in w) and w["pw"]: - n.connect(w["ssid"], w["pw"], timeout=10) - else: - n.connect(w["ssid"], timeout=10) - except Exception as e: - exc = e - connected = n.is_connected() - if connected: break - retries -= 1 -if (not connected) and (exc is not None): raise( exc ) +visible_ssids = [ ap['ssid'] for ap in n.list_aps() ] +known_ssids = [ ap['ssid'] for ap in w ] + +if len(set(known_ssids).intersection(visible_ssids)) < 1: + label.text("Couldn't find any networks listed in wifi.json :(" + wifi_info) + while True: pyb.wfi() + +for ap in w: + if ap["ssid"] in visible_ssids: + label.text("Connecting to '%s'.\nIf this is incorrect, please check your wifi.json%s" % (ap["ssid"], wifi_info)) + if ("pw" in ap) and ap["pw"]: + n.connect(ap["ssid"], ap["pw"], timeout=10) + else: + n.connect(ap["ssid"], timeout=10) + break # found a visible AP, don't bother with any more success = False failure_counter = 0 @@ -304,4 +304,4 @@ def download(url, target, expected_hash): os.sync() label.destroy() -machine.reset() +machine.reset() \ No newline at end of file From fbd5d8f376bc3d96c8ce89a74f99b97f15e9c22b Mon Sep 17 00:00:00 2001 From: prehensile Date: Fri, 12 Aug 2016 23:00:17 +0100 Subject: [PATCH 3/3] amended fresh_wifi_json for list format json --- stmhal/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stmhal/main.c b/stmhal/main.c index 226b34dcd0065..60b3ed220bbb6 100644 --- a/stmhal/main.c +++ b/stmhal/main.c @@ -138,7 +138,7 @@ static const char fresh_bootstrap_py[] = #include "genhdr/bootstrap.py.h" ; -static const char fresh_wifi_json[] = "{\"ssid\":\"emfcamp-insecure\"}\r\n"; +static const char fresh_wifi_json[] = "[{\"ssid\":\"emfcamp-insecure\"}]\r\n"; static const char fresh_pybcdc_inf[] = #include "genhdr/pybcdc_inf.h"