0
我是mqtt和python系統的新手,我試圖在python中執行一些繁重的計算並將結果發送到node.js服務器。python和node.js之間的mqtt只發送一條消息
每次運行node.js腳本時,我只能得到一個結果。這裏是我的簡單 代碼:
simple_mqtt.py
message = "Hello"
x=0
def add():
global x
x = x + 1
def on_publish(client, userdata, mid):
print("mid: "+str(mid))
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("main/app")
def on_message(client, userdata, msg):
global message
message = msg.payload
client = paho.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_publish = on_publish
client.connect("192.168.2.4", 1883)
client.loop_start()
while True:
if message=="Start":
print message
add()
client.publish("main/app",x)
elif message=="Stop":
print message
client.publish("main/app","Ended")
time.sleep(1)
和Node.js的:
var mqtt = require('mqtt');
var client = mqtt.connect('mqtt://192.168.2.4:1883');
client.on('connect', function() {
client.subscribe('main/app');
client.publish('main/app', 'Start');
});
client.on('message', function (topic, message) {
// message is Buffer
console.log(message.toString());
});
我有什麼修改,以從Python獲得持續應答,直到我送消息「停止」?
謝謝!我爲發送值創建了不同的主題,現在我可以控制程序的流程。 – macvg