Skip to content

Commit 77cd194

Browse files
Merge pull request #1 from jasonlshelton/Add_Push_IO
Add push io
2 parents 3da6b3c + 989d77c commit 77cd194

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

adafruit_pyportal.py

+33-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:
@@ -252,6 +254,9 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
252254
if url and not self._uselocal:
253255
self._connect_esp()
254256

257+
if self._debug:
258+
print("My IP address is", self._esp.pretty_ip(self._esp.ip_address))
259+
255260
# set the default background
256261
self.set_background(self._default_bg)
257262
board.DISPLAY.show(self.splash)
@@ -649,6 +654,33 @@ def image_converter_url(image_url, width, height, color_depth=16):
649654
width, height,
650655
color_depth, image_url)
651656

657+
def io_push(self, feed, data):
658+
# pylint: disable=line-too-long
659+
"""Push data to an adafruit.io feed
660+
661+
:param str feed: Name of feed to push data to.
662+
:param data: data to send to feed
663+
664+
"""
665+
# pylint: enable=line-too-long
666+
667+
try:
668+
aio_username = secrets['aio_username']
669+
aio_key = secrets['aio_key']
670+
except KeyError:
671+
raise KeyError("\n\n")
672+
673+
wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(self._esp, secrets, None)
674+
io_connect = RESTClient(aio_username, aio_key, wifi)
675+
676+
try:
677+
feed_id = io_connect.get_feed(feed)
678+
except AdafruitIO_RequestError:
679+
# If no feed exists, create one
680+
feed_id = io_connect.create_new_feed(feed)
681+
682+
io_connect.send_data(feed_id['key'], data)
683+
652684
def fetch(self, refresh_url=None):
653685
"""Fetch data from the url we initialized with, perfom any parsing,
654686
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)