[英]Why can't i receive messages in Paho MQTT?
So I have created this code to send and receive messages using Paho MQTT. 因此,我创建了此代码以使用Paho MQTT发送和接收消息。 It takes input from the user and publishes to a given topic and for taking the input I have created a while loop which will break if the input is 'exit'.
它从用户那里获取输入并发布到给定的主题,并且为了获取输入,我创建了一个while循环,如果输入为“ exit”,它将中断。
The problem is that I can send messages to the topic but I can't receive them. 问题是我可以向该主题发送消息,但无法接收。 I only receive them when I give 'exit' as input.
我仅在输入“退出”作为输入时才收到它们。
I tried reading the documentation but it was no help, plus I am new to python so maybe its something that I am missing out 我尝试阅读文档,但没有帮助,再加上我是python的新手,所以也许我错过了一些东西
import paho.mqtt.client as mqtt
import os
from urllib.parse import urlparse
# Define event callbacks
def on_connect(client, userdata, flags, rc):
print("rc: " + str(rc))
def on_message(client, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
def on_publish(client, obj, mid):
print("mid: " + str(mid))
def on_subscribe(client, obj, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(client, obj, level, string):
print(string)
mqttc = mqtt.Client(client_id="NV")
# Assign event callbacks
mqttc.on_message = on_message
mqttc.on_connect = on_connect
mqttc.on_publish = on_publish
mqttc.on_subscribe = on_subscribe
# Uncomment to enable debug messages
#mqttc.on_log = on_log
topic = 'test'
# Connect
mqttc.username_pw_set(username, pass)
mqttc.connect(server,port)
mqttc.loop_start()
# Start subscribe, with QoS level 0
mqttc.subscribe(topic, 0)
# Publish a specififc message
mqttc.publish(topic,'NV Online')
# INPUT from User
msg = 'run'
while(msg!='exit'):
msg = input()
mqttc.publish(topic,msg)
EDIT 1: Platforms: Eclipse Paho ver 1.4 OS: Windows 7 编辑1:平台:Eclipse Paho ver 1.4操作系统:Windows 7
Try changing your print
calls to include flush=True
. 尝试将
print
调用更改为包括flush=True
。
eg 例如
def on_message(client, obj, msg):
print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload), flush=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.