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
This commit is contained in:
Alice 2024-03-11 19:17:23 +01:00 committed by GitHub
parent f8c4943477
commit 5cc78e03bd
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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