How to use the command 'mosquitto_sub' (with examples)
The mosquitto_sub
command is a lightweight client utility that connects to a Message Queuing Telemetry Transport (MQTT) broker to subscribe to specific topics. It listens for messages that match these topics and prints them to the console. Designed for simple and efficient device communication, particularly in IoT (Internet of Things) environments, mosquitto_sub
makes it easy to tap into data streams produced by various sensors and devices.
Use case 1: Subscribe to sensors/temperature
with QoS 1
Code:
mosquitto_sub -t sensors/temperature -q 1
Motivation:
In IoT setups, temperature data from sensors can be critical for applications like climate control systems, server room monitoring, or agricultural environments where maintaining a certain temperature range is vital. Subscribing to the sensor data with a Quality of Service level of 1 ensures that the client receives the message at least once, providing a balance between message reliability and resource usage.
Explanation:
-t sensors/temperature
: Specifies the topic to subscribe to. Here, it targets a temperature sensor data stream.-q 1
: Sets the Quality of Service level. QoS 1 ensures that a message is delivered at least once, which offers reliability in communication without the overhead of QoS 2.
Example Output:
temperature: 24.5°C
temperature: 25.0°C
temperature: 24.7°C
Use case 2: Subscribe to broker status messages on a remote server
Code:
mosquitto_sub -v -h "iot.eclipse.org" -p 1885 -t \$SYS/#
Motivation:
Monitoring the broker status is essential for administrators to ensure a healthy MQTT environment. By subscribing to system topics (denoted with $SYS
), administrators can receive a plethora of information about the broker, such as its uptime, number of clients, and active topics. Using a remote host enables scalability and central monitoring for large deployments.
Explanation:
-v
: Enables verbose mode, which prefixes each message with its topic name. This helps distinguish between different types of status messages.-h "iot.eclipse.org"
: Specifies the hostname of the MQTT broker. Here, it points to a public broker provided by Eclipse.-p 1885
: Indicates the port to connect to. The default port is 1883, but in this example, 1885 is explicitly defined.-t \$SYS/#
: Subscribes to all system topics, using the “#” wildcard for a broad match. The backslash is used to escape the$
symbol, which otherwise has special meaning in shells.
Example Output:
$SYS/broker/uptime 12345 seconds
$SYS/broker/clients/connected 5
$SYS/broker/load/messages 20000
Use case 3: Subscribe to multiple topics using wildcard patterns
Code:
mosquitto_sub -t sensors/machines/+/temperature/+
Motivation:
In industrial environments, machines may be equipped with various sensors transmitting data to an MQTT broker. You might want to subscribe to temperature data across multiple machines or components without specifying each one individually. Wildcard patterns offer a powerful, flexible way to gather data streams covering a broad scope of topics.
Explanation:
-t sensors/machines/+/temperature/+
: Utilizes the+
wildcard, which matches any single level in a topic string. This pattern subscribes to any number of machines or temperature sensor types, thus accommodating dynamic setups or a growing array of sensors.
Example Output:
sensors/machines/01/temperature/internal 65.2°C
sensors/machines/02/temperature/external 70.4°C
sensors/machines/01/temperature/external 63.8°C
Conclusion
The mosquitto_sub
command proves to be a versatile tool for monitoring data in IoT applications, providing robust capabilities for data capture through topic subscriptions. Whether it’s machine temperature monitoring or gaining insights into a broker’s status, mosquitto_sub
facilitates efficient data retrieval in a streamlined manner. By understanding how to tailor subscriptions with various flags and arguments, users can optimize their use of MQTT for a wide range of applications.