Random Nerd Tutorials
Shares

Testing Mosquitto Broker and Client on Raspbbery Pi

Shares

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.

Recommended Resources

Home Automation using ESP8266 »
Build IoT projects and home automation gadgets with the ESP8266 Wi-Fi module.

Build a Home Automation System »
Learn how to build a automation system using open-source hardware and software from scratch.

Arduino Step-by-Step Projects »
Build 25 cool Arduino projects with our couse even with no prior experience!

Leave a Comment:

⏰ Early Black Friday 2017 Special ⏰
Download all our courses up to 35% off