一台笔记本电脑使用Ubuntu 18.04 LTS,另一台笔记本电脑使用Ubuntu 16.04 LTS。 Python 2。 用pip安装了paho.mqtt。
我正在尝试通过MQTT将变量发送到服务器。我有自己的代码,以前可以运行,但现在不能运行。
现在,我尝试了以下示例代码 https://github.com/pradeesi/Paho-MQTT-with-Python
我没有收到任何错误消息,并且服务器端也没有收到消息。
这是代码: mqtt_subscribe.py
# Import package
import paho.mqtt.client as mqtt
# Define Variables
#MQTT_HOST = "iot.eclipse.org"
MQTT_HOST = "192.168.1.127"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "helloTopic"
MQTT_MSG = "hello MQTT"
# Define on connect event function
# We shall subscribe to our Topic in this function
def on_connect(mosq, obj, rc):
mqttc.subscribe(MQTT_TOPIC, 0)
# Define on_message event function.
# This function will be invoked every time,
# a new message arrives for the subscribed topic
def on_message(mosq, obj, msg):
print "Topic: " + str(msg.topic)
print "QoS: " + str(msg.qos)
print "Payload: " + str(msg.payload)
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed to Topic: " +
MQTT_MSG + " with QoS: " + str(granted_qos))
# Initiate MQTT Client
mqttc = mqtt.Client()
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_subscribe = on_subscribe
# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
# Continue monitoring the incoming messages for subscribed topic
mqttc.loop_forever()
和mqtt_publish.py
# Import package
import paho.mqtt.client as mqtt
# Define Variables
#MQTT_HOST = "iot.eclipse.org"
MQTT_HOST = "192.168.1.127"
MQTT_PORT = 1883
MQTT_KEEPALIVE_INTERVAL = 45
MQTT_TOPIC = "helloTopic"
MQTT_MSG = "hello MQTT"
# Define on_publish event function
def on_publish(client, userdata, mid):
print "Message Published..."
# Initiate MQTT Client
mqttc = mqtt.Client()
# Register publish callback function
mqttc.on_publish = on_publish
# Connect with MQTT Broker
mqttc.connect(MQTT_HOST, MQTT_PORT, MQTT_KEEPALIVE_INTERVAL)
# Publish message to MQTT Broker
mqttc.publish(MQTT_TOPIC,MQTT_MSG)
# Disconnect from MQTT_Broker
mqttc.disconnect()
我认为笔记本电脑的某些网络设置有问题。谁能指出我如何调试该问题?这有什么问题吗?
在通过broker.mqttdashboard.com和本地(192.168.x.x)连接时尝试使用端口,而未提及端口。