Andreas Motzek
Published © CC BY

MQTT-Enabled Scrolling Text with ESP8266 and MicroPython

Use your ESP8266 to receive text via MQTT and scroll it on an OLED display.

IntermediateFull instructions provided1 hour1,887
MQTT-Enabled Scrolling Text with ESP8266 and MicroPython

Things used in this project

Hardware components

ESP8266 with SSD1306 OLED and CP2102 USB serial
×1

Software apps and online services

MicroPython Firmware for ESP8266 v1.19.1
Download esp8266-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 20, a user name in line 23 and a MQTT topic name in line 25 and then upload this file to your ESP8266.
import uos
from cooperative_multitasking import Tasks
from network import WLAN, AP_IF
from mqtt_client import MQTTClient
from machine import Pin, I2C
from ssd1306 import SSD1306_I2C
from scroller import Scroller
import kolony

tasks = Tasks()

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

i2c = I2C(scl = Pin(5), sda = Pin(4), freq = 300000)
display = SSD1306_I2C(128, 32, i2c)
scroller = None
kolony_x = 0
kolony_y = 0
twinker_count = 0

def receive_message(topic, payload_object):
    global scroller
    scroller = Scroller(display, payload_object[':message'])

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

def is_connected():
    return client.is_connected()

def is_not_connected():
    return not client.is_connected()

def has_message():
    return scroller is not None

def kolony_sleep():
    kolony_move()
    display.fill(0)
    kolony.eyes_closed(display, kolony_x, kolony_y)
    kolony.mouth_flat(display, kolony_x, kolony_y)
    display.show()
    tasks.only_one_of(tasks.when_then(is_connected, kolony_awake),
                      tasks.after(60000, kolony_sleep))

def kolony_awake():
    display.fill(0)
    kolony.eyes_open(display, kolony_x, kolony_y)
    kolony.mouth_flat(display, kolony_x, kolony_y)
    display.show()
    tasks.only_one_of(tasks.when_then(is_not_connected, kolony_sleep),
                      tasks.when_then(has_message, scroll_message),
                      tasks.after(500 + randint(0, 9500), kolony_twinker))

def kolony_twinker():
    global twinker_count
    twinker_count += 1
    if twinker_count <= 5:
        display.fill(0)
        kolony.eyes_closed(display, kolony_x, kolony_y)
        kolony.mouth_flat(display, kolony_x, kolony_y)
        display.show()
    else:
        twinker_count = 0
        kolony_move()
    tasks.after(150, kolony_awake)

def kolony_move():
    global kolony_x, kolony_y
    kolony_x = randint(-7, 101)
    kolony_y = randint(-6, 4)

def scroll_message():
    global scroller
    scroller.scroll()
    display.show()
    if is_connected():
        tasks.after(100, scroll_message)
    else:
        scroller = None
        tasks.now(kolony_sleep)

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

tasks.now(kolony_sleep)

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

MicroPython Libraries

Download cooperative_multitasking.mpy, kolony.mpy, mqtt.mpy and scroller.mpy from https://bitbucket.org/amotzek/micro-python/downloads/ and upload them to the lib folder of your ESP8266.

Credits

Andreas Motzek

Andreas Motzek

16 projects • 9 followers
I like algorithms, code, statistics and decisions based on them, solving difficult problems and, if possible, avoiding them elegantly.

Comments