Skip to main content
 首页 » 编程设计

python之Raspberry-pi mqttc.publish() 到 node-red 在 20 个条目后溢出

2024年10月25日8mfryf

我正在使用我的 python 代码通过 mqttc 将消息发布到 node-red。问题是在 20 mqttc 发布之后,数据没有发布到 node-red。

这是我用于发布到 node-red 的 python 代码:

mqttc.connect("nodered&mqtt_server_ip",1883) 
i=1 
while True: 
 
    i=i+1 
    mqttc.publish("sensor",i,1) 
    time.sleep(1) 

我也试过:

 i=1 
 while True: 
 
    mqttc.connect("nodered&mqtt_server_ip",1883) 
    i=i+1 
    mqttc.publish("sensor",i,1) 
    mqttc.disconnect() 
    time.sleep(1) 

无论如何,代码将前 20 个值发布到 node-red,然后停止。

我的 Node-red 工作流程如下。

请您参考如下方法:

  mqttc.connect("nodered&mqtt_server_ip",1883) 
    i=1 
    while True: 
 
        i=i+1 
        mqttc.publish("sensor",i,0)//corrected code 
        time.sleep(1)