import json import neopixel from led import Led from machine import UART, Pin uart = UART(0, 9600) led = Led() while True: if uart.any(): received = uart.read() try: input = received.decode("utf-8") payload = json.loads(input) command: str = payload["command"] except: # Return command in case of decoding error # so the script does not break command: str = "error" if command == "on": R,G,B,brightness = payload["data"]["r"] , payload["data"]["g"], payload["data"]["b"], payload["data"]["brightness"] led.on(int(R), int(G), int(B), int(brightness)) status: str = led.status() uart.write(status.encode("utf-8")) elif command == "off": led.off() status: str = led.status() uart.write(status.encode("utf-8")) elif command == "status": status = led.status() encoded = status.encode("UTF-8") uart.write(encoded) elif command == "error": print(received)