无法 post 连续向 mosquitto 代理发送数据

unable to post data to mosquitto broker continuously

我正在尝试使用 MQTT 将数据从 raspberry pi 连续发送到 windows 电脑, 我正在尝试向mosquitto发送5个数据,但是mosquitto似乎只得到一个值

编码 raspberry pi

import paho.mqtt.client as mqtt
client=mqtt.Client()
client.connect("192.168.0.104",1883,60)
for i in range(0,5):
    data={"protocol":"mqtt"}
    client.publish("/test",str(data))

代理接收数据的编码是

import paho.mqtt.client as mqtt

print("attempting to connect...")


def on_connect(client, userdata, flags, rc):
    if(rc==0):
        print("connection successful broker linked")
    elif(rc==1):
        print("connection refused -  incorrect protocol version")
    elif(rc==2):
        print("connection refused - invalid client identifier")
    elif(rc==3):
        print("connection refused- server unavailable")
    elif(rc==4):
        print("connection refused- bad username or password")
    elif(rc==5):
        print("connection refused- not authorised")
    else:
        print("currently unused")

    client.subscribe("s/test")

def on_message(client, userdata, msg):
    data=eval(msg.payload)
    print(data)

client = mqtt.Client()
client.connect("localhost",1883,60)

client.on_connect = on_connect
client.on_message = on_message
client.loop_forever()

您是否考虑过按照我在此处发布的答案进行操作?

https://github.com/eclipse/mosquitto/issues/972

您需要确保网络循环为发布客户端和订阅者运行。网络循环实际上处理发送消息。

以下是对您的代码最简单的修改。

import paho.mqtt.client as mqtt
client=mqtt.Client()
client.connect("192.168.0.104",1883,60)
for i in range(0,5):
    data={"protocol":"mqtt"}
    client.publish("/test",str(data))
    client.loop()