Pradeep Singh | 30th April 2016
MQTT is the protocol of choice for M2M and IoT Applications. However, when it comes to selecting the MQTT broker, most of the times we resort to Cloud based Brokers. Having a local MQTT Broker may have many advantages over Cloud based Brokers, like Security, Flexibility, Reliability, Low Latency, Cost Effectiveness, better QoS implementation etc.
Mosquitto MQTT Broker:
The Raspberry Pi is a Single Board Computer developed by Raspberry Pi Foundation. For more details you can refer to https://www.raspberrypi.org/
Mosquitto on Raspbery Pi:
Raspberry Pi has enough compute power to run Mosquitto and function as a personal MQTT Broker which can cater most of our personal MQTT needs. So let’s go ahead and explore Installation, Testing and Uninstallation process.
What do you need?
- Raspberry Pi with Raspbian Operating System
- Ethernet / WiFi Connection to Internet from Raspberry Pi
A. Install Mosquitto MQTT Broker:
1. SSH into Raspberry Pi and create a new directory for temp files –
mkdir mosquitto cd mosquitto
2. Import the repository package signing key –
wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key sudo apt-key add mosquitto-repo.gpg.key
3. Make the repository available to apt –
cd /etc/apt/sources.list.d/ sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
4. Install Mosquitto MQTT Broker –
apt-get install mosquitto
5. Check Mosquitto Service Status, Process and Default Port (1883) –
service mosquitto status ps -ef | grep mosq netstat -tln | grep 1883
If you see Mosquitto service running and listening to TCP Port 1883, you have a functional MQTT Broker.
B. Test Mosquitto MQTT Broker with MQTT Client:
For testing you can use any MQTT Client. However, if you have Python 2.7 Installed on your machine, you can test it with following sample Python scripts. To execute these Scripts, you must have Paho MQTT Client installed on your machine. You can install it with pip command –
pip install paho-mqtt
Once Paho Client Library is installed, you can download and execute following Python scripts (Don’t forget to change “MQTT_BROKER” IP Address) –
C. Uninstall Mosquitto MQTT Broker:
To uninstall Mosquitto you can use following command –
sudo apt-get purge mosquitto
If you want to completely remove Mosquitto with it’s associated configuration files, use following command –
sudo apt-get --purge remove mosquitto