Introduction: Installing MQTT Broker(Mosquitto) on Raspberry Pi

Picture of Installing  MQTT Broker(Mosquitto) on Raspberry Pi

In this instructables you will find how to install MQTT on Raspberry Pi. There are two methods to install mqtt, one is we directly connect keyboard mouse and monitor to a Pi , turn it on and go to the terminal of the raspberry pi and install it from there. Another method is to access Pi through a computer using putty(SSH) and logging into the root and installing, we have an instructable which mentions how to install putty and access Pi through a computer ,you can get it here .In this instructables we are following the second method. Both mqtt client(Pub/Sub Client) and broker( mosquitto) needs to be installed on Pi.

Step 1: Installation of the MQTT Broker on Raspberry Pi

Picture of Installation of the MQTT Broker on Raspberry Pi

To use the new repository you should first import the repository package signing key follow the below comman, the wget command is used to download single file and stores in a current directory.


sudo apt-key add mosquitto-repo.gpg.key

Then make the repository available to apt

cd /etc/apt/sources.list.d/

Enter the following

for wheezy

sudo wget

for jessie

sudo wget

to install the mqtt mosquitto for the raspberry pi follow the below steps use sudo before the command if not using root

sudo -i

The above command is not mandatory ,it is if you wish to use root or you will need to prefix each below command with sudo eg sudo apt-get update

The below command is used to update the source list

apt-get update

After updating type the following commands to install mosquitto broker as shown in the image 1.

apt-get install mosquitto

the above command is to install mqtt mosquitto broker.

Step 2: Installation of the MQTT Client on Raspberry Pi

Picture of Installation of the MQTT Client on Raspberry Pi

After the installation of mqtt broker, install the mqtt client on raspberry pi.

The below shows the command to install mqtt mosquitto client this can be clearly seen in the image1 .

apt-get install mosquitto-clients

Step 3: Testing the Working of MQTT

Picture of Testing the Working of MQTT

After installing mqtt mosquitto broker and client the user need to test by creating the topic. Open two putty programs on windows, you can do this by clicking the putty.exe file and enter the host name and click to open do this twice so that two putty programs access Pi via ssh. Lets say putty1 and putty2 are open and connected to Pi via ssh for our convenient.

In putty1 window type the command with the topic name armtronix_mqtt this topic name can be anything, according to the convenience of the user .

mosquitto_sub –d –t armtronix_mqtt

As shown in the image 1 this will subscribe Pi to MQTT broker(which is runs on Pi) handling topic armtronix_mqtt.

In putty2 image 2 window type the following command with the same topic i.e armtronix_mqtt

mosquitto_pub –d –t armtronix_mqtt –m “Hello armtronix”

Now come to the window putty1, image 1 here you will be able to see the message

Hello armtronix

This concludes the that you are running the Mosquitto broker successfully.

Similarly here another case we have tested mosquitto_pub –d –t armtronix_mqtt –m “Test passed” will display Test passed as it is shown in image 1 and image 2.

Step 4: Testing MQTT From Another System

Picture of Testing MQTT From Another System

If you want to mqtt to connect through the network/internet from another system then you need to type the IP address of the host(Broker System, in our case its Raspberry Pi ) and you will be able to connect. In the image 3 it is shown that another computer from the LAN network has subscribed to the topic armtronix_mqtt in the window 1 using the command .Please note is the raspberry pi ip address and may be different in your case.So you will need to enter following command as shown in the image 1 which refers to window 1.

mosquitto_sub -h -t armtronix_mqtt

Now in the terminal window 2 we are publishing by entering the command as shown in the image 2

mosquitto_pub -h -t armtronix_mqtt -m "Hi this is Armtronix_server"

Now in the window 1 we will be able to see the message Hi this is Armtronix_server as shown in the image 3

We hope the we have made it as clear as possible for other to understand the process of installing and testing Mqtt on Raspberry Pi .Thank you

Creating some other instructable- Installation of openHAB on Raspberry Pi and setting it to control the lights, fan and camera etc which will be updated shortly.


Bonzadog (author)2017-09-15

Stretch has replaced Jessie.

Trying to install MQTT under stretch I get:

AFter running this:

mkdir mosquitto
cd mosquitto/
sudo apt-key add mosquitto-repo.gpg.key
cd /etc/apt/sources.list.d/
sudo wget
sudo apt-get update

Then I made the manual entry:

pi@mqtt:~/Install-Programmes $ sudo apt install -y mosquitto
Reading package lists... Done
Building dependency tree
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
mosquitto : Depends: libssl1.0.0 (>= 1.0.1) but it is not installable
Depends: libwebsockets3 (>= 1.2) but it is not installable
E: Unable to correct problems, you have held broken packages.

ANy help on this

ChrisD287 (author)Bonzadog2018-01-08

I ran into the same problem on pi3. Here is what fixed the issue for me.


sudo dpkg -i libwebsockets3_1.2.2-1_armel.deb

sudo dpkg -i libssl1.0.0_1.0.2l-1~bpo8+1_armhf.deb

sudo apt-get install mosquitto

yoyojacky (author)Bonzadog2017-12-20

reset your /etc/apt/sources.list file and typing those commands on your terminal:

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

ChilCootCharlie (author)Bonzadog2017-09-28

use atpitude to install instead, it can override the dependency conflicts. done this twice now and it worked both times.

$ sudo aptitude install mosquitto

sudo aptitide install mosquitto did not solve it for me, same error, though eventually i got some options offered that seemed to install a package

ErikK56 (author)diy_bloke2017-11-16

Use this:

add (uncomment) deb-src stretch main ui
to raspi.list in /etc/apt/sources.list.d

apt-get install cmake uuid-dev
apt-get install libwebsockets-dev
apt-get install libssl1.0.2
apt-get install mosquitto mosquitto-clients

diy_bloke (author)ErikK562017-11-17

Thank you. Fortunately it is a while ago and just apt-get install ended up working for me

Ratty Ma Hatty (author)Bonzadog2017-10-02


sudo dpkg -i libssl1.0.0_1.0.1t-1+deb8u6_armhf.deb


sudo dpkg -i libwebsockets3_1.2.2-1_armhf.deb

oldmanbeefjerky (author)2018-01-05

does instructables have a text / code window? im pretty sure it still supports html editing so you could probs add one, i say this because somehow, your dashes "-" are not dashes, or, for whatever reason, your code wont paste into putty, specifically

mosquitto_pub -d -t armtronix_mqtt –m “Hello armtronix”
Error: Unknown option '–m'.

the solution was to retype it but someone less experienced might assume its glitch caused by incorrectly installing something.

sridarm (author)2017-11-20

I get the following error when trying to install Ras PI 3 Jessie.

Could you please help ?

pi@raspberrypi:/etc/apt/sources.list.d $ sudo apt-get update

Get:1 stretch InRelease [25.3 kB]

Get:2 jessie InRelease [3,884 B]

Get:3 wheezy InRelease [3,893 B]

Get:4 stretch InRelease [15.0 kB]

Fetched 48.0 kB in 2s (22.0 kB/s)

Reading package lists... Done

N: Ignoring file 'mosquitto-repo.gpg.key' in directory '/etc/apt/sources.list.d/' as it has an invalid filename extens ion

N: Ignoring file 'mosquitto-wheezy.list.2' in directory '/etc/apt/sources.list.d/' as it has an invalid filename exten sion

N: Ignoring file 'mosquitto-wheezy.list.1' in directory '/etc/apt/sources.list.d/' as it has an invalid filename exten sion

N: Ignoring file 'mosquitto-jessie.list.1' in directory '/etc/apt/sources.list.d/' as it has an invalid filename exten sion

N: Ignoring file 'mosquitto-wheezy.list.3' in directory '/etc/apt/sources.list.d/' as it has an invalid filename exten sion

PaulS69 (author)diy_bloke2017-11-03

Doesn't work. Same error as above

diy_bloke (author)PaulS692017-11-04

not sure what you are replying to

WalterE18 (author)2017-02-22

Nice guide, but needs massive updates due to the changes in Raspbian. The base Linux changed from Wheezy to Jessie.

It took me several Google research hours to get it working on my Raspian PIXEL. I have written up my lessons learned in this comment. Maybe Armtronix considers an update of his text.

The following topics turned out to be pretty problematic (might have been me):

-Wrong Mosquitto Repository

-Change of Linux startup from init.d to systemd

The command to fetch the right Mosquitto for the PI should be:

sudo wget

As MQTT is a secure communication layer, we may want to have it automatically starting at Raspberry boot. Raspbian is using systemd for autostart. Mosquitto expects init.d. This requires substantial changes:

1) Remove Init.d references introduced during Mosquitto installation

Stop it:

sudo /etc/init.d/mosquitto stop

Remove it:

sudo update-rc.d mosquitto remove

2) Create a Mosquitto config:

We need a password file:

sudo mosquitto_passwd -c /etc/mosquitto/passwd <username>

Enter the password into the prompt

<username> may be mosquitto

We need a db for message persistance. Create the directory where persistence db files will be stored, change owner to mosquitto:

mkdir /var/lib/mosquitto/Sudo chown mosquitto:mosquitto /var/lib/mosquitto/ -R

Now we define the Mosquitto configuration at /etc/mosquitto/mosquitto.conf

File Content:

--------------------<File starts below>--------------------------

# Place your local configuration in /etc/mosquitto/


# A full description of the configuration file is at

# /usr/share/doc/mosquitto/examples/mosquitto.conf.example

# Config file for mosquitto


# See mosquitto.conf(5) for more information.

pid_file /run/

user mosquitto

max_queued_messages 200

message_size_limit 0

allow_zero_length_clientid true

allow_duplicate_messages false

listener 1883

autosave_interval 900

autosave_on_changes false

persistence true

persistence_location /var/lib/mosquitto/

persistence_file mosquitto.db

allow_anonymous true

password_file /etc/mosquitto/passwd

--------------------<File ends above>--------------------------

Now check if Mosquittoe works with this configuration file:

mosquitto -c /etc/mosquitto/mosquitto.conf

This should start Mosquitto. In another terminal window, you may execute

netstat -at

A working Mosquitto is shown by the PI listening to port 1883.

3) Create a systemd service for autostart

Create a Mosquitto service file with Nano

sudo Nano /etc/systemd/system/mosquitto.service

--------------------<File starts below>--------------------------


Description=Mosquitto MQTT Broker daemon







ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d

ExecReload=/bin/kill -HUP $MAINPID




--------------------<File ends above>--------------------------

Now enable the service, start it and look into the status:

sudo systemctl enable mosquitto.service

sudo systemctl start mosquitto.service

sudo systemctl status mosquitto.service -l

A sucessful start will show a green Mosquitto service. Note that the config and service definition are interlinked via the path of the PID file.

realalien (author)WalterE182017-10-31

Awesome writeup!

mcjamesvon (author)2017-07-14

solved the issue "Error: Connection refused"

enable read/write/execute access to the following files;


located at /var/log/mosquitto


located at /etc/mosquitto

you can modify permissions (full control) to these files by typing;

sudo chmod 777 [full-file-path/file-name]

example: sudo chmod 777 /var/log/mosquitto/mosquitto.log

then type sudo systemctl enable mosquitto

restart RPi and you are good to go.

hope this helps.

MascotFlyer (author)2017-01-11

I followed the installation recipe but when I issued the speeling corrected command

sudo mosquitto_sub -d -t armtronix_mqtt

I got the response Error: Connection refused. What am I missing?

Tin.Man (author)MascotFlyer2017-06-21

I was having the same problem as well running the Jessie one. The solution that worked for me I found here

Apparently the MQTT service is not starting automatically so the following 2 commands need to be ran:
systemctl start mosquitto
systemctl enable mosquitto

jamesthequack (author)Tin.Man2017-06-24

Good lead! that worked for me!

So the whole process:

Mosquitto Broker


sudo apt-key add


sudo wget


sudo apt-get update

sudo apt-get install

mosquitto -v
[should be >= version 1.4]

systemctl start
mosquitto [Start the systemd service.]

systemctl enable
mosquitto [Enable the systemd service to run on boot.]

SantoshA12 (author)MascotFlyer2017-01-27

I get the same error.... but don't find anyone answering why this is happening?

adas22 (author)SantoshA122017-02-20

add "sudo" before apt-get update

AlanU7 (author)2017-04-22

Excellent. Couple of points:

1. There is an error in Step 1. It should read:

for jessie
sudo wget

2. Using Putty on a Windows 7 machine I had to copy some of the commands into notepad and then change the – to - and the " to ".

Actually I didn't get step 4 but step 3 proves it works

iggirex. (author)2017-03-19

@WalterE18 hey thanks for providing a more updated description, however, I was able to follow armtronix here and get up and running using the latest Jessie / PIXEL. Could it be that apt-get update commands will steer away from the old Wheezy repo and point to the new Jessie repo? At least for me, the update command looks like its getting a list of url pointing to jessie and not wheezy. The only trip up I had was armtronix's TYPOS, specially for the subbing/pubbing commands, other than that I'm up and running.

nigel.trewartha (author)2017-03-08


I would be a real help if you could revamp the article to reflect the changes mentioned by WalterE18.

One question though - can one use one RPi as the Broker AND be able to publish+suscribe as well. The setup being 4 Rpis One as broker that also collects data and three RPis that just collect and publish+subscribe data.

JosephZ11 (author)2017-01-15

Seiously ,,,,

mosquito_sub –d –t armtronix_mqtt
mosquito_pub –d –t armtronix_mqtt –m “Hello armtronix”

Its mosquitto_sub & mosquitto The whole point on INSTRUCTIONS is to give the correct instructions and you get the command wrong, Did you even check what your posted? Obviously not.

We need to read the comments to find the solution.

rvisser2 (author)2017-01-08


TomB354 (author)2016-12-29

Step 3 has spelling errors in mosquitto with only one t ;)

Isn't there a "code block" on this website? Copy-cut does not work as the dash and "-" are replaced by different characters.

But thanks for the help !

Gedarius (author)2016-12-19

nice, worked for me

AndrewJ1949 (author)2016-11-12

Most helpful, this got me going when I was stuck. Thanks!

diy_bloke (author)2016-10-31

when testing from another server I sadly get the message that
Mosquitto_sub 'is not installed' yes, I amusing the proper ip after
the -h option.
Please help me understand: I need to type that command from another system and I add the ip from the system where i put the MQTT broker, but then isnt that error understandable, because it will first look at the current system (on which mosquitto was NOT installed) before it even gets to the ip on which it was installed, so am i really understanding you correct in how to run it from "another system"

let me clarify, I have Mosquitto installed on a raspberry that hangs on
Then I go to my desktop and typ:

mosquitto_sub -h -t armtronix_mqtt
Then I get an error telling me mosquitto_sub is not installed, but ofcourse i get that error coz it isnt installed on my desktop but on
So I am a bit at loss what that command is supposed to do, or am i now completely misunderstanding it

diy_bloke (author)2016-10-31

Good article. just a few remarks:

mosquito_sub –d –t armtronix_mqtt
should be

mosquitto_sub –d –t armtronix_mqtt

if directly copied from your text it comes back with '–d' not being a valid parameter. That is because the '–d' should be '-d' same for the 't' parameter

Hamids (author)2016-10-28

thanks for information , but what if we intend to present the received sub data in the webserver (XAMP) installed on raspberry pi as well, I mean maybe there is instruction to put received data in mysql . then server use stored data as per its clients request ?
any help really appreciated

peebee44 (author)2016-10-27


nigel.trewartha (author)2016-10-10

Very good!. Perhaps an addendum for a publisher and Subscriber. I assume that one can create a rpi with publish and subscribe at the same time?

permie (author)2016-09-15

Great writeup! Thanks for taking the time.

ProG4 (author)2016-09-04

Thx dude :)

About This Instructable




More by Armtronix:Wifi Two Triac Dimmer BoardWIFI SINGLE RELAYWIFI Eight Relay Board
Add instructable to: