How to use the command "mosquitto_pub" (with examples)

How to use the command "mosquitto_pub" (with examples)

The mosquitto_pub command is a simple MQTT version 3.1.1 client that allows users to publish a single message on a topic and exit. MQTT is a lightweight messaging protocol commonly used in IoT applications for efficient message transfer.

Use case 1: Publishing a temperature value on a specific topic and host

Code:

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

Motivation: The example command is used to publish a temperature value of 32 on the sensors/temperature topic. By specifying the host (-h), the command sends the message to the specified IP address, which is 192.168.1.1 in this case. The Quality of Service (QoS) is set to 1, meaning the message will be delivered at least once.

Example output:

Message published successfully.

Use case 2: Publishing timestamp and temperature data to a remote host with a non-standard port

Code:

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

Motivation: This use case shows how to publish timestamp and temperature data on the sensors/temperature topic. By specifying the port (-p), the command sends the message to the remote host using a non-standard port, which is 1885 in this example.

Example output:

Message published successfully.

Use case 3: Publishing light switch status and retaining the message on a remote host

Code:

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

Motivation: The -r flag in the example command is used to retain the message on the topic switches/kitchen_lights/status. This is useful when there may be a long interval between light switch events and new subscribers to the topic can receive the last recorded status immediately upon subscribing.

Example output:

Message published successfully.

Use case 4: Sending the contents of a file as a message

Code:

mosquitto_pub -t sensors/temperature -f data.txt

Motivation: This use case illustrates how to send the contents of a file (data.txt) as a message. The command reads the file and publishes it to the sensors/temperature topic. This is useful when you want to publish large or complex messages without manually constructing them as command arguments.

Example output:

Message published successfully.

Use case 5: Sending the contents of a file from stdin as a message

Code:

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

Motivation: In this example, the -s flag is used to read from stdin instead of a file. The command sends the entire input as a message to the sensors/temperature topic. This is useful when you want to use a pipeline or redirect input from another command.

Example output:

Message published successfully.

Use case 6: Reading newline delimited data from stdin and publishing it

Code:

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

Motivation: The -l flag in the example command is used to treat the input from stdin as newline delimited data. It then publishes each line as a separate message to the sensors/temperature topic. This is useful when you have multiple messages to publish within a single input.

Example output:

Message published successfully.

Conclusion:

The mosquitto_pub command provides a versatile and convenient way to publish MQTT messages on various topics and hosts. With its support for file input, stdin reading, and different QoS levels, it allows users to integrate MQTT message publishing into their IoT and messaging applications effectively.

Related Posts

How to use the command iwctl (with examples)

How to use the command iwctl (with examples)

iwctl is a command used to control the iwd network supplicant.

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

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

Vale is an extensible style checker that supports multiple markup formats, such as Markdown and AsciiDoc.

Read More
How to use the command 'apx subsystems' (with examples)

How to use the command 'apx subsystems' (with examples)

This command is used to manage subsystems in apx. Subsystems are container-like entities that can be created based on pre-existing stacks.

Read More