-
Notifications
You must be signed in to change notification settings - Fork 0
Message Queuing using MQTT and Mosquitto
MQTT is a machine-to-machine (M2M)/"Internet of Things" connectivity protocol. It was designed as an extremely lightweight publish/subscribe messaging transport. It is useful for connections with remote locations where a small code footprint is required and/or network bandwidth is at a premium.
The MQTT protocol was developed around 1999. The main goal of this protocol was to create a protocol very efficient from the bandwidth point of view. Moreover, it is very power-saving protocol. For all these reasons, it is suitable for IoT.
This protocol used publish-subscriber paradigm in contrast to HTTP based on request/response paradigm. It uses binary messages to exchange information with a low overhead. It is very simple to implement and it is open. All these aspects contribute to its large adoption in IoT. Another interesting aspect is the fact that MQTT uses TCP stack as transmission substrate.
In software architecture, publish–subscribe is a messaging pattern where senders of messages, called publishers, do not program the messages to be sent directly to specific receivers, called subscribers, but instead categorize published messages into classes without knowledge of which subscribers, if any, there may be. Similarly, subscribers express interest in one or more classes and only receive messages that are of interest, without knowledge of which publishers, if any, there are.
In many pub/sub systems, publishers post messages to an intermediary message broker or event bus, and subscribers register subscriptions with that broker, letting the broker perform the filtering. The broker normally performs a store and forward function to route messages from publishers to subscribers. In addition, the broker may prioritize messages in a queue before routing.
In our attempt to build an Internet-of-Things (IoT) network for making our home smarter, we are going to use MQTT as the protocol for the sensors, microcontrollers, and computers to communicate with each other. For this, we are going to use Mosquitto as the message broker running on a Raspberry Pi

You can find the official installation instructions here, but be warned that it may change with time. We are going to go through some additional steps for our purposes. At the time of Ahrar going through this process, the following instructions led to a successful installation:
We begin by opening a terminal on the Raspberry Pi (RPi). You can do this in a number of ways:
- If you are running a graphical user interface (GUI) on the RPi, select the
Terminalprogram - If you boot into the command line interface (CLI), you are continue to work here
- You can ssh into the RPi using a second computer, in which case you will access the RPi using a CLI
To use the new repository you should first import the repository package signing key:
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/
Then one of the following, depending on which version of debian you are using:
sudo wget http://repo.mosquitto.org/debian/mosquitto-wheezy.list
sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list
Then update apt information:
sudo apt-get update
Upgrade the system packages
sudo apt-get upgrade
Install moquitto and mosquitto-clients (this is to later test our message broker)
sudo apt-get install mosquitto mosquitto-clients
SIDE NOTE: If you run into any errors indicating that some dependencies could not be installed, there is a chance that your RPi installation is not of Debian 7 or 8 (wheezy or jessie, respectively), but instead the recent Debian 9 (stretch).
You can check your OS version by running the following command
cat /etc/os-release
If you are running Debian 9 Stretch, you will need to register the repositories for jessie before being able to download the required dependencies.
Do this by opening the /etc/apt/sources.list file (ensure that you have write permission; use sudo if necessary), and adding the following line into the file
deb http://archive.raspbian.org/raspbian jessie main contrib non-free
Then run the following commands again
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients