diff --git a/main.py b/main.py index 5b93909..25b4b1c 100644 --- a/main.py +++ b/main.py @@ -66,6 +66,26 @@ def mqtt_on_connect(client, userdata, flags, rc): client.subscribe("server-rack-led-1/r") client.subscribe("server-rack-led-1/g") client.subscribe("server-rack-led-1/b") + +def mqtt_on_message(client, userdata, msg): + print(f"Message received [{msg.topic}]: {msg.payload}") + if str(msg.topic) == "server-rack-led-1/power": + power = str(msg.payload) + + elif str(msg.topic) == "server-rack-led-1/r": + r = str(msg.payload) + + elif str(msg.topic) == "server-rack-led-1/g": + g = str(msg.payload) + + elif str(msg.topic) == "server-rack-led-1/b": + b = str(msg.payload) + + elif str(msg.topic) == "server-rack-led-1/mode": + if str(msg.payload) == "0": + mode = 0 + elif str(msg.payload) == "1": + mode = 1 if __name__ == '__main__': @@ -177,31 +197,11 @@ if __name__ == '__main__': debug_print("Connecting to MQTT Broker "+str(mqttBroker)) client.connect(mqttBroker) client.on_connect = mqtt_on_connect + client.on_message = mqtt_on_message client.publish("server-rack-led-1/power","True") client.publish("server-rack-led-1/mode","0") - def mqtt_on_message(client, userdata, msg): - print(f"Message received [{msg.topic}]: {msg.payload}") - if str(msg.topic) == "server-rack-led-1/power": - power = str(msg.payload) - - elif str(msg.topic) == "server-rack-led-1/r": - r = str(msg.payload) - - elif str(msg.topic) == "server-rack-led-1/g": - g = str(msg.payload) - - elif str(msg.topic) == "server-rack-led-1/b": - b = str(msg.payload) - - elif str(msg.topic) == "server-rack-led-1/mode": - if str(msg.payload) == "0": - mode = 0 - elif str(msg.payload) == "1": - mode = 1 - - client.on_message = mqtt_on_message threading.Thread(target=lambda: client.loop_forever()) try: