MQTT Testing
The mosquitto broker package ships commandline tools (often as *-clients package) to send and receive MQTT messages. As an alternative have a look at hbmqtt_pub and hbmqtt_sub which are provided by HBMQTT. For sending test messages to a broker running on localhost check the example below:
mosquitto 代理程序包括命令行工具(通常作为 *-clients包)发送和接收 MQTT 消息。 另外看一下 HBMQTT 提供的 hbmqtt_pub 和 hbmqtt_sub 。 要将测试消息发送到本地运行的代理,请查看以下示例:
$ mosquitto_pub -h 127.0.0.1 -t home-assistant/switch/1/on -m "Switch is ON"
如果使用嵌入式MQTT代理,则命令看起来有所不同,因为您需要添加MQTT协议版本。
$ mosquitto_pub -V mqttv311 -t "hello" -m world
或者如果您使用 API 密码:
$ mosquitto_pub -V mqttv311 -u homeassistant -P <your api password> -t "hello" -m world
Another way to send MQTT messages by hand is to use the “Developer Tools” in the Frontend. Choose “Call Service” and then mqtt/mqtt_send under “Available Services”. Enter something similar to the example below into the “Service Data” field.手动发送 MQTT 消息的另一种方法是在前端使用“Developer Tools”。 选择““Call Service”,然后选择“Available Services”下的 mqtt/mqtt_send 。 在Service Data”字段中输入类似于以下示例的内容。
{
"topic":"home-assistant/switch/1/on",
"payload":"Switch is ON"
}
消息应出现在总线上:
... [homeassistant] Bus:Handling <Event MQTT_MESSAGE_RECEIVED[L]: topic=home-assistant/switch/1/on, qos=0, payload=Switch is ON>
For reading all messages sent on the topic home-assistant to a broker running on localhost:
要查看在本地主机上运行的代理发给 home-assistant 主题所有消息:
JSONPath queryJSON
somekey{ 'somekey': 100 }
somekey[0]{ 'somekey': [100] }
somekey[0].value{ 'somekey': [ { value: 100 } ] }
To use this, add the following key to your configuration.yaml:
# Example configuration.yaml entry
mqtt:
embedded:
# Your HBMQTT config here. Example at:
# [url]http://hbmqtt.readthedocs.org/en/latest/references/broker.html#broker-configuration[/url]
Run your own这是最私人的选择,但需要更多的工作。 有多个免费和开源的 brokers 可以从中选择:例如Mosquitto, EMQ, or Mosca.
# Example configuration.yaml entry
mqtt:
broker: 192.168.1.100