From 4fd8d0b0371902d6f915ebc57dc8b91f9e615200 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Fri, 1 Jul 2022 22:32:30 +0200 Subject: [PATCH] moved on_message def MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- main.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) 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: