top of page

How to install Mosquitto MQTT Broker on Raspberry pi

Urban Skate Park

What is MQTT?

MQTT stands for Message Queue Telemetry Transport which is designed by IBM. This protocol is simple and lightweight which is used to send and receive messages over internet and it is designed for the devices which have low bandwidth uses. Nowadays this protocol is frequently used in the IoT devices to send and receive the sensors data. Also, In IoT based home automation systems, this protocol can easily be used without using much of internet data.

There are few terms which are used frequently in MQTT:

  1. Subscribe and Publish
  2. Message
  3. Topic
  4. Broker

1. Subscribe and Publish: Subscribe means to get the data from other device, and publish means to send the data to other device.

When device1 sends the data to device2 then it is known as Publisher and another is Subscriber and vice-versa.


2. Message: Messages are the information that we are sending and receiving. It can be a data or any type of command. For example, if we are publishing the temperature data to the cloud then this temperature data is known as Message.


3. Topic: This is the way you register interest for incoming messages or how you specify where you want to publish the message. Topics are represented with strings separated by a forward slash. Data is published on the topics using MQTT and then MQTT device subscribe to the topic to get the data.


4. MQTT Broker: This thing is responsible for receiving all messages from publishers, filters the messages and then publishing the messages to the subscribers who are interested in them.


When this broker is hosted on the cloud then it is called MQTT cloud. There are many cloud based MQTT services like Adafruit IO, MQTT.IO, IBM bluemix, Microsoft Azure, etc. MQTT can also be used with popular Amazon AWS cloud.



Installing Mosquitto MQTT Broker on Raspberry Pi

Open the terminal in your Raspberry pi and type the following commands to install the broker

sudo apt update
sudo apt install -y mosquitto mosquitto-clients

Wait for the installation to finish. Now, to start the broker on startup of raspberry pi, Type the following command

sudo systemctl enable mosquitto.service

That’s it, we are all set to launch our MQTT broker. To check it is installed properly, enter the following command

mosquitto -v

This command will give you the version of your MQTT broker. It should be 1.4.x or above.

Testing the Raspberry Pi Mosquitto Broker

1. Run the Mosquitto  broker in background using the below command

mosquitto -d

2. Now, we will subscribe a topic in exampleTopic using following command

mosquitto_sub -d -t exampleTopic

3. Now, we will publish some message to exampleTopic

mosquitto_pub -d -t exampleTopic -m "Hello world!"

You will receive the Hello world! Message in the subscriber terminal.

bottom of page