From cb97c6c6422809b5b3ba1ef122639852f04fd6e8 Mon Sep 17 00:00:00 2001 From: flaisch <simon.flaisch@student.reutlingen-university.de> Date: Tue, 2 Apr 2024 16:59:06 +0200 Subject: [PATCH] Adding a script for a testing consumer of the mqtt messagebus. Ideal for debugging :) --- Consumer.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 Consumer.py diff --git a/Consumer.py b/Consumer.py new file mode 100644 index 0000000..19c157b --- /dev/null +++ b/Consumer.py @@ -0,0 +1,25 @@ +import keyboard +import paho.mqtt.client as mqtt + +# The callback for when the client receives a CONNACK response from the server. +def on_connect(client, userdata, flags, reason_code, properties): + print(f"Connected with result code {reason_code}") + client.subscribe("Topic1") + +# The callback for when a PUBLISH message is received from the server. +def on_message(client, userdata, msg): + print(msg.topic+" "+str(msg.payload)) + +mqttc = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2, userdata=None) +mqttc.on_connect = on_connect +mqttc.on_message = on_message +mqttc.username_pw_set(username="standardUser", password="GreatHHZ4Ever!") +mqttc.connect("localhost", 1883) +while True: + mqttc.loop_start() + if keyboard.read_key == "space": + mqttc.disconnect() + mqttc.loop_stop() + print("goodbye!") + break + mqttc.loop_stop() -- GitLab