Installing MQTT Broker(Mosquitto) on Raspberry Pi

Introduction: 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.

Teacher Notes

Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.

Step 1: 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.

wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key

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 http://repo.mosquitto.org/debian/mosquitto-wheezy.list

for jessie

sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list

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

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

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

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 192.168.1.10 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 192.168.1.10 -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 192.168.1.10 -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.

Be the First to Share

    Recommendations

    • Magnets Challenge

      Magnets Challenge
    • Raspberry Pi Contest 2020

      Raspberry Pi Contest 2020
    • Wearables Contest

      Wearables Contest

    47 Discussions

    0
    mbbelgium
    mbbelgium

    2 months ago

    Thanx for this tutorial. Managed to get it installed, but 3 little comments:
    - http://repo.mosquitto.org/debian/mosquitto-wheezy.... doestn't exist anymore. I went on the repo to see what is in it to change to the right file suitable for my configuration (http://repo.mosquitto.org/debian/mosquitto-buster....
    - The "-" in "mosquitto_sub –d –t armtronix_mqtt" is the wrong one, so just copy pasting will give you an error, and you will be looking for problems where there is none
    - idem for the " in the testline " mosquitto_pub –d –t armtronix_mqtt –m “Hello armtronix”
    "

    Other then that working like a charm! thank you for the explanation!

    0
    JohnN3
    JohnN3

    Question 1 year ago on Step 3

    When I get here: mosquitto_sub –d –t armtronix_mqtt
    I receive an error: Connection refused.
    I don't know where to go from here.

    0
    MrJadaml
    MrJadaml

    Answer 1 year ago

    You probably did not start the mosquitto broker -- looks like that step may have been missed in the instructions.

    In your terminal run `mosquitto`
    Then in a new tab, ssh into the pi again and run the `mosquitto_sub -d -t foo` command to subscribe to the "foo" topic.
    Then in a third terminal window ssh into the pi yet again and run `mosquitto_pub -t foo -m 'bar'`. You should see "bar" appear in the second terminal where you had originall subscribed to the "foo" topic.

    0
    Armtronix
    Armtronix

    Reply 1 year ago

    mosquitto_sub -h xxx.xxx.xxx.xxx -t armtronix_mqtt
    Here xxx.xxx.xxx.xxx is you broker ip address

    0
    MarkusM9
    MarkusM9

    1 year ago on Step 4

    I had trouble getting it to work on my old Raspberry Pi B (RPi B). The issue ended up being because I used the Raspian version "Stretch" and it having some dependencies that caused issues when installed on the older Raspberry Pi version. After I rebuilt the RPi B using "Jessie" instead of "Stretch" is worked straight away.

    0
    gatorback94
    gatorback94

    Reply 5 months ago

    Good comment. My version of Raspian is Buster, so I had to perform a similar modification. When will instructables explain this, so that younger users will not hit this obstacle?

    0
    JoachimS17
    JoachimS17

    Tip 11 months ago on Step 1

    sudo aptitude install mosquitto

    0
    Husam_a
    Husam_a

    Question 1 year ago on Step 4

    hello,
    I installed the MQTT on raspberry pi3 as my server but I can not connect to it through the MQTT from another pc on my network even though I was successful to send and receive messages on same server but not from another client on the same network any ideas of where to look the message I get is "connection failed!" thanks

    0
    GrahamR33
    GrahamR33

    Tip 1 year ago on Step 3

    The command options are prefixed by something other(–) than a regular hyphen(-) & the quotes(”) you use are different to these (') which I need on my ssh connection. Otherwise thanks for a nice short explanation of what MQTT is and how to get this set up. Cheers :)

    0
    Bonzadog
    Bonzadog

    2 years ago

    Stretch has replaced Jessie.

    Trying to install MQTT under stretch I get:

    AFter running this:

    mkdir mosquitto
    cd mosquitto/
    wget http://repo.mosquitto.org/debian/mosquitto-repo.g...
    sudo apt-key add mosquitto-repo.gpg.key
    cd /etc/apt/sources.list.d/
    sudo wget http://repo.mosquitto.org/debian/mosquitto-stretc...
    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

    0
    yoyojacky
    yoyojacky

    Reply 2 years ago

    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

    0
    ChilCootCharlie
    ChilCootCharlie

    Reply 2 years ago

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

    $ sudo aptitude install mosquitto

    0
    diy_bloke
    diy_bloke

    Reply 2 years ago

    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

    mosquitto2.png
    0
    ErikK56
    ErikK56

    Reply 2 years ago

    Use this:

    add (uncomment) deb-src http://archive.raspberrypi.org/debian/ 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

    0
    diy_bloke
    diy_bloke

    Reply 2 years ago

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

    0
    Ratty Ma Hatty
    Ratty Ma Hatty

    Reply 2 years ago

    wget http://security.debian.org/debian-security/pool/updates/main/o/openssl/libssl1.0.0_1.0.1t-1+deb8u6_armhf.deb


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

    wget http://ftp.nz.debian.org/debian/pool/main/libw/libwebsockets/libwebsockets3_1.2.2-1_armhf.deb

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

    0
    oldmanbeefjerky
    oldmanbeefjerky

    2 years ago

    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.