From 060e25961125b7783a08b5dd3437553a8838766f Mon Sep 17 00:00:00 2001 From: tony19990828 <58881058+tony19990828@users.noreply.github.com> Date: Thu, 17 Feb 2022 13:22:30 +0800 Subject: [PATCH] Create local_mqtt_pub_reciver.py --- local_mqtt_pub_reciver.py | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 local_mqtt_pub_reciver.py diff --git a/local_mqtt_pub_reciver.py b/local_mqtt_pub_reciver.py new file mode 100644 index 0000000..b5057b6 --- /dev/null +++ b/local_mqtt_pub_reciver.py @@ -0,0 +1,42 @@ +import ssl + +import paho.mqtt.client as mqtt + + +def on_connect(self, userdata, flags, rc): + print("Connected with result code " + str(rc)) + client.subscribe(topic) + + +def on_message(self, userdata, msg): + print(f"msg.topic {msg.payload.decode('utf-8')}") + + +def initialise_clients(cname): + # callback assignment + initialise_client = mqtt.Client(cname, True) # don't use clean session + return initialise_client + + +host = "192.168.50.180" +port = 1883 +topic = "mqtt/pub" + +client = initialise_clients("python_sub") + +client.on_connect = on_connect + +client.on_message = on_message + +client.connect(host, port, 60) + +client.loop_forever() + +# mqtt connect code list +# 0: Connection successful +# 1: Connection refused – incorrect protocol version +# 2: Connection refused – invalid client identifier +# 3: Connection refused – server unavailable +# 4: Connection refused – bad username or password +# 5: Connection refused – not authorised +# 6-255: Currently unused.