How to use the command mosquitto_sub (with examples)
The mosquitto_sub
command is a simple MQTT version 3.1.1 client that allows users to subscribe to topics and print the messages they receive. It is commonly used in the context of MQTT (Message Queue Telemetry Transport) protocol, which is a lightweight messaging protocol for IoT devices.
Use case 1: Subscribe to the topic with QoS set to 1
Code:
mosquitto_sub -t sensors/temperature -q 1
Motivation:
By using this command, the user can subscribe to the topic sensors/temperature
and receive temperature sensor readings from connected devices. Setting the Quality of Service (QoS
) to 1 ensures that each message is delivered at least once.
Explanation:
mosquitto_sub
: The name of the command being used.-t sensors/temperature
: Specifies the topic to subscribe to (in this case,sensors/temperature
).-q 1
: Sets the Quality of Service to 1, ensuring that each message is delivered at least once.
Example Output:
25.6
26.3
24.9
Use case 2: Subscribe to all broker status messages with verbose output
Code:
mosquitto_sub -v -h "iot.eclipse.org" -p 1885 -t \$SYS/#
Motivation:
By using this command, the user can subscribe to all broker status messages published on the broker at iot.eclipse.org
with port 1885. The verbose option (-v
) allows the user to see detailed output, including the topic, QoS, and retained flag.
Explanation:
mosquitto_sub
: The name of the command being used.-v
: Enables verbose output, providing more detailed information about each message received.-h "iot.eclipse.org"
: Specifies the hostname of the broker to connect to (in this case,iot.eclipse.org
).-p 1885
: Specifies the port to connect to (in this case, port 1885).-t \$SYS/#
: Subscribes to all topics under the$SYS
namespace, which typically contains broker status messages. The\
is used to escape the$
symbol.
Example Output:
\$SYS/broker/uptime 1609472139
\$SYS/broker/version 3.1.1
\$SYS/broker/clients/total 52
Use case 3: Subscribe to multiple topics matching a pattern
Code:
mosquitto_sub -t sensors/machines/+/temperature/+
Motivation:
By using this command, the user can subscribe to multiple topics that match a given pattern. In this case, the pattern is sensors/machines/+/temperature/+
, where the +
symbol is a wildcard that matches any metric name. This allows the user to receive temperature readings from multiple machines.
Explanation:
mosquitto_sub
: The name of the command being used.-t sensors/machines/+/temperature/+
: Specifies the topic to subscribe to, using wildcards (+
) to match any valid metric name. For example, it will match topics likesensors/machines/1/temperature/1
andsensors/machines/2/temperature/2
.
Example Output:
sensors/machines/1/temperature/1 25.6
sensors/machines/1/temperature/2 26.3
sensors/machines/2/temperature/1 24.9
sensors/machines/2/temperature/2 22.8
Conclusion:
The mosquitto_sub
command is a useful tool for subscribing to MQTT topics and receiving messages. Its flexibility allows users to subscribe to specific topics, receive verbose output, and subscribe to multiple topics matching a pattern. This command greatly enhances the ability to receive real-time data from IoT devices and is a valuable tool in MQTT-based applications.