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:

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.

Published by Gnd_To_Vcc

Here to spread my knowledge . Knowledge should always be spread not stored.

7 thoughts on “Testing Mosquitto Broker and Client on Raspbbery Pi

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create your website with WordPress.com
Get started
%d bloggers like this: