MQTT:Python 脚本应订阅子主题
MQTT: Python script should subscribe to subtopics
我们正在开展一个项目,其中一些传感器将数据发送到 mqtt 代理,我们编写了一个 python 脚本来获取这些数据并将其存储在 csv 文件中。
现在,当我们添加更多传感器时,我们的主题会像这样变化:
topic/sensor1
topic/sensor2
等等。现在我们想让这个脚本中的子主题自动化,而不是在添加或删除传感器时对其进行硬编码。
您有什么建议,我们如何循环订阅所有子主题?
到目前为止我们有以下内容:
import paho.mqtt.client as mqtt
import logging
from datetime import datetime
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid))
def on_log(client, userdata, level, buf):
print(buf)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("envdata/#")
def on_message(client, userdata, msg):
f = open("log.csv", "a")
msg_decoded = str(msg.payload, 'utf-8')
msg_decoded = msg_decoded.replace("\n","")
msg_decoded = msg_decoded + "\ttime:" + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\n"
f.write(msg_decoded)
f.close()
print(msg.topic+" "+msg_decoded)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_log = on_log
client.on_subscribe = on_subscribe
client.username_pw_set(user, password=password)
client.connect(url, 1883, 60)
client.loop_forever()
我们想通了。因此,当我们使用 client.subscribe("topic/#")
订阅每个子主题时,我们可以使用 msg.topic
.
访问 on_message
回调中的所有主题
现在我们可以将这个主题存储在一个代表 .csv
文件的字符串中。
所以我们的 on_message
回调现在看起来像下面这样:
def on_message(client, userdata, msg):
filename = msg.topic
filename = filename.replace("/","-")
f = open(filename + ".csv", "a")
msg_decoded = str(msg.payload, 'utf-8')
msg_decoded = msg_decoded.replace("\n","")
msg_decoded = msg_decoded + "\ttime:" + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\n"
f.write(msg_decoded)
f.close()
print(msg.topic+" "+msg_decoded)
这对于我们想要通过此脚本达到的目的来说效果很好。现在,当脚本识别出新消息时,它会打开或创建一个类似 topic-subtopic.csv
的文件,并用预期的数据填充它。
我们正在开展一个项目,其中一些传感器将数据发送到 mqtt 代理,我们编写了一个 python 脚本来获取这些数据并将其存储在 csv 文件中。
现在,当我们添加更多传感器时,我们的主题会像这样变化:
topic/sensor1
topic/sensor2
等等。现在我们想让这个脚本中的子主题自动化,而不是在添加或删除传感器时对其进行硬编码。
您有什么建议,我们如何循环订阅所有子主题?
到目前为止我们有以下内容:
import paho.mqtt.client as mqtt
import logging
from datetime import datetime
def on_subscribe(mosq, obj, mid, granted_qos):
print("Subscribed: " + str(mid))
def on_log(client, userdata, level, buf):
print(buf)
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe("envdata/#")
def on_message(client, userdata, msg):
f = open("log.csv", "a")
msg_decoded = str(msg.payload, 'utf-8')
msg_decoded = msg_decoded.replace("\n","")
msg_decoded = msg_decoded + "\ttime:" + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\n"
f.write(msg_decoded)
f.close()
print(msg.topic+" "+msg_decoded)
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.on_log = on_log
client.on_subscribe = on_subscribe
client.username_pw_set(user, password=password)
client.connect(url, 1883, 60)
client.loop_forever()
我们想通了。因此,当我们使用 client.subscribe("topic/#")
订阅每个子主题时,我们可以使用 msg.topic
.
访问 on_message
回调中的所有主题
现在我们可以将这个主题存储在一个代表 .csv
文件的字符串中。
所以我们的 on_message
回调现在看起来像下面这样:
def on_message(client, userdata, msg):
filename = msg.topic
filename = filename.replace("/","-")
f = open(filename + ".csv", "a")
msg_decoded = str(msg.payload, 'utf-8')
msg_decoded = msg_decoded.replace("\n","")
msg_decoded = msg_decoded + "\ttime:" + datetime.now().strftime("%d/%m/%Y %H:%M:%S") + "\n"
f.write(msg_decoded)
f.close()
print(msg.topic+" "+msg_decoded)
这对于我们想要通过此脚本达到的目的来说效果很好。现在,当脚本识别出新消息时,它会打开或创建一个类似 topic-subtopic.csv
的文件,并用预期的数据填充它。