Skip to content

Commit b1718ca

Browse files
authored
Merge pull request #36 from jasonlshelton/master
Added IO_push
2 parents 013d0d6 + ba20f85 commit b1718ca

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

adafruit_pyportal.py

+47-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
import adafruit_touchscreen
5454
import neopixel
5555

56-
from adafruit_esp32spi import adafruit_esp32spi
56+
from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager
5757
import adafruit_esp32spi.adafruit_esp32spi_requests as requests
5858
try:
5959
from adafruit_display_text.text_area import TextArea # pylint: disable=unused-import
@@ -69,6 +69,8 @@
6969
import rtc
7070
import supervisor
7171

72+
from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError
73+
7274
try:
7375
from secrets import secrets
7476
except ImportError:
@@ -263,6 +265,9 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
263265
if url and not self._uselocal:
264266
self._connect_esp()
265267

268+
if self._debug:
269+
print("My IP address is", self._esp.pretty_ip(self._esp.ip_address))
270+
266271
# set the default background
267272
self.set_background(self._default_bg)
268273
board.DISPLAY.show(self.splash)
@@ -662,6 +667,47 @@ def image_converter_url(image_url, width, height, color_depth=16):
662667
width, height,
663668
color_depth, image_url)
664669

670+
def push_to_io(self, feed_key, data):
671+
# pylint: disable=line-too-long
672+
"""Push data to an adafruit.io feed
673+
674+
:param str feed_key: Name of feed key to push data to.
675+
:param data: data to send to feed
676+
677+
"""
678+
# pylint: enable=line-too-long
679+
680+
try:
681+
aio_username = secrets['aio_username']
682+
aio_key = secrets['aio_key']
683+
except KeyError:
684+
raise KeyError("Adafruit IO secrets are kept in secrets.py, please add them there!\n\n")
685+
686+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None)
687+
io_client = RESTClient(aio_username, aio_key, wifi)
688+
689+
while True:
690+
try:
691+
feed_id = io_client.get_feed(feed_key)
692+
except AdafruitIO_RequestError:
693+
# If no feed exists, create one
694+
feed_id = io_client.create_new_feed(feed_key)
695+
except RuntimeError as exception:
696+
print("An error occured, retrying! 1 -", exception)
697+
continue
698+
break
699+
700+
while True:
701+
try:
702+
io_client.send_data(feed_id['key'], data)
703+
except RuntimeError as exception:
704+
print("An error occured, retrying! 2 -", exception)
705+
continue
706+
except NameError as exception:
707+
print(feed_id['key'], data, exception)
708+
continue
709+
break
710+
665711
def fetch(self, refresh_url=None):
666712
"""Fetch data from the url we initialized with, perfom any parsing,
667713
and display text or graphics. This function does pretty much everything

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
autodoc_mock_imports = ["rtc", "supervisor", "pulseio", "audioio", "displayio", "neopixel",
2424
"microcontroller", "adafruit_touchscreen", "adafruit_bitmap_font",
2525
"adafruit_display_text", "adafruit_esp32spi", "secrets",
26-
"adafruit_sdcard", "storage"]
26+
"adafruit_sdcard", "storage", "adafruit_io"]
2727

2828

2929
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)}

0 commit comments

Comments
 (0)