Publish and subscribe to MQTT messages on the command line
thin-edge.io cli provides a convenient way to debug and aid development process.
Publish​
Command tedge mqtt pub
can be used to publish MQTT messages on a topic to the local mosquitto server.
Example:
- tedge
- mosquitto
- mqtt
tedge mqtt pub 'te/device/main///m/env_sensor' '{
"temperature": 21.3
}'
mosquitto_pub -t 'te/device/main///m/env_sensor' -m '{
"temperature": 21.3
}'
te/device/main///m/env_sensor
{
"temperature": 21.3
}
Messages can also be published with a different Quality of Service (QoS).
- tedge
- mosquitto
- mqtt
tedge mqtt pub -q 2 'te/device/main///m/env_sensor' '{
"temperature": 21.3
}'
mosquitto_pub -q 2 -t 'te/device/main///m/env_sensor' -m '{
"temperature": 21.3
}'
te/device/main///m/env_sensor
{
"temperature": 21.3
}
MQTT messages can also be published using the retained option which means that the message will be received by new MQTT clients connecting to the broker after the message was published.
Below shows an example of publishing a retained MQTT message:
- tedge
- mosquitto
- mqtt
tedge mqtt pub -r -q 1 'te/device/main///a/high_temperature' '{
"text": "Temperature is critical",
"severity": "critical"
}'
mosquitto_pub -r -q 1 -t 'te/device/main///a/high_temperature' -m '{
"text": "Temperature is critical",
"severity": "critical"
}'
te/device/main///a/high_temperature
{
"text": "Temperature is critical",
"severity": "critical"
}
By default the mqtt message will be published with retain flag set to false.
Subscribe​
Command tedge mqtt sub
can be used to ease debugging of of MQTT communication on local bridge. You can subscribe to topic of your choosing:
- tedge
- mosquitto
- mqtt
tedge mqtt sub 'te/errors'
mosquitto_sub -t 'te/errors'
te/errors
Or you can subscribe to any topic on the server using wildcard (#
) topic:
- tedge
- mosquitto
- mqtt
tedge mqtt sub '#'
mosquitto_sub -t '#'
#
Now using a different console/shell, publish the following measurement so that the previous subscription will receive it:
- tedge
- mosquitto
- mqtt
tedge mqtt pub -r -q 1 'te/device/main///m/env_sensor' '{
"temperature": 21.3
}'
mosquitto_pub -r -q 1 -t 'te/device/main///m/env_sensor' -m '{
"temperature": 21.3
}'
te/device/main///m/env_sensor
{
"temperature": 21.3
}
All messages from sub command are printed to stdout
and can be captured to a file if you need to:
- tedge
- mosquitto
- mqtt
tedge mqtt sub '#' > filename.mqtt
mosquitto_sub -t '#' > filename.mqtt
#
Wildcard (#
) topic is used by MQTT protocol as a wildcard and will listen on all topics