Skip to content
48 changes: 47 additions & 1 deletion adafruit_pyportal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -69,6 +69,8 @@
import rtc
import supervisor

from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError

try:
from secrets import secrets
except ImportError:
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
Expand Down