import network
import ujson
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 = ujson.loads(msg)
    pixels.fill((msg[0], msg[1], msg[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", "broker.hivemq.com")
client.set_callback(mqtt_message)
client.connect()
client.subscribe("remoticon")
print("Connected!")

while True:
  client.wait_msg()