moved on_message def

Signed-off-by: Ebbe Baß <ebbe@ping-mee.de>
master
Ebbe Baß 2022-07-01 22:32:30 +02:00
parent 481b3c7194
commit 4fd8d0b037
1 changed files with 21 additions and 21 deletions

42
main.py
View File

@ -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: