From a7b8d4aa8cb58b6caa291004506b4bc094403b04 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 18 Feb 2019 09:46:26 -0800 Subject: [PATCH 1/8] Added reset after a certain number of attempts --- adafruit_esp32spi/adafruit_esp32spi_wifimanager.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 3476d8d..1012ff1 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -39,11 +39,12 @@ class ESPSPI_WiFiManager: """ A class to help manage the Wifi connection """ - def __init__(self, esp, settings, status_neopixel=None): + def __init__(self, esp, settings, attempts=3, status_neopixel=None): """ :param ESP_SPIcontrol esp: The ESP object we are using :param dict settings: The WiFi and Adafruit IO Settings (See examples) - :param status_neopixel: (Pptional) The neopixel pin - Usually board.NEOPIXEL (default=None) + :param attempts: (Optional) Failed attempts before resetting the ESP32 (default=3) + :param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None) :type status_neopixel: Pin """ # Read the settings @@ -51,6 +52,7 @@ def __init__(self, esp, settings, status_neopixel=None): self.debug = False self.ssid = settings['ssid'] self.password = settings['password'] + self.attempts = 3 requests.set_interface(self._esp) if status_neopixel: self.neopix = neopixel.NeoPixel(status_neopixel, 1, brightness=0.2) @@ -69,15 +71,22 @@ def connect(self): print("MAC addr:", [hex(i) for i in self._esp.MAC_address]) for access_pt in self._esp.scan_networks(): print("\t%s\t\tRSSI: %d" % (str(access_pt['ssid'], 'utf-8'), access_pt['rssi'])) + failure_count = 0 while not self._esp.is_connected: try: if self.debug: print("Connecting to AP...") self.neo_status((100, 0, 0)) self._esp.connect_AP(bytes(self.ssid, 'utf-8'), bytes(self.password, 'utf-8')) + failure_count = 0 self.neo_status((0, 100, 0)) except (ValueError, RuntimeError) as error: print("Failed to connect, retrying\n", error) + failure_count += 1 + if failure_count >= self.attempts: + failure_count = 0 + self._esp.reset() + print("Resetting ESP32\n", error) continue def get(self, url, **kw): From 8be610cd4b19380d046ce20413cfc822daaff628 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 18 Feb 2019 09:51:29 -0800 Subject: [PATCH 2/8] Linting --- adafruit_esp32spi/adafruit_esp32spi_wifimanager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 1012ff1..b4c67f6 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -39,7 +39,7 @@ class ESPSPI_WiFiManager: """ A class to help manage the Wifi connection """ - def __init__(self, esp, settings, attempts=3, status_neopixel=None): + def __init__(self, esp, settings, attempts=1, status_neopixel=None): """ :param ESP_SPIcontrol esp: The ESP object we are using :param dict settings: The WiFi and Adafruit IO Settings (See examples) @@ -52,7 +52,7 @@ def __init__(self, esp, settings, attempts=3, status_neopixel=None): self.debug = False self.ssid = settings['ssid'] self.password = settings['password'] - self.attempts = 3 + self.attempts = attempts requests.set_interface(self._esp) if status_neopixel: self.neopix = neopixel.NeoPixel(status_neopixel, 1, brightness=0.2) From 303425035494af7dca0d21373dd4ab678afb1d71 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 18 Feb 2019 10:12:45 -0800 Subject: [PATCH 3/8] Fixed error introduced from Linting --- adafruit_esp32spi/adafruit_esp32spi_wifimanager.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index b4c67f6..71b65b6 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -39,11 +39,11 @@ class ESPSPI_WiFiManager: """ A class to help manage the Wifi connection """ - def __init__(self, esp, settings, attempts=1, status_neopixel=None): + def __init__(self, esp, settings, status_neopixel=None, attempts=1): """ :param ESP_SPIcontrol esp: The ESP object we are using :param dict settings: The WiFi and Adafruit IO Settings (See examples) - :param attempts: (Optional) Failed attempts before resetting the ESP32 (default=3) + :param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=3) :param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None) :type status_neopixel: Pin """ From cd633725d241d5716ed1252a41cdd3220c397df6 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 18 Feb 2019 10:59:11 -0800 Subject: [PATCH 4/8] Added reset to the get and post methods on error --- adafruit_esp32spi/adafruit_esp32spi_wifimanager.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 71b65b6..4eb8c4d 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -43,7 +43,7 @@ def __init__(self, esp, settings, status_neopixel=None, attempts=1): """ :param ESP_SPIcontrol esp: The ESP object we are using :param dict settings: The WiFi and Adafruit IO Settings (See examples) - :param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=3) + :param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=1) :param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None) :type status_neopixel: Pin """ @@ -104,7 +104,16 @@ def get(self, url, **kw): if not self._esp.is_connected: self.connect() self.neo_status((100, 100, 0)) - return_val = requests.get(url, **kw) + attempt_count = 0 + while attempt_count < self.attempts: + try: + attempt_count += 1 + return_val = requests.get(url, **kw) + except(ValueError, RuntimeError) as error: + if attempt_count >= self.attempts: + attempt_count = 0 + self._esp.reset() + print("Resetting ESP32\n", error) self.neo_status(0) return return_val From 4b56b5403a32e75a830f1e85c5cf30e8eae283c8 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Mon, 18 Feb 2019 11:15:30 -0800 Subject: [PATCH 5/8] Actually saved before pushing code this time --- adafruit_esp32spi/adafruit_esp32spi_wifimanager.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 4eb8c4d..bd600ce 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -132,7 +132,16 @@ def post(self, url, **kw): if not self._esp.is_connected: self.connect() self.neo_status((100, 100, 0)) - return_val = requests.post(url, **kw) + attempt_count = 0 + while attempt_count < self.attempts: + try: + attempt_count += 1 + return_val = requests.post(url, **kw) + except(ValueError, RuntimeError) as error: + if attempt_count >= self.attempts: + attempt_count = 0 + self._esp.reset() + print("Resetting ESP32\n", error) self.neo_status(0) return return_val From b878f96583ab112c89ca4c7cb343611fe099c7bd Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Tue, 19 Feb 2019 08:49:42 -0800 Subject: [PATCH 6/8] Removed attempt code from get/post. Examples reset. --- .../adafruit_esp32spi_wifimanager.py | 38 +++++++------------ examples/esp32spi_aio_post.py | 1 + examples/esp32spi_cheerlights.py | 1 + 3 files changed, 15 insertions(+), 25 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 11b570d..d62fcd2 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -39,11 +39,11 @@ class ESPSPI_WiFiManager: """ A class to help manage the Wifi connection """ - def __init__(self, esp, settings, status_neopixel=None, attempts=1): + def __init__(self, esp, settings, status_neopixel=None, attempts=2): """ :param ESP_SPIcontrol esp: The ESP object we are using :param dict settings: The WiFi and Adafruit IO Settings (See examples) - :param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=1) + :param int attempts: (Optional) Failed attempts before resetting the ESP32 (default=2) :param status_neopixel: (Optional) The neopixel pin - Usually board.NEOPIXEL (default=None) :type status_neopixel: Pin """ @@ -60,6 +60,14 @@ def __init__(self, esp, settings, status_neopixel=None, attempts=1): self.neopix = None self.neo_status(0) + def reset(self): + """ + Perform a hard reset on the ESP32 + """ + if self.debug: + print("Resetting ESP32\n", error) + self._esp.reset() + def connect(self): """ Attempt to connect to WiFi using the current settings @@ -85,8 +93,7 @@ def connect(self): failure_count += 1 if failure_count >= self.attempts: failure_count = 0 - self._esp.reset() - print("Resetting ESP32\n", error) + self.reset() continue def get(self, url, **kw): @@ -104,16 +111,7 @@ def get(self, url, **kw): if not self._esp.is_connected: self.connect() self.neo_status((100, 100, 0)) - attempt_count = 0 - while attempt_count < self.attempts: - try: - attempt_count += 1 - return_val = requests.get(url, **kw) - except(ValueError, RuntimeError) as error: - if attempt_count >= self.attempts: - attempt_count = 0 - self._esp.reset() - print("Resetting ESP32\n", error) + return_val = requests.get(url, **kw) self.neo_status(0) return return_val @@ -132,17 +130,7 @@ def post(self, url, **kw): if not self._esp.is_connected: self.connect() self.neo_status((100, 100, 0)) - attempt_count = 0 - while attempt_count < self.attempts: - try: - attempt_count += 1 - return_val = requests.post(url, **kw) - except(ValueError, RuntimeError) as error: - if attempt_count >= self.attempts: - attempt_count = 0 - self._esp.reset() - print("Resetting ESP32\n", error) - self.neo_status(0) + return_val = requests.post(url, **kw) return return_val def put(self, url, **kw): diff --git a/examples/esp32spi_aio_post.py b/examples/esp32spi_aio_post.py index b6d99a9..2527369 100644 --- a/examples/esp32spi_aio_post.py +++ b/examples/esp32spi_aio_post.py @@ -38,6 +38,7 @@ print("OK") except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) + wifi.reset() continue response = None time.sleep(15) diff --git a/examples/esp32spi_cheerlights.py b/examples/esp32spi_cheerlights.py index d9f7781..b44060f 100644 --- a/examples/esp32spi_cheerlights.py +++ b/examples/esp32spi_cheerlights.py @@ -47,6 +47,7 @@ response.close() except (ValueError, RuntimeError) as e: print("Failed to get data, retrying\n", e) + wifi.reset() continue if not value: From 41f7c256e15dd5d5f5eedc6b2fe13f85bba94652 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Tue, 19 Feb 2019 08:52:18 -0800 Subject: [PATCH 7/8] Small bug fix --- adafruit_esp32spi/adafruit_esp32spi_wifimanager.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index d62fcd2..1347bfe 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -65,7 +65,7 @@ def reset(self): Perform a hard reset on the ESP32 """ if self.debug: - print("Resetting ESP32\n", error) + print("Resetting ESP32") self._esp.reset() def connect(self): From 2bd2a3b9d2b13b7dc3595d2bff1db4871d6408f8 Mon Sep 17 00:00:00 2001 From: Melissa LeBlanc-Williams Date: Tue, 19 Feb 2019 09:40:52 -0800 Subject: [PATCH 8/8] Changed WiFi is use to blue because yellow can mean safe mode/error --- adafruit_esp32spi/adafruit_esp32spi_wifimanager.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py index 1347bfe..cc65f92 100755 --- a/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py +++ b/adafruit_esp32spi/adafruit_esp32spi_wifimanager.py @@ -110,7 +110,7 @@ def get(self, url, **kw): """ if not self._esp.is_connected: self.connect() - self.neo_status((100, 100, 0)) + self.neo_status((0, 0, 100)) return_val = requests.get(url, **kw) self.neo_status(0) return return_val @@ -129,7 +129,7 @@ def post(self, url, **kw): """ if not self._esp.is_connected: self.connect() - self.neo_status((100, 100, 0)) + self.neo_status((0, 0, 100)) return_val = requests.post(url, **kw) return return_val @@ -147,7 +147,7 @@ def put(self, url, **kw): """ if not self._esp.is_connected: self.connect() - self.neo_status((100, 100, 0)) + self.neo_status((0, 0, 100)) return_val = requests.put(url, **kw) self.neo_status(0) return return_val @@ -166,7 +166,7 @@ def patch(self, url, **kw): """ if not self._esp.is_connected: self.connect() - self.neo_status((100, 100, 0)) + self.neo_status((0, 0, 100)) return_val = requests.patch(url, **kw) self.neo_status(0) return return_val @@ -185,7 +185,7 @@ def delete(self, url, **kw): """ if not self._esp.is_connected: self.connect() - self.neo_status((100, 100, 0)) + self.neo_status((0, 0, 100)) return_val = requests.delete(url, **kw) self.neo_status(0) return return_val @@ -201,7 +201,7 @@ def ping(self, host, ttl=250): """ if not self._esp.is_connected: self.connect() - self.neo_status((100, 100, 0)) + self.neo_status((0, 0, 100)) response_time = self._esp.ping(host, ttl=ttl) self.neo_status(0) return response_time