将MQTT数据从订阅主题保存到文本文件中

时间:2019-01-21 14:35:11

标签: python mqtt paho

我正设置为从订阅的主题接收MQTT数据,并且我想将数据保存在文本文件中。

我添加了将变量保存到文本文件的代码。但是,这不起作用,因为它只给我变量而不是它的值,即不给我“ on_message”的值。有人可以帮帮我吗?

谢谢

我的代码如下:

import paho.mqtt.client as mqttClient
import time

def on_connect(client, userdata, flags, rc):

    if rc == 0:

        print("Connected to broker")

        global Connected                #Use global variable
        Connected = True                #Signal connection

    else:

        print("Connection failed")

def on_message(client, userdata, message):
    print "Message received: "  + message.payload

Connected = False   #global variable for the state of the connection

broker_address= "192.168.0.6"  #Broker address
port = 1883                         #Broker port
user = "me"                    #Connection username
password = "abcdef"            #Connection password

client = mqttClient.Client("Python")               #create new instance
client.username_pw_set(user, password=password)    #set username and password
client.on_connect= on_connect                      #attach function to callback
client.on_message= on_message                      #attach function to callback

f = open('/home/pi/test.txt','w')
f.write('on_message')
f.close()



client.connect(broker_address, port=port)          #connect to broker

client.loop_start()        #start the loop

while Connected != True:    #Wait for connection
    time.sleep(0.1)

client.subscribe("home/OpenMQTTGateway/433toMQTT")

try:
    while True:
        time.sleep(1)

except KeyboardInterrupt:
    print "exiting"
    client.disconnect()
    client.loop_stop()

我尝试了其他尝试,但是失败了。我是python的新手,仍然在学习。

1 个答案:

答案 0 :(得分:0)

您应该在on_message回调中将数据追加到文件中,并且显然应该先连接然后订阅主题

import paho.mqtt.client as mqttClient
import time

def on_connect(client, userdata, flags, rc):

    if rc == 0:

        print("Connected to broker")

        global Connected                #Use global variable
        Connected = True                #Signal connection

    else:

        print("Connection failed")

def on_message(client, userdata, message):
    print "Message received: "  + message.payload
    with open('/home/pi/test.txt','a+') as f:
         f.write("Message received: "  + message.payload + "\n")

Connected = False   #global variable for the state of the connection

broker_address= "192.168.0.6"  #Broker address
port = 1883                         #Broker port
user = "me"                    #Connection username
password = "abcdef"            #Connection password

client = mqttClient.Client("Python")               #create new instance
client.username_pw_set(user, password=password)    #set username and password
client.on_connect= on_connect                      #attach function to callback
client.on_message= on_message                      #attach function to callback
client.connect(broker_address,port,60) #connect
client.subscribe("some/topic") #subscribe
client.loop_forever() #then keep listening forever

现在,如果您在“某些/主题”上发布消息,则您的代码应将数据附加到文件中