Python MQTT 发布者不发布

Python MQTT publisher doesn't publish

正在尝试从 raspberry pi 发布一些数据,但没有成功。但是,通过 MQTT.fx 发布可以正常工作。这是我的 python 脚本:

import paho.mqtt.client as mqtt
import paho.mqtt.publish as publish

publish.single("example_topic", payload="example message", qos=0, retain=False,\
         hostname="example.com",port=8883, client_id="random_number", \
        keepalive=60, will=None,auth = {'username':"example_username",\
         'password':"example_password"}, tls=None, protocol=mqtt.MQTTv311, transport="tcp")

了解这一点可能很重要:在 MQTT.fx 中,如果不导航到 "SSL/TLS",我无法连接,在 "Enable SSL/TLS" 旁边打叉,然后在下一个打叉至 "CA signed server certificate".

有人可以告诉我我做错了什么吗? :)

你似乎把不同的东西混在一起了。 publish.single 不需要任何 mqtt.Client() 的东西。它旨在通过单个函数调用完成所有事情。

publish.single() 对所有内容都有选择。文档是 here

import paho.mqtt.publish as publish

publish.single(topic, payload=None, qos=0, retain=False, hostname="localhost",
        port=1883, client_id="", keepalive=60, will=None, 
        auth={username:"user", password:"pass"}, tls=None,
        protocol=mqtt.MQTTv311, transport="tcp")