MQTT input prepared

This commit is contained in:
Ollo 2024-12-07 15:33:16 +01:00
parent a58b526670
commit 90ceb142a8

View File

@ -13,6 +13,7 @@ mqtt_topic=os.environ['MQTT_TOPIC']
# SERIAL Port # SERIAL Port
serial_device=os.environ['SERIAL_DEVICE'] serial_device=os.environ['SERIAL_DEVICE']
# The callback for when a PUBLISH message is received from the server. # The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg): def on_message(client, userdata, msg):
print(msg.topic+" "+str(msg.payload)) print(msg.topic+" "+str(msg.payload))
@ -21,7 +22,15 @@ def on_message(client, userdata, msg):
# Start mqtt # Start mqtt
client_id = f'python-fan-ctrl' client_id = f'python-fan-ctrl'
client = mqtt_client.Client(mqtt_client.CallbackAPIVersion.VERSION2) 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_message = on_message
client.on_connect = on_connect
if (not (mqtt_server)): if (not (mqtt_server)):
print("MQTT_SERVER is not set") print("MQTT_SERVER is not set")
@ -100,6 +109,8 @@ while True:
time.sleep(0.2) time.sleep(0.2)
except (KeyboardInterrupt, SystemExit): except (KeyboardInterrupt, SystemExit):
cleanAndExit() client.publish(mqtt_topic + "debug", "User aborted service")
time.sleep(0.5)
print("User aborted service")
client.loop_stop() client.loop_stop()