In the fast-changing world of IoT, there’s no such thing as a universal standard. But over the last few years, MQTT has started to establish itself as the de facto messaging protocol. In this tutorial you’ll learn how to set up an MQTT message server in just a few minutes using a Raspberry Pi.
Before starting with the tutorial, let us know a little more about MQTT.
WhatisMQTT?
MQTT stands for Message Queuing Telemetry Transport, which is quite a complex name. In simple words, it is a very simple protocol which enables us to send and receive messages across a network to any of the devices.
It is a publish/subscribe communication protocol and is quite flexible to use. The next section deals with MQTT Brokers, publishers and subscribers.
MQTTBrokers,PublishersandSubscribers
Even though these terms look scary, it is a very simple explanation. In terms of a client-server terminology, here is how to explain them:
- The MQTT Broker is the Server.
- The MQTT Subscribersand Publishers are the Clients.
The MQTT Broker in this tutorial will be our raspberry pi. The role of the broker is to receive all the messages from the publishers (i.e. the clients that publish on this server) and to send particular messages to the subscribers (i.e. the devices which have opted to view the published sensor data). The image below explains the concept we discussed just now.
But one more question arises now. How does the Broker know where and what message to publish or send to which device. This is done using the concept of TopicsandMessages. The next section will explain them in detail.
MQTTTopicsandMessages
There are no client device addresses or identifiers in MQTT, which makes it very easy to build an expansible, ad hoc network. The only thing all clients must know is the address of the server.
So, how do messages get routed between the clients? The solution for this is TopicsandMessages.
- Publishers
The Publishers sends messages to a particular topic, which are equivalent to channels. A topic can have sub-topics too. For example, in an application where you send the temperature data from a sensor connected to your fridge, the topic will look something like this:
Kitchen/Fridge/
The main topic is the Kitchen, and the appliance is the subtopic. The message will be "Temperature:14" on the given topic.
- Subscribers
The subscribers listen to the topic. So, if the subscriber is listening to the topic Kitchen
then it will have access to all the sub topics which are a part of this topic.
- Broker
The Broker handles all these topics and acts as an intermediate connector. It helps the publisher to publish its messages on a topic and the subscriber to listen to the given topic. The only requirement is they should be on the same network if the Broker is running locally.
This is all you need to know about MQTT to get on with this tutorial. Now, we will learn the following:
1. Host a MQTT Broker on a Raspberry Pi
2. Publish to a particular topic locally and remotely using a Windows PC.
3. Send DHT11 sensor data from a node MCU to the MQTT Server.
So, let's get started.
Hosting a Raspberry Pi MQTT BrokerThe only requirement is a raspberry pi with the latest Raspbian Buster build installed. Please follow this tutorial to do this.
After this is done, you can use a display to access the Pi if you have one. I generally use headless systems so everything I outline here will use the command line. Here’s a complete guide to setting up a headless installation of Raspbian Lite.
If you’re working on a GUI installation, just open Terminal and follow the instructions.
1. Install the mosquitto MQTT Broker
mosquitto
is a popular MQTT broker that is well-supported on Debian-based Linux platforms such as Raspbian. It’s easy to install using apt
:-
sudo apt install mosquitto mosquitto-clients
You’ll need to enter your password the first time you run sudo
.
You don’t strictly need the mosquitto-clients
package for running the broker, but installing it allows you to run the MQTT client code locally which is great for testing.
It also means you can use the Raspberry Pi as a proper MQTT client as well as a broker. This means you could, for example, add a user interface to control other MQTT clients around your home directly from the Raspberry Pi.
2. Enable the mosquitto broker
Enable the broker and allow it to auto-start after reboot using the following command:-
sudo systemctl enable mosquitto
The broker should now be running. You can confirm by checking the systemd
service status:-
sudo systemctl status mosquitto
This should produce an output similar to:-
● mosquitto.service - LSB: mosquitto MQTT v3.1 message broker
Loaded: loaded (/etc/init.d/mosquitto; generated; vendor preset: enabled)
Active: active (running) since Sat 2018-12-29 16:27:56 GMT; 22h ago
Docs: man:systemd-sysv-generator(8)
CGroup: /system.slice/mosquitto.service
└─1685 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf
Dec 29 16:27:56 raspberrypi systemd[1]: Starting LSB: mosquitto MQTT v3.1 message broker...
Dec 29 16:27:56 raspberrypi mosquitto[1679]: Starting network daemon:: mosquitto.
Dec 29 16:27:56 raspberrypi systemd[1]: Started LSB: mosquitto MQTT v3.1
Publish/Subscribe to a Topic Locally and RemotelyTestingMQTT Topic Locally
In this section, we will test if our server is active by using the Raspberry Pi terminals to test. Follow the steps given below to do so.
Test Mosquitto by creating two new instances of the terminal. In one terminal enter the following:
mosquitto_sub -v -t test/message
In the other new terminal enter:
mosquitto_pub -t test/message -m 'Hello World!'
After pressing enter on your second terminal you should see the message “test/message Hello World!” on the first terminal as illustrated in the figure below:
TestingMQTT Topic Remotely
When it comes to a remote connection, we will use a windows PC do so. For that, we will need mosquitto in our windows PC or any machine you are using.
Installing mosquitto MQTT Client on Linux
Assuming you’re running on Linux, including a virtual Linux machine, you can install the mosquitto
client code using:-
apt update
sudo apt install mosquitto-clients
If your installation doesn’t yet support apt
, replace it with apt-get
.
Installing mosquitto MQTT Client on mac OS
Or if you’re on mac OS, there’s no separation between the the broker and client packages, so just use the following to install everything:-
brew update
brew install mosquitto
Installing mosquitto MQTT Client on Windows
Installation and usage on a Windows machine will be different – please see this download link for more info.
Now after you have installed this, we can move on to the next step. Please note that you have to keep the Broker running on your Raspberry Pi for this to work. Additionally, you will need the hostname or IP of your Raspberry Pi. For that, simply type these commands on your Pi terminal:
hostname #for obtaining the hostname. default name is raspberrypi
hostname -I #for obtaining the IP of your Pi
Please note one of these values as you will be needing them. This tutorial is for windows so all the commands given below are for the command prompt and not the Linux terminal.
In windows, you cannot use the commands for your mosquitto directly. You first need to cd into your mosquitto folder whose location you must have selected while installing the package. Now, to publish a message from your PC, just type the following commands:
cd <--location of your mosquitto folder. Default : C:/Program Files/mosquitto-->
mosquitto_pub -h raspberrypi -t "test/message" -m "Hello World!"
-----or-----
mosquitto_pub -h <--IP of your host--> -t "test/message" -m "Hello World!"
You can just subscribe to the test message like this:-
mosquitto_sub -h raspberrypi -t "test/message"
The output will look something like this:
This is the final section of our tutorial. We will now use a demo application to explain how to send sensor data from a NodeMCU (any sensor Node) to our MQTT server which we have hosted on the raspberry pi.
This sketch demonstrates the capabilities of the pubsub library in combination with the ESP8266 board/library.
- It connects to an MQTT server then:
- publishes "hello world" to the topic "outTopic" every two seconds
- subscribes to the topic "inTopic", printing out any messages
- It receives. NB - it assumes the received payloads are strings not binary. If the first character of the topic "inTopic" is an 1, switch ON the ESP Led, else switch it off
- It will reconnect to the server if the connection is lost using a blocking reconnect function. See the 'mqtt_reconnect_nonblocking' example for how to achieve the same result without blocking the main loop.
The code is attached in the end. After uploading the code and testing the functions, your terminals and the Serial Monitor will looking like this:
That's all for this tutorial. Hope you find it useful and informative. Please like this tutorial and share.
Comments