From 90ceb142a8f5cc4ae8261b8cb5e7fccfde68183a Mon Sep 17 00:00:00 2001 From: Ollo Date: Sat, 7 Dec 2024 15:33:16 +0100 Subject: [PATCH] MQTT input prepared --- mqttclient/fanATserial2mqtt.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/mqttclient/fanATserial2mqtt.py b/mqttclient/fanATserial2mqtt.py index 6001f03..82f600e 100644 --- a/mqttclient/fanATserial2mqtt.py +++ b/mqttclient/fanATserial2mqtt.py @@ -13,6 +13,7 @@ mqtt_topic=os.environ['MQTT_TOPIC'] # SERIAL Port serial_device=os.environ['SERIAL_DEVICE'] + # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): print(msg.topic+" "+str(msg.payload)) @@ -21,7 +22,15 @@ def on_message(client, userdata, msg): # Start mqtt client_id = f'python-fan-ctrl' client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2) + + +def on_connect(mqttc, obj, flags, reason_code, properties): + client.subscribe(mqtt_topic + "cmd/led") + client.subscribe(mqtt_topic + "cmd/rpm") + print("Connected: " + str(reason_code)) + client.on_message = on_message +client.on_connect = on_connect if (not (mqtt_server)): print("MQTT_SERVER is not set") @@ -100,6 +109,8 @@ while True: time.sleep(0.2) except (KeyboardInterrupt, SystemExit): - cleanAndExit() + client.publish(mqtt_topic + "debug", "User aborted service") + time.sleep(0.5) + print("User aborted service") client.loop_stop()