Hackster is hosting Hackster Holidays, Ep. 6: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Monday!Stream Hackster Holidays, Ep. 6 on Monday!
Andreas Motzek
Published © CC BY

Execute Logo on M5Stack ESP32 Basic with MicroPython

Use your M5Stack ESP32 Basic to receive little Logo programs via MQTT and show the results.

IntermediateFull instructions provided1 hour4,846
Execute Logo on M5Stack ESP32 Basic with MicroPython

Things used in this project

Hardware components

ESP32 Basic Core IoT Development Kit
M5Stack ESP32 Basic Core IoT 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 68, a user name in line 66 and a MQTT topic name in line 69 then upload the file with ampy.
from cooperative_multitasking import Tasks
from network import WLAN, AP_IF
from machine import Pin, SPI
from ili9341 import ILI9341
from graphic_terminal import GraphicTerminal
from mqtt_client import MQTTClient

tasks = Tasks()
ap = WLAN(AP_IF)
ap.active(False)
ap = None
spi = SPI(sck = Pin(18), mosi = Pin(23), miso = Pin(19))
tft = ILI9341(spi)
tft.set_background(tft.to_color(0, 0, 0))
left_button = Pin(39, Pin.IN, Pin.PULL_UP)
terminal = GraphicTerminal(tasks, tft)
terminal.start()
visibility = 0

def is_hidden():
    return visibility == 0

def is_button_touched():
    return left_button.value() == 0

def is_button_released():
    return left_button.value() == 1

def on_button_touched():
    if is_hidden():
        show()
    tasks.when_for_then(is_button_released, 300, on_button_released)
    
def on_button_released():
    tasks.when_then(is_button_touched, on_button_touched)

def hide():
    global visibility
    visibility -= 1
    if visibility == 0:
        tft.off()
        
def show():
    global visibility
    visibility += 1
    if visibility == 1:
        tft.on()
    tasks.after(300000, hide)
    
def on_receive(topic_name, payload):
    try:
        terminal.print(payload[':message'])
        show()
        return
    except:
        pass
    try:
        terminal.execute_logo(payload[':logo'])
        show()
    except:
        pass

client = MQTTClient(tasks,
                    hostname = 'broker.mqttdashboard.com',
                    client_id = '',
                    user_name = 'my-username',
                    password = '') 
client.activate_wlan([('...', '...')])
client.subscribe('my-topic', on_receive)
client.start() 
terminal.execute_logo('pendown; repeat 4 [forward 8; right 90]')
on_button_touched()

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

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