Andreas Motzek
Published © CC BY

MQTT-Enabled Scrolling Text with ATOM Matrix and MicroPython

Use your ATOM Matrix to receive text via MQTT and scroll it on the RGB LEDs.

IntermediateFull instructions provided1 hour5,313
MQTT-Enabled Scrolling Text with ATOM Matrix and MicroPython

Things used in this project

Hardware components

M5Stack ATOM Matrix ESP32 Development Kit
×1

Software apps and online services

MicroPython Firmware for ESP32 v1.19.1
Download esp32-20220618-v1.19.1.bin from the download page.
MQTT Client
Use the MQTT client to send messages to your device from a web browser.

Story

Read more

Code

main.py

Python
Fill in your WLAN credentials in line 21, a user name in line 19 and a MQTT topic name in line 49.
import uos
from cooperative_multitasking import Tasks
from network import WLAN, AP_IF
from mqtt_client import MQTTClient
from machine import Pin
from neopixel import NeoPixel
from font5 import Font5
from neopixel_scroller import NeopixelScroller

tasks = Tasks()

ap = WLAN(AP_IF)
ap.active(False)
ap = None

client = MQTTClient(tasks,
                    hostname = 'broker.mqttdashboard.com',
                    client_id = '',
                    user_name = '...',
                    password = '')
client.activate_wlan([('...', '...')])
client.start()

gpio27 = Pin(27, Pin.OUT)
neopixels = NeoPixel(gpio27, 25)
font = Font5()
colors = [(20, 0, 0), (20, 20, 0), (0, 20, 0), (0, 0, 20)]
scroller = None

def randint(low, high):
    s = 0
    bs = uos.urandom(4)
    for b in bs:
        s = (s << 8) | b
    return low + (s % (high - low + 1))

def random_color():
    return colors[randint(0, len(colors) - 1)]

def receive_message(topic, payload_object):
    global scroller
    try:
        message = payload_object['message']
        color = random_color()
        scroller = NeopixelScroller(neopixels, message, font, foreground_color=color)
    except:
        pass

client.subscribe('...', receive_message)

def has_message():
    return scroller is not None

def await_message():
    tasks.when_then(has_message, scroll_message)

def is_connected():
    return client.is_connected()

def scroll_message():
    scroller.scroll()
    neopixels.write()
    if is_connected():
        tasks.after(300, scroll_message)
    else:
        tasks.now(clear_display)

def clear_display():
    global scroller
    scroller = None
    for pixel_index in range(25):
        neopixels[pixel_index] = (0, 0, 0)
    neopixels.write()
    tasks.now(await_message)

tasks.now(await_message)

while tasks.available():
    tasks.run()

Credits

Andreas Motzek

Andreas Motzek

16 projects • 9 followers
I love mathematics and computer science. I work for an international consulting company.

Comments