Real Time Data Transfer for IoT with MQTT , Android and NodeMCU

In my last article I talked about using Google Firebase as a real time communication medium for your IoT based applications. Today I’m going to tell you about MQTT (Message Queuing Telemetry Transport) which is a very handy messaging protocol you can use in your IoT application.

What is MQTT ?

First , lets have a quick introduction about MQTT. It is an publish and subscribe based (more on this later) extremely lightweight messaging protocol. Since it is lightweight, it can be used with connections which have a very low bandwidth (Slow Speeds) or connections which are unreliable.

What is the Advantage of using MQTT over Firebase ?

The main advantage of using MQTT is it is very lightweight. So if you are on a very slow network, using Firebase can be a burden because it uses HTTP and a relatively high bandwidth. If your network is very unreliable, even a small signal drop can interrupt your communication with the IoT device. But because MQTT is so lightweight it can communicate in most of these scenarios and can be very easily used to transmit your IoT sensor/device readings.

What is Publish and Subscribe ?

Instead of using the common client-server pattern, MQTT uses publish and subscribe method to transfer information. In this method there are two main entities ,

  1. MQTT Broker
  2. MQTT Client

Here the Broker can be named as the central entity of this system. Clients can send messages to the Broker only. They can’t send messages to each other. Here sending messages is known as “publishing” . These messages typically have a Topic and a Message body. The broker is the entity who filters the messages which are sent by the clients and forwards them.

To receive a certain message. We have to subscribe to a certain topic. And we will receive all the messages published under that specific topic irrespective of the client who sent them.

photo credits — www.amebaiot.com

Lets take an example. Imagine you have couple of weather sensors (temperature sensor and a humidity sensor) and 2 mobile phones. You need to send the temperature to one mobile phone and the humidity to the other.

Using MQTT we can manage this task very easily. First you need to set up a MQTT broker service. ( There are a lot of free services on the internet). After that you can register the two sensors on the broker as clients and set them up to send messages on Topics “Temp” and “Humid”. Then we can also register the two mobiles on the broker and the first mobile phone can subscribe to the topic of “Temp” and the second one can subscribe to “Humid”. THAT’S IT ! Now those two devices will receive the temperature and the humidity whenever the sensors publish it to the broker. :)


In this simple demonstration I have used Cloud-MQTT as the MQTT broker. For the arduino part I used Pub-Sub Client as the MQTT library for arduino (Node-MCU). For the android part I used Paho MQTT as the MQTT API.

First of all you need to set-up the MQTT broker. Go to https://www.cloudmqtt.com/ and register for a free account. After that you will get a screen like this.

Step 1

Then click Create New Instance to create a new project.

Step 2

Follow these steps to create the MQTT boker.

Step 3

After confirming this. You are done making the MQTT broker. You will be redirected to a dashboard like this.

Here the most important sections are Server, User, Password and Port. So make sure to write down those.


Arduino Code

After that let’s get into the arduino part. Here I have used arduino flashed NodeMCU with a DHT-11 temperature sensor. If you are an absolute beginner, you can go here to know how to set up the NodeMCU and here is a little introduction to the NodeMCU.

If you don’t have a DHT-11 it doesn’t matter ! take any sensor you want , or any data source.

Here is my connection of the two devices.

Connection.

In the coding part, first you need to import the MQTT library and the EspWiFi Library.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

Then define these constants which are needed for the MQTT and WiFi Configurations. Here use your Wifi Credentials and use user, password in the Cloud-MQTT dashboard for MQTT user and Pass.

Constants

Then define these constants and inport your sensor’s library.

Constants

Then you should define functions to set-up wifi , set-up MQTT and callback method for MQTT message. These are coded by referencing Pub-Sub client documentation.

Set-Up WiFi
Connect MQTT
Callback Function

Then use these functions in the setup , to configure the settings and connect to servers.

Setup

Then use the following code in the loop to publish messages to the MQTT broker. Here we can get both Temperature and Humidity from the DHT sensor. We are saving them in h and t. And then publishing them on topics “dht” and “bmp”.

loop { }

Now the arduino code is complete. You can go to the WebSocket UI tab in your dashboard and see the sensor values !

WebSocket UI

The Android Application

Then we need to make the android application to get the data from the MQTT broker.

First make a new project in Android Studio. if you are not aware of that , follow steps in this.

Then you need to create a simple UI. Just to display the sensor values. you can see the UI which I made below.

Sample User Interface

Then you need to include the Paho MQTT plugin in your app.gradle file as below. (You can copy the code from my git repo which is included at the end)

In the main activity you need to include 3 main functions.

  1. Connect( )
  2. Publish ( )
  3. Subscribe ( )

Connect function is used to connect with the Cloud MQTT Broker. Here make sure to give the server address in this format “tcp://your-URL:port”. As an example take this “tcp://m12.cloudmqtt.com:17389”. Then set options.setUserName and setPassword methods correctly and after that you can use subscribe( ) method to subscribe to any topic you want.

Here are the publish and subscribe methods. This setup is relatively easy to understand. So I’m not going to explain it in detail.

Finally include the connect( ) method in your onCreate( ) method. The final code looks like this.

final

Then run the code in a android device/emulator and make sure there is no error !

You can check you android application by going to the WebSocket UI in your cloud MQTT dashboard and sending a number by the topic dht as below. If the application is correct it will appear on the application screen.


Finally run both android application and the arduino application and you can see the android application showing the temperature/humidity from the given time interval.

Using this simple setup you can create many complicated systems as you wish, no need to have a deep understanding about the MQTT protocol !

Thank you for reading ! The full code is available on my github ! Feel free to ask questions regarding this :) Cheers !