📖 Scenario: You are working with MQTT, a messaging protocol used in IoT devices. You want to send and receive messages using the mosquitto_pub and mosquitto_sub commands on your local machine.This project will guide you through publishing a message to a topic and subscribing to that topic to receive the message.
🎯 Goal: Learn how to use the mosquitto_pub command to publish a message to a topic and the mosquitto_sub command to subscribe and receive messages from that topic.
📋 What You'll Learn
Have Mosquitto client tools installed (mosquitto_pub and mosquitto_sub)
Use the local MQTT broker at localhost
Publish a message to a topic named home/temperature
Subscribe to the topic home/temperature to receive messages
💡 Why This Matters
🌍 Real World
MQTT is widely used in IoT devices to send sensor data like temperature or humidity to a central server or cloud.
💼 Career
Knowing how to use mosquitto_pub and mosquitto_sub commands is essential for IoT developers and DevOps engineers working with MQTT-based systems.
Progress0 / 4 steps
1
Create the MQTT topic name
Create a variable called topic and set it to the string "home/temperature".
IOT Protocols
Hint
The topic is a string that identifies the message channel. Use quotes around the topic name.
2
Create the message to publish
Create a variable called message and set it to the string "22.5" representing the temperature value.
IOT Protocols
Hint
The message is the data you want to send. Use quotes around the number as a string.
3
Write the mosquitto_pub command
Create a variable called publish_command and set it to the string that runs mosquitto_pub to publish the message to the topic on the local broker. The command should be: mosquitto_pub -h localhost -t home/temperature -m 22.5.
IOT Protocols
Hint
Use an f-string to insert the topic and message variables into the command string.
4
Write the mosquitto_sub command and print both commands
Create a variable called subscribe_command and set it to the string that runs mosquitto_sub to subscribe to the topic on the local broker. The command should be: mosquitto_sub -h localhost -t home/temperature. Then print both publish_command and subscribe_command on separate lines.
IOT Protocols
Hint
Use an f-string for the subscribe_command. Use two print statements to show both commands.
Practice
(1/5)
1. What is the main purpose of the mosquitto_pub command in MQTT?
easy
A. To send messages to a specific MQTT topic
B. To listen for messages on a topic
C. To create a new MQTT broker
D. To delete MQTT topics
Solution
Step 1: Understand the role of mosquitto_pub
The mosquitto_pub command is used to publish or send messages to MQTT topics.
Step 2: Compare with other options
mosquitto_sub listens for messages, not mosquitto_pub. Creating or deleting topics is not done by this command.
Final Answer:
To send messages to a specific MQTT topic -> Option A
Quick Check:
mosquitto_pub = send messages [OK]
Hint: Remember: pub means publish (send) messages [OK]
Common Mistakes:
Confusing mosquitto_pub with mosquitto_sub
Thinking it manages topics instead of messages
Assuming it listens for messages
2. Which of the following is the correct syntax to subscribe to the topic home/temperature using mosquitto_sub?
easy
A. mosquitto_sub -m home/temperature
B. mosquitto_sub -t home/temperature
C. mosquitto_sub -p home/temperature
D. mosquitto_sub -s home/temperature
Solution
Step 1: Identify the correct option flag for topic
The -t flag is used to specify the topic in mosquitto_sub.
Step 2: Verify other flags
-p is for port, -m is for message in mosquitto_pub, and -s is not a valid flag here.
Final Answer:
mosquitto_sub -t home/temperature -> Option B
Quick Check:
-t means topic [OK]
Hint: Use -t to specify topic in mosquitto_sub [OK]
Common Mistakes:
Using -p instead of -t for topic
Confusing mosquitto_pub flags with mosquitto_sub
Using invalid flags like -s
3. What will be the output when running mosquitto_sub -t sensors/humidity in one terminal followed by mosquitto_pub -t sensors/humidity -m 45 in another terminal connected to the same broker?
medium
A. The subscriber terminal will display nothing
B. The publisher terminal will display '45'
C. The subscriber terminal will display '45'
D. Both terminals will display '45'
Solution
Step 1: Understand the publish-subscribe flow
mosquitto_pub sends the message '45' to topic sensors/humidity. The subscriber listens on the same topic.
Step 2: Identify where the message appears
The subscriber terminal receives and displays the message. The publisher terminal does not display the message.
Final Answer:
The subscriber terminal will display '45' -> Option C
Quick Check:
Publisher sends, subscriber receives message [OK]
Hint: Subscriber shows messages sent to its topic [OK]
Common Mistakes:
Expecting publisher to show message output
Thinking subscriber won't receive messages
Confusing topic names or broker connection
4. You run mosquitto_sub -t home/lights but do not see any messages even after publishing with mosquitto_pub -t home/lights -m ON. What is the most likely cause?
medium
A. The subscriber is not connected to the same MQTT broker as the publisher
B. The topic name is misspelled in the publisher command
C. The message payload is empty
D. The subscriber command is missing the -m flag
Solution
Step 1: Check connection to MQTT broker
Both publisher and subscriber must connect to the same broker to exchange messages.
Step 2: Evaluate other options
Topic names match, message payload is 'ON' (not empty), and subscriber does not use -m flag (which is for publishing).
Final Answer:
The subscriber is not connected to the same MQTT broker as the publisher -> Option A
Quick Check:
Same broker needed for pub-sub communication [OK]
Hint: Check broker connection if no messages received [OK]
Common Mistakes:
Assuming subscriber needs -m flag
Ignoring broker address mismatch
Overlooking topic name typos
5. You want to publish the message "Alert: Temperature high!" to the topic alerts/temperature using mosquitto_pub with QoS level 1 and retain the message. Which command is correct?
hard
A. mosquitto_pub -r -q 1 -m "Alert: Temperature high!" alerts/temperature
B. mosquitto_pub -m "Alert: Temperature high!" -t alerts/temperature -q 0 -r
C. mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 2
D. mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 1 -r
Solution
Step 1: Identify flags for QoS and retain
-q 1 sets QoS level 1, and -r sets the retain flag in mosquitto_pub.
Step 2: Check command syntax and order
mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 1 -r correctly uses -t for topic, -m for message, -q 1 for QoS, and -r for retain. Other options have wrong QoS, missing retain, or incorrect argument order.
Final Answer:
mosquitto_pub -t alerts/temperature -m "Alert: Temperature high!" -q 1 -r -> Option D
Quick Check:
Use -q 1 and -r to set QoS and retain [OK]
Hint: Use -q for QoS and -r to retain message [OK]
Common Mistakes:
Using wrong QoS level
Forgetting the -r flag for retain
Incorrect argument order causing errors
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.