When Internet connectivity is available (usually via WiFi), data may be logged to the Adafruit IO data service.
See this guide to get started with Adafruit IO:
The following uses a PyPortal (M4 + ESP32) in writing a handler to send log messages to Adafruit IO.
Most of the code is in the constructor to set up the connection to the ESP32 and Adafruit IO. You pass a string to the constructor that is used to create the feed name which is -logging.
Line terminators don't need to be added, so we don't need a format method; we can directly use the inherited one.
# SPDX-FileCopyrightText: 2018 Dave Astels for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
Adafruit IO based message handler for CircuitPython logging.
Adafruit invests time and resources providing this open source code.
Please support Adafruit and open source hardware by purchasing
products from Adafruit!
Written by Dave Astels for Adafruit Industries
Copyright (c) 2018 Adafruit Industries
Licensed under the MIT license.
All text above must be included in any redistribution.
"""
from adafruit_portalbase import PortalBase
from adafruit_logging import Handler, NOTSET
class AIOHandler(Handler):
def __init__(self, name, portal_device, level: int = NOTSET):
"""Create an instance."""
super().__init__(level)
self._log_feed_name = f"{name}-logging"
if not issubclass(type(portal_device), PortalBase):
raise TypeError(
"portal_device must be a PortalBase or subclass of PortalBase"
)
self._portal_device = portal_device
def emit(self, record):
"""Generate the message and write it to the AIO Feed.
:param record: The record (message object) to be logged
"""
self._portal_device.push_to_io(self._log_feed_name, self.format(record))
You'll need a settings.toml file to hold your WiFi and Adafruit IO credentials. You will also need the required libraries for your board and an Adafruit IO account. See this guide for setting it all up on a PyPortal.
The example code to use the above handler on a PyPortal or M4 Express WiFi:
# SPDX-FileCopyrightText: 2019 Dave Astels for Adafruit Industries
#
# SPDX-License-Identifier: MIT
import time
import random
from adafruit_pyportal import PyPortal
from aio_handler import AIOHandler
import adafruit_logging as logging
device = PyPortal()
l = logging.getLogger("aio")
l.addHandler(AIOHandler("test", device))
while True:
t = random.randint(1, 5)
if t == 1:
print("debug")
l.debug("debug message: %d", random.randint(0, 1000))
elif t == 2:
print("info")
l.info("info message: %d", random.randint(0, 1000))
elif t == 3:
print("warning")
l.warning("warning message: %d", random.randint(0, 1000))
elif t == 4:
print("error")
l.error("error message: %d", random.randint(0, 1000))
elif t == 5:
print("critical")
l.critical("critical message: %d", random.randint(0, 1000))
time.sleep(5.0 + (random.random() * 5.0))
Page last edited June 20, 2025
Text editor powered by tinymce.