loading

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

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.

<p>Excellent. Couple of points:</p><p>1. There is an error in Step 1. It should read:</p><p>for jessie<br>sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list</p><p>2. Using Putty on a Windows 7 machine I had to copy some of the commands into notepad and then change the &ndash; to - and the &quot; to &quot;<strong>.<br></strong></p><p>Actually I didn't get step 4 but step 3 proves it works<strong><br></strong></p>
<p>@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.</p>
<p>@Armtronix</p><p>I would be a real help if you could revamp the article to reflect the changes mentioned by WalterE18.</p><p>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. </p>
<p>Nice guide, but needs massive updates due to the changes in Raspbian. The base Linux changed from Wheezy to Jessie. </p><p>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.</p><p>The following topics turned out to be pretty problematic (might have been me):</p><p>-Wrong Mosquitto Repository</p><p>-Change of Linux startup from init.d to systemd</p><p>The command to fetch the right Mosquitto for the PI should be:</p><p>sudo wget <a href="http://repo.mosquitto.org/debian/mosquitto-jessie.list" rel="nofollow"> http://repo.mosquitto.org/debian/mosquitto-jessie...</a></p><p>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:</p><p>1) Remove Init.d references introduced during Mosquitto installation</p><p>Stop it: </p><p>sudo /etc/init.d/mosquitto stop</p><p>Remove it:</p><p>sudo update-rc.d mosquitto remove</p><p>2) Create a Mosquitto config:</p><p>We need a password file:</p><p>sudo mosquitto_passwd -c /etc/mosquitto/passwd &lt;username&gt;</p><p>Enter the password into the prompt</p><p>&lt;username&gt; may be mosquitto</p><p>We need a db for message persistance. Create the directory where persistence db files will be stored, change owner to mosquitto:</p><p>mkdir /var/lib/mosquitto/Sudo chown mosquitto:mosquitto /var/lib/mosquitto/ -R</p><p>Now we define the Mosquitto configuration at /etc/mosquitto/mosquitto.conf </p><p>File Content:</p><p>--------------------&lt;File starts below&gt;--------------------------</p><p># Place your local configuration in /etc/mosquitto/</p><p>#</p><p># A full description of the configuration file is at</p><p># /usr/share/doc/mosquitto/examples/mosquitto.conf.example</p><p># Config file for mosquitto</p><p>#</p><p># See mosquitto.conf(5) for more information.</p><p>pid_file /run/mosquitto.pid</p><p>user mosquitto</p><p>max_queued_messages 200</p><p>message_size_limit 0</p><p>allow_zero_length_clientid true</p><p>allow_duplicate_messages false</p><p>listener 1883</p><p>autosave_interval 900</p><p>autosave_on_changes false</p><p>persistence true</p><p>persistence_location /var/lib/mosquitto/</p><p>persistence_file mosquitto.db</p><p>allow_anonymous true</p><p>password_file /etc/mosquitto/passwd</p><p>--------------------&lt;File ends above&gt;--------------------------</p><p>Now check if Mosquittoe works with this configuration file: </p><p>mosquitto -c /etc/mosquitto/mosquitto.conf</p><p>This should start Mosquitto. In another terminal window, you may execute </p><p>netstat -at</p><p>A working Mosquitto is shown by the PI listening to port 1883.</p><p>3) Create a systemd service for autostart</p><p>Create a Mosquitto service file with Nano</p><p>sudo Nano /etc/systemd/system/mosquitto.service</p><p>--------------------&lt;File starts below&gt;--------------------------</p><p>[Unit]</p><p>Description=Mosquitto MQTT Broker daemon</p><p>ConditionPathExists=/etc/mosquitto/mosquitto.conf</p><p>After=network.target</p><p>Requires=network.target</p><p>[Service]</p><p>Type=forking</p><p>RemainAfterExit=no</p><p>StartLimitInterval=0</p><p>PIDFile=/run/mosquitto.pid</p><p>ExecStart=/usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf -d</p><p>ExecReload=/bin/kill -HUP $MAINPID</p><p>Restart=on-failure</p><p>RestartSec=2</p><p>[Install]</p><p>WantedBy=multi-user.target</p><p>--------------------&lt;File ends above&gt;--------------------------</p><p>Now enable the service, start it and look into the status:</p><p>sudo systemctl enable mosquitto.service</p><p>sudo systemctl start mosquitto.service</p><p>sudo systemctl status mosquitto.service -l</p><p>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.</p>
<p>I followed the installation recipe but when I issued the speeling corrected command</p><p> sudo mosquitto_sub -d -t armtronix_mqtt</p><p>I got the response Error: Connection refused. What am I missing?</p>
<p>I get the same error.... but don't find anyone answering why this is happening?</p>
<p>add &quot;sudo&quot; before apt-get update</p>
<p>Seiously ,,,,</p><p>mosquito_sub &ndash;d &ndash;t armtronix_mqtt<br>mosquito_pub &ndash;d &ndash;t armtronix_mqtt &ndash;m &ldquo;Hello armtronix&rdquo;</p><p>Its mosquitto_sub &amp; 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.</p><p>We need to read the comments to find the solution.</p>
<p>Thanks!!</p>
<p>Step 3 has spelling errors in mosquitto with only one t ;)</p><p>Isn't there a &quot;code block&quot; on this website? Copy-cut does not work as the dash and &quot;-&quot; are replaced by different characters.</p><p>But thanks for the help !</p>
<p>nice, worked for me</p>
<p>Most helpful, this got me going when I was stuck. Thanks!</p>
<p>when testing from another server I sadly get the message that <br>Mosquitto_sub 'is not installed' yes, I amusing the proper ip after <br>the -h option.<br>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 &quot;another system&quot;<br><br>let me clarify, I have Mosquitto installed on a raspberry that hangs on 192.168.1.102.<br>Then I go to my desktop and typ: </p><p><strong>mosquitto_sub -h 192.168.1.102 -t armtronix_mqtt<br></strong>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 192.168.1.102<br>So I am a bit at loss what that command is supposed to do, or am i now completely misunderstanding it</p>
<p>Good article. just a few remarks:</p><p><strong>mosquito_sub &ndash;d &ndash;t armtronix_mqtt<br></strong>should be <strong> </strong></p><p><strong>mosquitto_sub &ndash;d &ndash;t armtronix_mqtt<br></strong></p><p>if directly copied from your text it comes back with '&ndash;d' not being a valid parameter. That is because the '&ndash;d' should be '-d' same for the 't' parameter<strong><br></strong></p><p></p><p></p>
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 ?<br>any help really appreciated
<p>super! </p>
<p>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? </p>
<p>Great writeup! Thanks for taking the time.</p>
<p>Thx dude :)</p>

About This Instructable

93,863views

43favorites

License:

More by Armtronix:OpenHAB http binding with Armtronix Dimmer/Speed Control Board  Accessing Devices Connected to Router From Remote Place (using Pi and OpenHab) How to Configure Static IP Address for Raspberry Pi 
Add instructable to: