|
| 1 | +""" |
| 2 | +Adafruit IO based message handler for CircuitPython logging. |
| 3 | +
|
| 4 | +Adafruit invests time and resources providing this open source code. |
| 5 | +Please support Adafruit and open source hardware by purchasing |
| 6 | +products from Adafruit! |
| 7 | +
|
| 8 | +Written by Dave Astels for Adafruit Industries |
| 9 | +Copyright (c) 2018 Adafruit Industries |
| 10 | +Licensed under the MIT license. |
| 11 | +
|
| 12 | +All text above must be included in any redistribution. |
| 13 | +""" |
| 14 | + |
| 15 | +#pylint:disable=missing-super-argument |
| 16 | + |
| 17 | +# Example: |
| 18 | +# |
| 19 | +# from aio_handler import AIOHandler |
| 20 | +# import adafruit_logging as logging |
| 21 | +# l = logging.getLogger('aio') |
| 22 | +# l.addHandler(AIOHandler('test')) |
| 23 | +# l.level = logging.ERROR |
| 24 | +# l.error("test") |
| 25 | + |
| 26 | +import board |
| 27 | +import busio |
| 28 | +from digitalio import DigitalInOut |
| 29 | +import neopixel |
| 30 | +from adafruit_logging import LoggingHandler |
| 31 | +from adafruit_esp32spi import adafruit_esp32spi, adafruit_esp32spi_wifimanager |
| 32 | +from adafruit_io.adafruit_io import RESTClient, AdafruitIO_RequestError |
| 33 | + |
| 34 | +try: |
| 35 | + from secrets import secrets |
| 36 | +except ImportError: |
| 37 | + print("WiFi secrets are kept in secrets.py, please add them there!") |
| 38 | + raise |
| 39 | + |
| 40 | + |
| 41 | +class AIOHandler(LoggingHandler): |
| 42 | + |
| 43 | + def __init__(self, name): |
| 44 | + """Create an instance.""" |
| 45 | + # PyPortal ESP32 Setup |
| 46 | + esp32_cs = DigitalInOut(board.ESP_CS) |
| 47 | + esp32_ready = DigitalInOut(board.ESP_BUSY) |
| 48 | + esp32_reset = DigitalInOut(board.ESP_RESET) |
| 49 | + spi = busio.SPI(board.SCK, board.MOSI, board.MISO) |
| 50 | + esp = adafruit_esp32spi.ESP_SPIcontrol(spi, esp32_cs, esp32_ready, esp32_reset) |
| 51 | + status_light = neopixel.NeoPixel(board.NEOPIXEL, 1, brightness=0.2) |
| 52 | + wifi = adafruit_esp32spi_wifimanager.ESPSPI_WiFiManager(esp, secrets, status_light) |
| 53 | + |
| 54 | + # Set your Adafruit IO Username and Key in secrets.py |
| 55 | + # (visit io.adafruit.com if you need to create an account, |
| 56 | + # or if you need your Adafruit IO key.) |
| 57 | + ADAFRUIT_IO_USER = secrets['adafruit_io_user'] |
| 58 | + ADAFRUIT_IO_KEY = secrets['adafruit_io_key'] |
| 59 | + |
| 60 | + # Create an instance of the Adafruit IO REST client |
| 61 | + self._io = RESTClient(ADAFRUIT_IO_USER, ADAFRUIT_IO_KEY, wifi) |
| 62 | + |
| 63 | + self._name = '{0}-logging'.format(name) |
| 64 | + try: |
| 65 | + # Get the logging feed from Adafruit IO |
| 66 | + self._log_feed = self._io.get_feed(self._name) |
| 67 | + except AdafruitIO_RequestError: |
| 68 | + # If no logging feed exists, create one |
| 69 | + self._log_feed = self._io.create_new_feed(self._name) |
| 70 | + |
| 71 | + def emit(self, level, msg): |
| 72 | + """Generate the message and write it to the UART. |
| 73 | +
|
| 74 | + :param level: The level at which to log |
| 75 | + :param msg: The core message |
| 76 | +
|
| 77 | + """ |
| 78 | + self._io.send_data(self._log_feed['key'], self.format(level, msg)) |
0 commit comments