From aa27df6f57c579ff32158b1283a8b073f948ae71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ebbe=20Ba=C3=9F?= Date: Fri, 1 Jul 2022 22:26:11 +0200 Subject: [PATCH] added mqtt on message if requests for each topic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Ebbe Baß --- main.py | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/main.py b/main.py index f6b6bd0..8b91925 100644 --- a/main.py +++ b/main.py @@ -61,14 +61,11 @@ def setColor(strip, color, wait_ms=10): def mqtt_on_connect(client, userdata, flags, rc): #sub to status - client.subscribe("server-rack-led-1/power/status") - client.subscribe("server-rack-led-1/mode/status") - client.subscribe("server-rack-led-1/r/status") - client.subscribe("server-rack-led-1/g/status") - client.subscribe("server-rack-led-1/b/status") - -def mqtt_on_message(client, userdata, msg): - print(f"Message received [{msg.topic}]: {msg.payload}") + client.subscribe("server-rack-led-1/power") + client.subscribe("server-rack-led-1/mode") + client.subscribe("server-rack-led-1/r") + client.subscribe("server-rack-led-1/g") + client.subscribe("server-rack-led-1/b") if __name__ == '__main__': @@ -174,31 +171,52 @@ if __name__ == '__main__': threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80)).start() mqttBroker ="homeassistant.ping-mee.local" - client = mqtt.Client("server-rack-led-1") + client = mqtt.Client("server_rack_led_1") client.username_pw_set("mqtt", "pmMQTT_11!") debug_print("Connecting to MQTT Broker "+str(mqttBroker)) client.connect(mqttBroker) client.on_connect = mqtt_on_connect + + 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: while True: if power == "True": - client.publish("server-rack-led-1/power/status","True") + client.publish("server-rack-led-1/power","True") if mode == 0: - client.publish("server-rack-led-1/mode/status","0") + client.publish("server-rack-led-1/mode","0") rainbowCycle(strip) elif mode == 1: - client.publish("server-rack-led-1/mode/status","1") - client.publish("server-rack-led-1/r/status",str(r)) - client.publish("server-rack-led-1/g/status",str(g)) - client.publish("server-rack-led-1/b/status",str(b)) + client.publish("server-rack-led-1/mode","1") + client.publish("server-rack-led-1/r",str(r)) + client.publish("server-rack-led-1/g",str(g)) + client.publish("server-rack-led-1/b",str(b)) setColor(strip, Color(int(r), int(g), int(b))) time.sleep(1) else: - client.publish("server-rack-led-1/power/status","False") + client.publish("server-rack-led-1/power","False") setColor(strip, Color(0,0,0)) time.sleep(1) except KeyboardInterrupt: