mirror of
https://github.com/meshtastic/meshtastic.git
synced 2025-03-05 21:00:08 -08:00
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:
parent
f8c4943477
commit
5cc78e03bd
|
@ -7,15 +7,15 @@ sidebar_position: 2
|
||||||
|
|
||||||
### Sending/receiving messages on mosquitto server using python
|
### 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
|
```python
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
from random import randrange, uniform
|
from random import uniform
|
||||||
import time
|
import time
|
||||||
|
|
||||||
client = mqtt.Client("some_client_id")
|
client = mqtt.Client(mqtt.CallbackAPIVersion.VERSION2)
|
||||||
client.connect('localhost')
|
client.connect('localhost')
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
@ -36,11 +36,11 @@ def on_message(mosq, obj, msg):
|
||||||
print("%-20s %d %s" % (msg.topic, msg.qos, msg.payload))
|
print("%-20s %d %s" % (msg.topic, msg.qos, msg.payload))
|
||||||
mosq.publish('pong', 'ack', 0)
|
mosq.publish('pong', 'ack', 0)
|
||||||
|
|
||||||
def on_publish(mosq, obj, mid):
|
def on_publish(mosq, obj, mid, reason_codes, properties):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
client = paho.Client()
|
client = paho.Client(paho.CallbackAPIVersion.VERSION2)
|
||||||
client.on_message = on_message
|
client.on_message = on_message
|
||||||
client.on_publish = on_publish
|
client.on_publish = on_publish
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue