From 5cc78e03bd3b7771e4374ea6eb615a1b58a6897c Mon Sep 17 00:00:00 2001 From: Alice <84906+wheresalice@users.noreply.github.com> Date: Mon, 11 Mar 2024 19:17:23 +0100 Subject: [PATCH] Correct Python MQTT code paho-mqtt version 2.0 is quite different to the old version 1, so the code needs updating to support the new version --- docs/software/integrations/mqtt/python.mdx | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/docs/software/integrations/mqtt/python.mdx b/docs/software/integrations/mqtt/python.mdx index 6f14c1a7..74af6002 100644 --- a/docs/software/integrations/mqtt/python.mdx +++ b/docs/software/integrations/mqtt/python.mdx @@ -7,15 +7,15 @@ sidebar_position: 2 ### Sending/receiving messages on mosquitto server using python -Here is an example publish message in python: +Here is an example publish message in python (run `pip install paho-mqtt` first): ```python #!/usr/bin/env python3 import paho.mqtt.client as mqtt -from random import randrange, uniform +from random import uniform import time -client = mqtt.Client("some_client_id") +client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2) client.connect('localhost') while True: @@ -36,11 +36,11 @@ def on_message(mosq, obj, msg): print("%-20s %d %s" % (msg.topic, msg.qos, msg.payload)) mosq.publish('pong', 'ack', 0) -def on_publish(mosq, obj, mid): +def on_publish(mosq, obj, mid, reason_codes, properties): pass if __name__ == '__main__': - client = paho.Client() + client = paho.Client(paho.CallbackAPIVersion.VERSION2) client.on_message = on_message client.on_publish = on_publish