如何使用 mosquitto 测试 python paho mqtt?
How to test the python paho mqtt with mosquitto?
我想测试 mosquitto MQTT Python 客户端端口。
import json
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
"""Called when connected to MQTT broker."""
client.subscribe("hermes/intent/#")
client.subscribe("hermes/nlu/intentNotRecognized")
print("Connected. Waiting for intents.")
def on_disconnect(client, userdata, flags, rc):
"""Called when disconnected from MQTT broker."""
client.reconnect()
def on_message(client, userdata, msg):
"""Called each time a message is received on a subscribed topic."""
nlu_payload = json.loads(msg.payload)
if msg.topic == "hermes/nlu/intentNotRecognized":
sentence = "Unrecognized command!"
print("Recognition failure")
else:
# Intent
print("Got intent:", nlu_payload["intent"]["intentName"])
# Speak the text from the intent
sentence = nlu_payload["input"]
site_id = nlu_payload["siteId"]
client.publish("hermes/tts/say", json.dumps({"text": sentence, "siteId": site_id}))
# Create MQTT client and connect to broker
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.connect("localhost", 1883)
client.loop_forever()
我运行它用命令
$ python script.py`
Connected. Waiting for intents.
mosquitto 是否发送 POST 请求?还是我必须用 mosquitto 开始请求?我应该如何创建请求才能得到
Got intent: SetTimer
MQTT 不是 HTTP,POST 是一个 HTTP 动词,在 MQTT 上下文中没有任何意义。
MQTT 是一种 pub/sub 协议,而 HTTP 是一种 request/response 协议。
您发布的代码仅订阅了 2 个主题,它不会发布任何内容(直到收到消息)。因此,除非您有另一个应用程序将向 python 代码订阅的 2 个主题之一发布消息,否则它只会坐在那里等待消息。
如果需要,您可以使用 mosquitto 命令行工具发送消息。例如
mosquitto_pub -t hermes/intent/foo -m '{"intent": { "intentName": "SetTimer"}}'
我想测试 mosquitto MQTT Python 客户端端口。
import json
import paho.mqtt.client as mqtt
def on_connect(client, userdata, flags, rc):
"""Called when connected to MQTT broker."""
client.subscribe("hermes/intent/#")
client.subscribe("hermes/nlu/intentNotRecognized")
print("Connected. Waiting for intents.")
def on_disconnect(client, userdata, flags, rc):
"""Called when disconnected from MQTT broker."""
client.reconnect()
def on_message(client, userdata, msg):
"""Called each time a message is received on a subscribed topic."""
nlu_payload = json.loads(msg.payload)
if msg.topic == "hermes/nlu/intentNotRecognized":
sentence = "Unrecognized command!"
print("Recognition failure")
else:
# Intent
print("Got intent:", nlu_payload["intent"]["intentName"])
# Speak the text from the intent
sentence = nlu_payload["input"]
site_id = nlu_payload["siteId"]
client.publish("hermes/tts/say", json.dumps({"text": sentence, "siteId": site_id}))
# Create MQTT client and connect to broker
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message
client.connect("localhost", 1883)
client.loop_forever()
我运行它用命令
$ python script.py`
Connected. Waiting for intents.
mosquitto 是否发送 POST 请求?还是我必须用 mosquitto 开始请求?我应该如何创建请求才能得到
Got intent: SetTimer
MQTT 不是 HTTP,POST 是一个 HTTP 动词,在 MQTT 上下文中没有任何意义。
MQTT 是一种 pub/sub 协议,而 HTTP 是一种 request/response 协议。
您发布的代码仅订阅了 2 个主题,它不会发布任何内容(直到收到消息)。因此,除非您有另一个应用程序将向 python 代码订阅的 2 个主题之一发布消息,否则它只会坐在那里等待消息。
如果需要,您可以使用 mosquitto 命令行工具发送消息。例如
mosquitto_pub -t hermes/intent/foo -m '{"intent": { "intentName": "SetTimer"}}'