Skip to content

Commit 48bae80

Browse files
authored
Merge pull request #564 from dastels/cp_logging
Tweaks to reflect changes to the logging module and add an AdafruitIO handler.
2 parents 564c2c6 + 761e5a2 commit 48bae80

5 files changed

Lines changed: 113 additions & 9 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
*~
22
Hue_Controller/secrets.h
33
.idea
4+
5+
CircuitPython_Logger/secrets\.py
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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))

CircuitPython_Logger/aio_test.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import time
2+
import random
3+
from aio_handler import AIOHandler
4+
import adafruit_logging as logging
5+
6+
l = logging.getLogger('aio')
7+
l.addHandler(AIOHandler('test'))
8+
9+
while True:
10+
t = random.randint(1, 5)
11+
if t == 1:
12+
l.debug("debug message: %d", random.randint(0, 1000))
13+
elif t == 2:
14+
l.info("info message: %d", random.randint(0, 1000))
15+
elif t == 3:
16+
l.warning("warning message: %d", random.randint(0, 1000))
17+
elif t == 4:
18+
l.error("error message: %d", random.randint(0, 1000))
19+
elif t == 5:
20+
l.critical("critical message: %d", random.randint(0, 1000))
21+
time.sleep(5.0 + (random.random() * 5.0))

CircuitPython_Logger/file_handler.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

1717
# Example:
1818
#
19+
#
1920
# from file_handler import FileHandler
20-
# from adafruit_logger import Logger, ERROR
21-
# l = Logger(FileHandler('log.txt'))
22-
# l.level = ERROR
21+
# import adafruit_logging as logging
22+
# l = logging.getLogger('file')
23+
# l.addHandler(FileHandler('log.txt'))
24+
# l.level = logging.ERROR
2325
# l.error("test")
2426

25-
from adafruit_logger import LoggingHandler
27+
from adafruit_logging import LoggingHandler
2628

2729
class FileHandler(LoggingHandler):
2830

CircuitPython_Logger/uart_handler.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,16 @@
1919
#
2020
# import board
2121
# import busio
22-
# from adafruit_logger.uart_handler import UartHandler
23-
# from adafruit_logger import *
22+
# from uart_handler import UartHandler
23+
# import adafruit_logging as logging
2424
#
2525
# uart = busio.UART(board.TX, board.RX, baudrate=115200)
26-
# logger = Logger(UartHandler(uart))
27-
# logger.level = INFO
26+
# logger = logging.getLogger('uart')
27+
# logger.addHandler(UartHandler(uart))
28+
# logger.level = logging.INFO
2829
# logger.info('testing')
2930

30-
from adafruit_logger import LoggingHandler
31+
from adafruit_logging import LoggingHandler
3132

3233
class UartHandler(LoggingHandler):
3334
"""Send logging output to a serial port."""

0 commit comments

Comments
 (0)