Today people are in great need of cheap and qualitative micro controllers that track sensor data and transfer it via the internet. It is a new technology that has been predicted to completely change our world.
Instead of giving difficult explanations with complicated terms, let’s make a long story short and explain what IoT means using a simple example.
Imagine that your alarm automatically knows that your bus is going to be 15 minutes late today and it corrects itself to go off a quarter of an hour later. In addition to this, your coffee machine starts making your favorite drink a bit later too. So, it all coordinates and you can sleep a little bit longer. Your environment accommodates itself to help you save your valuable time.
That sounds like a perfect world, but this is what reality is going to look like in the nearest future. In fact, some scientists predict that 50 billion devices will be connected and will work in coordination by 2020. Such coordination will be enabled by the MQTT protocol and further you can find more specific and detailed explanation.
MQTT is a machine to machine (M2M) connectivity protocol. It was designed as a very simple thing to connect devices (sensors, controllers, etc.) whose locations are not far away from a Broker.
Broker is a MQTT server software which is a primary device inside IoT network. It collects information about each IoT device state change and enables organization of the internal network ecosystem without human intrusion.
Each sensor, controller or other device can have one of two possible roles regarding to other IoT devices: Producer or Consumer. In other words, each device can work both as the Producer for its own events and as the Consumer for events from other devices.
As you can see, Broker functions are quite trivial. It provides the ability to store each state change from Producer and allows Consumers to receive information about these changes. Each IoT device that works with the Broker inside the IoT network has its own unique device ID. This ID is used to identify connected device and helps Broker to keep information about connected device actual. Information published by Producer is stored in topics. Within MQTT it is called Topic Path. Each Topic on the Broker can have zero or several subtopics. To put it simply, topics are organized as a tree and in practice they look like a path inside *nix filesystems. This way each device category can be easily identified.
The installation of MQTT is quite simple. There are a lot of apps made for different platforms. One of these apps is Mosquitto.
I have chosen old Raspberry Pi 1 Model B as a platform for the Broker because it is small, affordable, and has low power consumption. Moreover, it has enough HW power for Mosquitto server. Another reason was that I haven’t had the chance to use this board yet and this seemed like a perfect opportunity. From my personal point of view, every device should be used to its full capacity, so there is no reason to use HW with OS like Linux to operate simple devices.
Below you can find a quick guideline on how to install Mosquitto on Raspberry Pi with Raspbian OS.
Working RPI board would be a plus, but this is not critical as Mosquitto has many pre-built packages for different kinds of platforms. If you don’t have Raspberry Pi, steps that are listed below can be easily made using Debian or Ubuntu virtual machine (VM). But because this topic is not about setting up VM, I will skip this step.
So, the first thing we should do is update package repository to the latest version. To do this, connect to your RPI via PuTTY (for Windows users) or SSH (for Linux users) using network connection. There are several reasons why it is so important to have your RPI connected to the network:
If you have a question why wired interface is preferred to wireless, here is a short answer. When you build object infrastructure with high requirements for functional stability and security (industrial objects) you should choose wired network.
When terminal connection is ready, please log in to RPI shell using your RPI username and password (standard username and password are: username: pi, password: rasbberry).
After you are logged in, execute the next command to get root access:
This command will give you permission to update and install packages using the following commands:
apt-get update -y apt-get install mosquitto mosquitto-clients -y
Package mosquitto-clients is required to test your Broker without real hardware publishers and subscribers. It contains two simple tools: mosquitto_pub and mosquitto_sub, which simulate publisher and subscriber work accordingly. If you do not have a plan to test Broker or have some real publishers/subscribers, you can omit installing this package.
When installation is finished, you will get configured MQTT Broker with default settings. Now you can test it using tools from the second package.
Let’s use mosquitto_sub to create topic ‘test’ and listen to it.
mosquitto_sub -d -t test
Below is a picture of how it looks on my MAC. (Listening to topic ‘test’).
Open the second terminal connection, log in to board shell, and start a publisher.
mosquitto_pub -d -t test –m "Hello world"
Both tools have -d and -t options. Option -d is used to enter debug mode. This mode allows users to view received messages from publishers on a terminal. On the production cases this option is not used. More information about the available options and their description you can find using ‘mosquitto_sub -h‘ or ‘man mosquitto_sub‘ commands. Option -t specifies topic name which should be listed by subscriber.
The result of the executed command is published message “Hello world”. As you have probably guessed, option -m specifies which message should be sent.
Immediately after you execute command PUBLISH, message appears in subscriber terminal. Moreover, you can start several subscriber terminals and all of them will receive messages of the interesting topics. Here is how the result looks on the subscriber terminal.
In some cases publishers, subscribers, and a Broker are all separate devices. In this case subscriber and publisher should know an IP address of the Broker. This behavior can also be simulated with mosquito client tools. Just start each tool on separate machine or virtual machine (all machines should be connected and be in the same subnet) and specify the IP address of the Broker after option -h.