"""
MicroPython MQTT NeoPixels Example for Wokwi.com
To control the pixels:
1. Go to https://replit.com/@mopfeil/MQTTPublisher#main.py
2. Start the Repl
3. The broker sits at: https://mqtt.one/broker.html
Derived from: Copyright (C) 2022, Uri Shaked
https://wokwi.com/arduino/projects/315787266233467457
"""
import network
from neopixel import NeoPixel
from machine import Pin
from time import sleep
from umqtt.simple import MQTTClient
pixels = NeoPixel(Pin(13), 16)
pixels.fill((0, 0, 0))
pixels.write()
def mqtt_message(topic, msg):
print("Incoming message:", msg)
try:
msg_deco = list(msg)
pixels.fill((msg_deco[0], msg_deco[1], msg_deco[2]))
pixels.write()
except Exception as e:
print("Error:", e)
print("Connecting to WiFi...", end="")
import network
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect("Wokwi-GUEST", "")
while not wifi.isconnected():
sleep(0.5)
print(".", end="")
print("Done")
print("Connecting to MQTT...")
client = MQTTClient("wokwi1", server = b'b37.mqtt.one', port = b'1883', user = b'36eost4479', password = b'3780impruy')
client.set_callback(mqtt_message)
client.connect()
client.subscribe(b'36eost4479/wokwi1')
print("Connected!")
while True:
client.wait_msg()