Skip to content

Commit 0e86d19

Browse files
committed
using current PortalBase much of the complexity of logging to AIO goes away
1 parent a66a382 commit 0e86d19

2 files changed

Lines changed: 15 additions & 42 deletions

File tree

CircuitPython_Logger/aio_handler.py

Lines changed: 11 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -12,67 +12,37 @@
1212
All text above must be included in any redistribution.
1313
"""
1414

15+
from adafruit_portalbase import PortalBase
16+
1517
#pylint:disable=missing-super-argument
1618

1719
# Example:
1820
#
1921
# from aio_handler import AIOHandler
2022
# import adafruit_logging as logging
2123
# l = logging.getLogger('aio')
22-
# l.addHandler(AIOHandler('test'))
24+
# # Pass in the device object based on portal_base (Funhouse, PyPortal, MagTag, etc) as the 2nd parameter
25+
# l.addHandler(AIOHandler('test'), portal_device)
2326
# l.level = logging.ERROR
2427
# l.error("test")
2528

26-
import board
27-
import busio
28-
from digitalio import DigitalInOut
29-
import neopixel
3029
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-
4030

4131
class AIOHandler(LoggingHandler):
4232

43-
def __init__(self, name):
33+
def __init__(self, name, portal_device):
4434
"""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)
35+
self._log_feed_name=f"{name}-logging"
36+
if not issubclass(type(portal_device), PortalBase):
37+
raise TypeError("portal_device must be a PortalBase or subclass of PortalBase")
38+
self._portal_device = portal_device
6239

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)
7040

7141
def emit(self, level, msg):
72-
"""Generate the message and write it to the UART.
42+
"""Generate the message and write it to the AIO Feed.
7343
7444
:param level: The level at which to log
7545
:param msg: The core message
7646
7747
"""
78-
self._io.send_data(self._log_feed['key'], self.format(level, msg))
48+
self._portal_device.push_to_io(self._log_feed_name, self.format(level, msg))

CircuitPython_Logger/aio_test.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import time
22
import random
3+
from adafruit_pyportal import PyPortal
34
from aio_handler import AIOHandler
45
import adafruit_logging as logging
56

7+
device=PyPortal()
8+
69
l = logging.getLogger('aio')
7-
l.addHandler(AIOHandler('test'))
10+
l.addHandler(AIOHandler('test'), device)
811

912
def go():
1013
while True:

0 commit comments

Comments
 (0)