From a748fb036aaff6ac8fe5e24e48d29ab3a872dec5 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Wed, 1 May 2019 14:27:16 -0700 Subject: [PATCH 1/3] Added exception catching to network related function calls to account for occasional timeout from the esp chip --- adafruit_pyportal.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 1f232e8..cb0198c 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -678,8 +678,13 @@ def io_push(self, feed, data): except AdafruitIO_RequestError: # If no feed exists, create one feed_id = io_connect.create_new_feed(feed) + except RuntimeError as exception: + print("Some error occured, retrying! -", exception) - io_connect.send_data(feed_id['key'], data) + try: + io_connect.send_data(feed_id['key'], data) + except RuntimeError as exception: + print("Some error occured, retrying! -", exception) def fetch(self, refresh_url=None): """Fetch data from the url we initialized with, perfom any parsing, From 569adef0c64ad343b9771421ed471a16feb49b25 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Fri, 3 May 2019 10:36:31 -0700 Subject: [PATCH 2/3] Improved error handling allowing pyportal to retry connections that faail instead of crashing out with an error --- adafruit_pyportal.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index cb0198c..b5193d7 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -673,18 +673,27 @@ def io_push(self, feed, data): wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None) io_connect = RESTClient(aio_username, aio_key, wifi) - try: - feed_id = io_connect.get_feed(feed) - except AdafruitIO_RequestError: - # If no feed exists, create one - feed_id = io_connect.create_new_feed(feed) - except RuntimeError as exception: - print("Some error occured, retrying! -", exception) - - try: - io_connect.send_data(feed_id['key'], data) - except RuntimeError as exception: - print("Some error occured, retrying! -", exception) + while True: + try: + feed_id = io_connect.get_feed(feed) + except AdafruitIO_RequestError: + # If no feed exists, create one + feed_id = io_connect.create_new_feed(feed) + except RuntimeError as exception: + print("An error occured, retrying! 1 -", exception) + continue + break + + while True: + try: + io_connect.send_data(feed_id['key'], data) + except RuntimeError as exception: + print("An error occured, retrying! 2 -", exception) + continue + except NameError as exception: + print(feed_id['key'], data, exception) + continue + break def fetch(self, refresh_url=None): """Fetch data from the url we initialized with, perfom any parsing, From eb238599e313adcef3ed12ff79d2600aa9a991d3 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Mon, 6 May 2019 08:42:47 -0700 Subject: [PATCH 3/3] Style changes as requested --- adafruit_pyportal.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index b5193d7..8079672 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -654,11 +654,11 @@ def image_converter_url(image_url, width, height, color_depth=16): width, height, color_depth, image_url) - def io_push(self, feed, data): + def io_push(self, feed_key, data): # pylint: disable=line-too-long """Push data to an adafruit.io feed - :param str feed: Name of feed to push data to. + :param str feed_key: Name of feed to push data to. :param data: data to send to feed """ @@ -668,17 +668,17 @@ def io_push(self, feed, data): aio_username = secrets['aio_username'] aio_key = secrets['aio_key'] except KeyError: - raise KeyError("\n\n") + raise KeyError("Adafruit IO secrets are kept in secrets.py, please add them there!\n\n") wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None) - io_connect = RESTClient(aio_username, aio_key, wifi) + io_client = RESTClient(aio_username, aio_key, wifi) while True: try: - feed_id = io_connect.get_feed(feed) + feed_id = io_client.get_feed(feed_key) except AdafruitIO_RequestError: # If no feed exists, create one - feed_id = io_connect.create_new_feed(feed) + feed_id = io_client.create_new_feed(feed_key) except RuntimeError as exception: print("An error occured, retrying! 1 -", exception) continue @@ -686,7 +686,7 @@ def io_push(self, feed, data): while True: try: - io_connect.send_data(feed_id['key'], data) + io_client.send_data(feed_id['key'], data) except RuntimeError as exception: print("An error occured, retrying! 2 -", exception) continue