added mqtt on message if requests for each topic
Signed-off-by: Ebbe Baß <ebbe@ping-mee.de>master
parent
3966b98201
commit
aa27df6f57
50
main.py
50
main.py
|
@ -61,14 +61,11 @@ def setColor(strip, color, wait_ms=10):
|
||||||
|
|
||||||
def mqtt_on_connect(client, userdata, flags, rc):
|
def mqtt_on_connect(client, userdata, flags, rc):
|
||||||
#sub to status
|
#sub to status
|
||||||
client.subscribe("server-rack-led-1/power/status")
|
client.subscribe("server-rack-led-1/power")
|
||||||
client.subscribe("server-rack-led-1/mode/status")
|
client.subscribe("server-rack-led-1/mode")
|
||||||
client.subscribe("server-rack-led-1/r/status")
|
client.subscribe("server-rack-led-1/r")
|
||||||
client.subscribe("server-rack-led-1/g/status")
|
client.subscribe("server-rack-led-1/g")
|
||||||
client.subscribe("server-rack-led-1/b/status")
|
client.subscribe("server-rack-led-1/b")
|
||||||
|
|
||||||
def mqtt_on_message(client, userdata, msg):
|
|
||||||
print(f"Message received [{msg.topic}]: {msg.payload}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
@ -174,31 +171,52 @@ if __name__ == '__main__':
|
||||||
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80)).start()
|
threading.Thread(target=lambda: app.run(host='0.0.0.0', port=80)).start()
|
||||||
|
|
||||||
mqttBroker ="homeassistant.ping-mee.local"
|
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!")
|
client.username_pw_set("mqtt", "pmMQTT_11!")
|
||||||
debug_print("Connecting to MQTT Broker "+str(mqttBroker))
|
debug_print("Connecting to MQTT Broker "+str(mqttBroker))
|
||||||
client.connect(mqttBroker)
|
client.connect(mqttBroker)
|
||||||
client.on_connect = mqtt_on_connect
|
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
|
client.on_message = mqtt_on_message
|
||||||
threading.Thread(target=lambda: client.loop_forever())
|
threading.Thread(target=lambda: client.loop_forever())
|
||||||
|
|
||||||
try:
|
try:
|
||||||
while True:
|
while True:
|
||||||
if power == "True":
|
if power == "True":
|
||||||
client.publish("server-rack-led-1/power/status","True")
|
client.publish("server-rack-led-1/power","True")
|
||||||
|
|
||||||
if mode == 0:
|
if mode == 0:
|
||||||
client.publish("server-rack-led-1/mode/status","0")
|
client.publish("server-rack-led-1/mode","0")
|
||||||
rainbowCycle(strip)
|
rainbowCycle(strip)
|
||||||
elif mode == 1:
|
elif mode == 1:
|
||||||
client.publish("server-rack-led-1/mode/status","1")
|
client.publish("server-rack-led-1/mode","1")
|
||||||
client.publish("server-rack-led-1/r/status",str(r))
|
client.publish("server-rack-led-1/r",str(r))
|
||||||
client.publish("server-rack-led-1/g/status",str(g))
|
client.publish("server-rack-led-1/g",str(g))
|
||||||
client.publish("server-rack-led-1/b/status",str(b))
|
client.publish("server-rack-led-1/b",str(b))
|
||||||
setColor(strip, Color(int(r), int(g), int(b)))
|
setColor(strip, Color(int(r), int(g), int(b)))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
else:
|
else:
|
||||||
client.publish("server-rack-led-1/power/status","False")
|
client.publish("server-rack-led-1/power","False")
|
||||||
setColor(strip, Color(0,0,0))
|
setColor(strip, Color(0,0,0))
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
Loading…
Reference in New Issue