Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将MQTT主题和消息放入数组中_Python_Mqtt - Fatal编程技术网

Python 将MQTT主题和消息放入数组中

Python 将MQTT主题和消息放入数组中,python,mqtt,Python,Mqtt,我完全是python的新手,并且拥有非常基本的MQTT知识 我试图编写一个python脚本,订阅一个通配符主题,然后在通配符下构建一个主题列表。我知道MQTT协议不能满足这一要求,所以我需要通过python实现这一点。我正在考虑将主题和消息放在一个数组中 我有以下主题: /天气/电流/温度 /天气/电流/湿度 /天气/海流/气压 /天气/当前/时间 在我的python脚本中,我订阅了/weather/current/# 例如,我想象数组是这样的: [/weather/current/temper

我完全是python的新手,并且拥有非常基本的MQTT知识

我试图编写一个python脚本,订阅一个通配符主题,然后在通配符下构建一个主题列表。我知道MQTT协议不能满足这一要求,所以我需要通过python实现这一点。我正在考虑将主题和消息放在一个数组中

我有以下主题:

/天气/电流/温度

/天气/电流/湿度

/天气/海流/气压

/天气/当前/时间

在我的python脚本中,我订阅了/weather/current/#

例如,我想象数组是这样的:

[/weather/current/temperature,message]

[/天气/当前/湿度,信息]

[/weather/current/pressure,message]

[/weather/current/time,message]

我的脚本基本上是一个标准示例,在这个示例中,我尝试了几种方法来实现这一点,但都失败了。我认为我的根本问题是我对on_消息功能缺乏了解。它是针对所有主题执行一次还是针对每个主题执行一次

def on_connect(mqttc, obj, rc):
    print("rc: "+str(rc))

def on_message(mqttc, obj, msg,):
    # print(msg.topic+" "+str(msg.payload))
    payload = str(msg.payload)
    print(msg.topic+" Payload -> "+payload)

def on_publish(mqttc, obj, mid):
    print("mid: "+str(mid))

def on_subscribe(mqttc, obj, mid, granted_qos):
    print("Subscribed: "+str(mid)+" "+str(granted_qos))

def on_log(mqttc, obj, level, string):
    print(string)

try:
    mqttc = mqtt.Client("Python-MQTT-Sub")

    mqttc = mqtt.Client()
    mqttc.on_message = on_message
    mqttc.on_connect = on_connect
    mqttc.on_publish = on_publish
    mqttc.on_subscribe = on_subscribe
    # Uncomment to enable debug messages
    #mqttc.on_log = on_log
    mqttc.connect("localhost", 1883, 60)
    mqttc.subscribe("/weather/current/#", 0)

    mqttc.loop_forever()

except KeyboardInterrupt:
    print("\ninterrupt received, exiting...")

消息上的
将在收到来自代理的消息时被调用。这可以是您订阅的任何主题的消息,也可以是任何
/weather/current
及以上内容的消息。消息都是不同的事件,即使您只使用了一个订阅

另一个小问题-除非您使用
clean session
设置为false,否则硬编码客户端id通常不是一个好主意。重复的客户端ID会导致您与代理断开连接。您可以自行生成唯一的内容,或者在调用
client()
时将客户端id保留为缺失,然后将使用默认值
None
,这意味着客户端id是为您随机生成的


最后一件事——除非有其他理由,否则没有必要用斜杠开始主题。前导斜杠实际上添加了一个额外的层次结构级别,第一级为空字符串。这并不完全是您所期望的,因此在某些情况下可能会令人困惑。

on\u message
将在收到来自代理的消息时被调用。这可以是您订阅的任何主题的消息,也可以是任何
/weather/current
及以上内容的消息。消息都是不同的事件,即使您只使用了一个订阅

另一个小问题-除非您使用
clean session
设置为false,否则硬编码客户端id通常不是一个好主意。重复的客户端ID会导致您与代理断开连接。您可以自行生成唯一的内容,或者在调用
client()
时将客户端id保留为缺失,然后将使用默认值
None
,这意味着客户端id是为您随机生成的


最后一件事——除非有其他理由,否则没有必要用斜杠开始主题。前导斜杠实际上添加了一个额外的层次结构级别,第一级为空字符串。这并不完全是您所期望的,因此在某些情况下可能会令人困惑。

正如@ralight在上面所说的,在收到消息(可能是保留消息,也可能不是保留消息)时调用消息上的
。为了说明这一点,我稍微修改了您的代码,添加了一个名为
topic\u names
的数组,该数组在消息到达程序时填充

import paho.mqtt.client as mqtt 

topic_names = []

def on_message(mqttc, obj, msg,):
    # print(msg.topic + " " + str(msg.payload))
    payload = str(msg.payload)
    print(msg.topic + " Payload -> " + payload)

    topic_names.append(msg.topic)

try:
    mqttc = mqtt.Client()
    mqttc.on_message = on_message

    mqttc.connect("localhost", 1883, 60)
    mqttc.subscribe("weather/current/#", 0)

    mqttc.loop_forever()

except KeyboardInterrupt:
    print "Received topics:"
    for topic in topic_names:
        print topic
运行此程序并向其发布两条消息显示

weather/current/temp Payload -> Fair
weather/current/humidity Payload -> 10
^C
weather/current/temp
weather/current/humidity

正如上面@ralight所说,在接收到消息(可能是保留消息,也可能不是保留消息)时,会调用on_message
。为了说明这一点,我稍微修改了您的代码,添加了一个名为
topic\u names
的数组,该数组在消息到达程序时填充

import paho.mqtt.client as mqtt 

topic_names = []

def on_message(mqttc, obj, msg,):
    # print(msg.topic + " " + str(msg.payload))
    payload = str(msg.payload)
    print(msg.topic + " Payload -> " + payload)

    topic_names.append(msg.topic)

try:
    mqttc = mqtt.Client()
    mqttc.on_message = on_message

    mqttc.connect("localhost", 1883, 60)
    mqttc.subscribe("weather/current/#", 0)

    mqttc.loop_forever()

except KeyboardInterrupt:
    print "Received topics:"
    for topic in topic_names:
        print topic
运行此程序并向其发布两条消息显示

weather/current/temp Payload -> Fair
weather/current/humidity Payload -> 10
^C
weather/current/temp
weather/current/humidity

非常感谢你的指点。我使用庞特作为经纪人,我认为它需要领先的斜杠,我不是一个测试的家。我会测试并让你知道。(灯泡时刻!!!)你对客户id和on_消息的解释也会解释为什么我在arduino YUN上与mqtt较量。再次,非常感谢你的指点,非常合适。非常感谢你的指点。我使用庞特作为经纪人,我认为它需要领先的斜杠,我不是一个测试的家。我会测试并让你知道。(灯泡时刻!!!)你对客户id和on_消息的解释也会解释为什么我在arduino YUN上与mqtt较量。再次感谢你的指点,非常合适。