diff --git a/adafruit_pyportal.py b/adafruit_pyportal.py index beb03ad..942da58 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) @@ -649,6 +654,47 @@ def image_converter_url(image_url, width, height, color_depth=16): width, height, color_depth, image_url) + 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 key 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("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_client = RESTClient(aio_username, aio_key, wifi) + + while True: + try: + feed_id = io_client.get_feed(feed_key) + except AdafruitIO_RequestError: + # If no feed exists, create one + feed_id = io_client.create_new_feed(feed_key) + except RuntimeError as exception: + print("An error occured, retrying! 1 -", exception) + continue + break + + while True: + try: + io_client.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, and display text or graphics. This function does pretty much everything 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)}