From 76694786139480526aec076782ae3318ae175f13 Mon Sep 17 00:00:00 2001 From: Jason Shelton Date: Mon, 29 Apr 2019 12:29:32 -0700 Subject: [PATCH 1/4] 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/4] 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/4] 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/4] 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)}