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