From 76694786139480526aec076782ae3318ae175f13 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Mon, 29 Apr 2019 12:29:32 -0700 Subject: [PATCH 1/8] Added IO_push added function to push a data point to an adafruitio.com feed. Creates feed if it does not exist --- adafruit_pyportal.py | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index beb03ad..4424142 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -53,7 +53,7 @@ import adafruit_touchscreen import neopixel -from adafruit_esp32spi import adafruit_esp32spi +from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager import adafruit_esp32spi.adafruit_esp32spi_requests as requests try: from adafruit_display_text.text_area import TextArea # pylint: disable=unused-import @@ -69,6 +69,8 @@ import rtc import supervisor +from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError + try: from secrets import secrets except ImportError: @@ -252,6 +254,9 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None, if url and not self._uselocal: self._connect_esp() + if self._debug: + print("My IP address is", self._esp.pretty_ip(self._esp.ip_address)) + # set the default background self.set_background(self._default_bg) board.DISPLAY.show(self.splash) @@ -648,7 +653,34 @@ def image_converter_url(image_url, width, height, color_depth=16): return IMAGE_CONVERTER_SERVICE % (aio_username, aio_key, width, height, color_depth, image_url) + + def io_push(self, feed, data): + # pylint: disable=line-too-long + """Push data to an adafruit.io feed + + :param str feed: Name of feed to push data to. + :param data: data to send to feed + + """ + # pylint: enable=line-too-long + + try: + aio_username = secrets['aio_username'] + aio_key = secrets['aio_key'] + except KeyError: + raise KeyError("\n\n") + + wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None) + io = RESTClient(aio_username, aio_key, wifi) + + try: + feedID = io.get_feed(feed) + except AdafruitIO_RequestError: + # If no feed exists, create one + feedID = io.create_new_feed(feed) + io.send_data(feedID['key'], data) + def fetch(self, refresh_url=None): """Fetch data from the url we initialized with, perfom any parsing, and display text or graphics. This function does pretty much everything From 74b4c5c17fdab67b86d94521174c3498873c7ad7 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Mon, 29 Apr 2019 12:45:48 -0700 Subject: [PATCH 2/8] fixed pylint issues --- adafruit_pyportal.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 4424142..57f394e 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -256,7 +256,7 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None, if self._debug: print("My IP address is", self._esp.pretty_ip(self._esp.ip_address)) - + # set the default background self.set_background(self._default_bg) board.DISPLAY.show(self.splash) @@ -669,18 +669,18 @@ def io_push(self, feed, data): aio_key = secrets['aio_key'] except KeyError: raise KeyError("\n\n") - + wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None) - io = RESTClient(aio_username, aio_key, wifi) + io_connect = RESTClient(aio_username, aio_key, wifi) try: - feedID = io.get_feed(feed) + feed_ID = io_connect.get_feed(feed) except AdafruitIO_RequestError: # If no feed exists, create one - feedID = io.create_new_feed(feed) + feed_ID = io_connect.create_new_feed(feed) + + io_connect.send_data(feed_ID['key'], data) - io.send_data(feedID['key'], data) - def fetch(self, refresh_url=None): """Fetch data from the url we initialized with, perfom any parsing, and display text or graphics. This function does pretty much everything From 0464b87faf27f84488d91df3db719c8198e1f1ca Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Mon, 29 Apr 2019 12:57:51 -0700 Subject: [PATCH 3/8] more pylint corrections --- adafruit_pyportal.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 57f394e..1f232e8 100644 --- a/adafruit_pyportal.py +++ b/adafruit_pyportal.py @@ -653,7 +653,7 @@ def image_converter_url(image_url, width, height, color_depth=16): return IMAGE_CONVERTER_SERVICE % (aio_username, aio_key, width, height, color_depth, image_url) - + def io_push(self, feed, data): # pylint: disable=line-too-long """Push data to an adafruit.io feed @@ -674,12 +674,12 @@ def io_push(self, feed, data): io_connect = RESTClient(aio_username, aio_key, wifi) try: - feed_ID = io_connect.get_feed(feed) + feed_id = io_connect.get_feed(feed) except AdafruitIO_RequestError: # If no feed exists, create one - feed_ID = io_connect.create_new_feed(feed) + feed_id = io_connect.create_new_feed(feed) - io_connect.send_data(feed_ID['key'], data) + io_connect.send_data(feed_id['key'], data) def fetch(self, refresh_url=None): """Fetch data from the url we initialized with, perfom any parsing, From 989d77c39ebe5b83693f66adc881057c6e152a0b Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Mon, 29 Apr 2019 15:14:40 -0700 Subject: [PATCH 4/8] updated docs config --- docs/conf.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/conf.py b/docs/conf.py index 355f02b..02ae91b 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -23,7 +23,7 @@ autodoc_mock_imports = ["rtc", "supervisor", "pulseio", "audioio", "displayio", "neopixel", "microcontroller", "adafruit_touchscreen", "adafruit_bitmap_font", "adafruit_display_text", "adafruit_esp32spi", "secrets", - "adafruit_sdcard", "storage"] + "adafruit_sdcard", "storage", "adafruit_io"] intersphinx_mapping = {'python': ('https://docs.python.org/3.4', None),'BusDevice': ('https://circuitpython.readthedocs.io/projects/busdevice/en/latest/', None),'CircuitPython': ('https://circuitpython.readthedocs.io/en/latest/', None)} From a748fb036aaff6ac8fe5e24e48d29ab3a872dec5 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Wed, 1 May 2019 14:27:16 -0700 Subject: [PATCH 5/8] 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 6/8] 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 7/8] 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 From d2ab7fd98ea69bd3d05e39aeb40ae39d41086905 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Tue, 4 Jun 2019 16:12:50 -0700 Subject: [PATCH 8/8] Style changes as requested --- adafruit_pyportal.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index 8079672..942da58 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_key, data): + def push_to_io(self, feed_key, data): # pylint: disable=line-too-long """Push data to an adafruit.io feed - :param str feed_key: Name of feed to push data to. + :param str feed_key: Name of feed key to push data to. :param data: data to send to feed """