|
53 | 53 | import adafruit_touchscreen
|
54 | 54 | import neopixel
|
55 | 55 |
|
56 |
| -from adafruit_esp32spi import adafruit_esp32spi |
| 56 | +from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager |
57 | 57 | import adafruit_esp32spi.adafruit_esp32spi_requests as requests
|
58 | 58 | try:
|
59 | 59 | from adafruit_display_text.text_area import TextArea # pylint: disable=unused-import
|
|
69 | 69 | import rtc
|
70 | 70 | import supervisor
|
71 | 71 |
|
| 72 | +from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError |
| 73 | + |
72 | 74 | try:
|
73 | 75 | from secrets import secrets
|
74 | 76 | except ImportError:
|
@@ -263,6 +265,9 @@ def __init__(self, *, url=None, headers=None, json_path=None, regexp_path=None,
|
263 | 265 | if url and not self._uselocal:
|
264 | 266 | self._connect_esp()
|
265 | 267 |
|
| 268 | + if self._debug: |
| 269 | + print("My IP address is", self._esp.pretty_ip(self._esp.ip_address)) |
| 270 | + |
266 | 271 | # set the default background
|
267 | 272 | self.set_background(self._default_bg)
|
268 | 273 | board.DISPLAY.show(self.splash)
|
@@ -662,6 +667,47 @@ def image_converter_url(image_url, width, height, color_depth=16):
|
662 | 667 | width, height,
|
663 | 668 | color_depth, image_url)
|
664 | 669 |
|
| 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 | + |
665 | 711 | def fetch(self, refresh_url=None):
|
666 | 712 | """Fetch data from the url we initialized with, perfom any parsing,
|
667 | 713 | and display text or graphics. This function does pretty much everything
|
|
0 commit comments