I'm trying to create an mqtt connection using paho. After publishing, the rc
returns a status of success, however the message has not been delivered.
Here is the code:
import os
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
print("connected with rc: "+str(rc))
pass
def on_publish(client, data, result):
print("data published")
pass
client = mqtt.Client()
client.username_pw_set(username="test", password="test" )
client.on_connect = on_connect
client.on_publish = on_publish
client.connect("some-host", 1883)
topic = "/something"
value = "cool"
ret = client.publish(topic, value)
if ret.rc == 0:
return {
"statusCode": 200,
"body": "The payload was successfully published"
}
else:
return {
"statusCode": 500,
"body": "There was an error publishing this payload"
}
Does anyone know what's happening here? I saw this post but my issue wasn't resolved by adding client.tls_set()
.
Move the publish to the on_connect
callback. The code is trying to publish before the connection is finished and add it's a QoS 0 message it's just getting thrown away
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.