将订阅主题中的 MQTT 数据保存在文本文件中 [英] Saving MQTT data from subscribe topic on a text file
问题描述
我正在设置从订阅的主题接收 MQTT 数据,我想将数据保存在文本文件中.
I am setting up to receive MQTT data from a subscribed topic and I want to save the data in a text file.
我添加了将变量保存到文本文件的代码.然而,这不起作用,因为它只给我变量而不是它的值,即不给我on_message"的值.有人可以帮帮我吗?
I have added the code to save the variable to a text file. However this doesn't work as it gives me just the variable and not the value of it i.e. doesn't give me the values of "on_message". Can someone please help me?
谢谢
我的代码如下:
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 还很陌生,还在学习中.
I have tried other attempts but have failed. I am fairly new to python and still learning.
推荐答案
您应该在 on_message 回调中将数据附加到文件中并且显然应该连接然后订阅主题
You should append data to file in on_message call back and obviously should connect then subscribe to topic
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
现在,如果您在某个/主题"上发布消息,您的代码应将数据附加到文件中
Now if you publish message on "some/topic" your code shall append data to the file
这篇关于将订阅主题中的 MQTT 数据保存在文本文件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!