Testing Mosquitto Broker and Client on Raspbbery Pi

In this post you’re going to test the Mosquitto Broker and Client on a Raspberry Pi by subscribing to an MQTT topic and publishing sample messages.

Recommended resources:

If you like home automation and you want to build a complete home automation system, I recommend downloading my home automation course.

Testing MQTT Broker Installation

After installing MQTT Broker, I recommend installing an MQTT Cllient to test the Broker installation and publish sample messages.

The next command shows how to install MQTT Mosquitto Client:

pi@raspberry:~ $ sudo apt-get install mosquitto-clients

You’ll have to type Y and press Enter to confirm the installation.

Run Mosquitto on background as a daemon:

pi@raspberry:~ $ mosquitto -d

Subscribing to testTopic Topic

To subscribe to an MQTT topic with Mosquitto Client open a terminal Window #1 and enter the command:

pi@raspberry:~ $ mosquitto_sub -d -t testTopic

You’re now subscribed to a topic called testTopic.

Publishing “Hello World!” Message to testTopic Topic

To publish a sample message to testTopic, open a terminal Window #2 and run this command:

pi@raspberry:~ $ mosquitto_pub -d -t testTopic -m "Hello world!"

The message “Hello World!” is received in Window #1 as illustrated in the figure above.

Publishing a Message to Multiple Clients

Having Window #1 still subscribed to topic testTopic, open a new terminal Window #3 and run this command to subscribe to testTopic topic:

pi@raspberry:~ $ mosquitto_sub -d -t testTopic

On Window #2 publish the “Hello World!” message:

pi@raspberry:~ $ mosquitto_pub -d -t testTopic -m "Hello world!"

Since two clients are subscribed to testTopic topic, they will both receive “Hello world!” message.

This simple example shows how MQTT works and how your devices (for example: ESP8266) could be subscribed to the same topic to receive messages or a device could publish messages to multiple devices. We’ll explore this concept further in future blog posts.

Wrapping up

In the next blog post, we will experiment with MQTT and ESP8266 to see how everything works with practical examples.

Like home automation? Learn more about Node-RED, Raspberry Pi, ESP8266 and Arduino with my course: Build a Home Automation System for $100.

Do you have any questions? Leave a comment down below!

Thanks for reading. If you like this post probably you might like my next ones, so please support me by subscribing my blog.

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Learn how to program and build projects with the ESP32 and ESP8266 using MicroPython firmware DOWNLOAD »

Enjoyed this project? Stay updated by subscribing our weekly newsletter!

3 thoughts on “Testing Mosquitto Broker and Client on Raspbbery Pi”

  1. Hi Rui, Odd things happening for me on this one. I am using a Rpi2b, Raspian Pixel, Mosquitto ver 1.4.14. When I type in the second terminal window “mosquitto_pub -d -t testTopic -m “Hello World” the following occurs:
    1 I receive an Error: Unknown option ‘world’
    2 I noticed when I typed the command again on the first depression of the quote (shift + ‘) the quote did not show. (it does show in text editor).
    AND if I type “mosquitto_pub -d -t testTopic -m Hello World (without the quotes) I get the same error, but if I type mosquitto_pub -d -t testTopic -m Hello, the message “Hello” is transmitted. Any thoughts on this?

  2. awesome tutorial, thanks. So many questions answered in my head, going through it and seeing it work really helps the understanding.


Leave a Comment

Download our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.