How to Use the Command 'mosquitto_pub' (with Examples)

How to Use the Command 'mosquitto_pub' (with Examples)

The mosquitto_pub command is a straightforward MQTT client tool that allows users to publish messages to specific topics and exit immediately after publishing. MQTT, short for Message Queuing Telemetry Transport, is a lightweight messaging protocol, typically used for the Internet of Things (IoT). The mosquitto_pub command is part of the Mosquitto project, which provides an MQTT broker and client libraries. Its functionality is vital for scenarios where devices need to communicate efficiently, such as when transmitting sensor data from remote devices.

Use Case 1: Publish a Temperature Value with QoS

Code:

mosquitto_pub -h 192.168.1.1 -t sensors/temperature -m 32 -q 1

Motivation:

In IoT applications, like home automation or industrial monitoring, it’s essential to relay sensory data accurately and reliably. This example demonstrates the process of sending a temperature reading from a device to a broker while ensuring the message’s importance by setting the Quality of Service (QoS) level.

Explanation:

  • -h 192.168.1.1: This specifies the broker’s hostname or IP address. The client connects to this address to publish the message.
  • -t sensors/temperature: The topic to which you’re publishing the message, sensors/temperature here represents a channel dedicated to temperature readings.
  • -m 32: This denotes the message being sent; in this case, the temperature value is 32.
  • -q 1: This sets the Quality of Service level to 1, ensuring at least one delivery of the message. Unlike QoS 0, which does not guarantee delivery, QoS 1 assures that the broker receives the message at least once.

Example Output:

A successful execution would not produce any standard output. Instead, a receiving subscriber on the sensors/temperature topic should receive the message “32” with QoS 1.

Use Case 2: Publish Timestamp and Temperature Data on a Non-standard Port

Code:

mosquitto_pub -h 192.168.1.1 -p 1885 -t sensors/temperature -m "1266193804 32"

Motivation:

Sometimes, a broker might use a non-standard port for MQTT connections, particularly in environments with customized network configurations or enhanced security measures. This example shows how to publish a message, including both a timestamp and a temperature reading, to a broker using such settings.

Explanation:

  • -h 192.168.1.1: This specifies the broker’s address as before.
  • -p 1885: This denotes the port number on which the broker is listening. By default, MQTT uses port 1883; however, here, a non-standard port (1885) is specified.
  • -t sensors/temperature: Refers to the topic sensors/temperature to which the message is published.
  • -m "1266193804 32": The message, a string combining a timestamp and a temperature value. The timestamp allows tracking when the reading was taken.

Example Output:

Again, there won’t be standard output, but subscribers to the sensors/temperature topic should receive “1266193804 32” on the specified port.

Use Case 3: Publish Light Switch Status with a Retained Message

Code:

mosquitto_pub -r -h "iot.eclipse.org" -t switches/kitchen_lights/status -m "on"

Motivation:

For events with long intervals, such as room light switches turning on or off, it’s crucial that the last known state is accessible to new subscribers to avoid unnecessary messages. The -r flag enables this functionality.

Explanation:

  • -r: Retain the message. It instructs the broker to store the last message published on a topic and send this stored message to any new subscribers.
  • -h "iot.eclipse.org": Specifies the broker’s hostname where the message is sent.
  • -t switches/kitchen_lights/status: The topic concerning the kitchen lights’ switch status.
  • -m "on": This is the message, indicating that the switch is currently “on.”

Example Output:

There will be no direct output from the command, but any new subscribers will immediately receive the retained message “on” upon subscribing to the switches/kitchen_lights/status topic.

Use Case 4: Send File Contents as a Message

Code:

mosquitto_pub -t sensors/temperature -f data.txt

Motivation:

In cases where data needs to be published in bulk or is pre-collected in files, sending the contents directly as a message can streamline operations, eliminating manual copy-pastes and ensuring accuracy.

Explanation:

  • -t sensors/temperature: The topic set for the message publication.
  • -f data.txt: Indicates reading the message content from data.txt. The file’s entire content becomes the message’s payload.

Example Output:

Subscribers to the sensors/temperature topic will receive the contents of data.txt as a single message.

Use Case 5: Send File via stdin

Code:

mosquitto_pub -t sensors/temperature -s < data.txt

Motivation:

This use case is beneficial when more control over standard input and process control is needed. Using stdin allows for integrating with other command-line operations, such as piping or redirection.

Explanation:

  • -t sensors/temperature: Indicates the destination topic.
  • -s: This reads the message from standard input (stdin), allowing dynamic or script-based input processing.
  • < data.txt: Redirects the file data.txt to standard input, enabling its contents to be used as a message.

Example Output:

There will be no direct output from this command, but subscribers will see the content of data.txt on the sensors/temperature topic.

Use Case 6: Publish Newline Delimited Data from stdin

Code:

echo data.txt | mosquitto_pub -t sensors/temperature -l

Motivation:

When publishing several records, possibly generated or processed in real-time by another program, the -l flag can be handy. It offers a straightforward method to publish each line as an individual message, suitable for log entries or batched sensor data.

Explanation:

  • -t sensors/temperature: Specifies the publishing topic.
  • -l: Reads from standard input and publishes each newline-delimited string as a separate message.
  • echo data.txt: Acts as input to mosquitto_pub, for example, simulating a log or a process output.

Example Output:

Each line within data.txt is published separately to sensors/temperature; output from other integrations may capture these events.

Conclusion:

The mosquitto_pub command is an incredibly useful tool for IoT applications and other systems relying on the MQTT protocol for message exchange. The examples above illustrate its versatility, from handling simple value publications to more complex operations such as file input handling and ensuring message retention across connections. Understanding these use cases can significantly aid in leveraging MQTT effectively in diverse technological ecosystems.

Related Posts

How to use the command 'gemtopnm' (with examples)

How to use the command 'gemtopnm' (with examples)

The gemtopnm command is part of the Netpbm toolkit, which offers a vast array of image processing utilities.

Read More
Exploring the 'compgen' Command in Bash (with examples)

Exploring the 'compgen' Command in Bash (with examples)

The compgen command in Bash is a built-in utility primarily used for autocomplete functionalities.

Read More
How to use the command 'crane version' (with examples)

How to use the command 'crane version' (with examples)

The crane version command is part of the crane toolset provided by Google’s Go Container Registry project.

Read More