Sunday, 28 February 2016

MQTT + ESP8266 12e (Node MCU)





             MQTT(Message Queue Telemetry Transport) is a Client Server publish/subscribe messaging transport protocol. This protocol is widely used in the field of IoT for communication between Machine to Machine because of its following features ,
  •  light weight,
  • open, and
  • designed so as to be easy to implement.

Hence these features are Ideal for the IoT purpose as we need to transmit mostly status of one machine to another.It was originally developed by IBM and is now an open standard.


Note: If you are not familiar with Node MCU module, I'll request you to watch my video Getting started with Node MCU before going into MQTT.


Working of MQTT Transport Protocol.

This protocol is easy to implement and also very easy to understand. It basically comprises of one Broker and multiple clients where clients can be treated as our smart phone, sensors, etc. and they all communicate with the server which is known as Broker.

In this protocol, every client need to connect to any address of the broker which is known as the topic to be subscribed in MQTT. In single broker there can be multiple topics and clients can also subscribe to multiple topics of the same broker.

First lets see this process in block diagram which will be easy for you to understand.



So basically here we have one broker and 3 clients subscribed to the topic “temperature”. 


So as soon as any of the client(Temperature Sensor) publishes or updates the value of Temperature to the broker, than all the clients(Smart Phone) subscribed to that topic will receive the value of temperature updated.
That’s it.

We can do a lot with this simple protocol. We can even subscribe to the topics of any other person if we know the username and password of the broker.

Implementation 


We will be making a project on "Controlling Home Appliances through Internet via MQTT". I will be demonstrating this by controlling Radio and Light of my room. 

Things required for making this:

  • ESP8266 12e development Board
  • 6V Relay x2
  • BC547 Transistor x2
  • 330 ohm Resistor x2
  • Connecting wires
  • BreadBoard




Broker
First of all we will need a MQTT broker. There are many broker for MQTT but I have used Adafruit MQTT broker. Its quite simple and its UI is also great. You will love to use that broker. For using Adafruit MQTT broker, first of all you need to make an account on Adafruit.io. Fill up the basic details and you are ready to use that broker.

Than goto your dashboard. My dashboard is like this.


There many options on the right corner of the page to edit the blocks, add new blocks,get the key,etc.

Now we will start with making a new button on the dashboard. For that click on the 2nd button i.e."Create a New block". It will show this window.


There are number of blocks to be added in this window like toggle button, push button, slider etc. In our project we will be using the first block i.e. toggle button. Click on create button and you will get following options.


Than you have to provide feed name which should be unique because this feed name is nothing but the topic which clients will be subscribing. I have given name of the feed as onoff. Than click the create button. And click on the choose button front of your feed name. Than click on the next step.


Than we need to provide what string to be sent when the button is ON and OFF. So, I have wriiten 1 for ON and 0 for OFF.


That's it. You are done with the broker side. No complexity, nothing. Only simple and great UI and that's why I like adafruit broker. You can even drag and resize the block according to your need.


Account holder at Adafruit IO will have their unique key which is also called as password for the subscription. You can get your key by clicking on the third button on the right corner. It is a key shaped icon on the button.



Client

We will be using two clients in our project first is the ESP866 12e development board and another is our Smart Phone. First of all you need to download library for MQTT  client by Adafruit. You can download the library from here.

Now open example in your Arduino IDE named "mqtt_esp8266". Just change the ssid name, password for internet access and also provide your broker username and password(AIO key). And than just upload the program because in the example sketch, they have already subscribed to the topic onoff so no need to change anything in this. Than open serial monitor and your adafruit dashboard side by side.


As MQTT is very light weight, the response we can observe is around in couple of milliseconds. It's really fast!!! As you can see in the Serial monitor, whenever I toggle the switch it shows the response like "GOT 1" or "GOT 0". And side by side it is also publishing value of counter on the topic named photocell. 

Another Client is our smart phone. For that you need to download an app of MQTT client. I have downloaded this app in my android device. Just provide 4 details.

Broker URL, here it is (io.adafruit.com)
Port, (1883)
Username ( username you have registered while making your account)
Password (Key which you can get by clicking the key icon on your dashboard)

After filling this details just subscribe to any of the topic and send either 0 or 1 to turn the button on or off. And yeah!!!!, now you can toggle the button from anywhere in the planet earth as long as you are in coverage area of your Internet service provider.


#techiesms

4 comments:

  1. Hi I'm Rahul. I complied the exampled code without any error. After modifying the code I got errors like include libraries even though I included. Can u tell me why the errors are occur.

    ReplyDelete
    Replies
    1. Can u submit the modified code here .

      Delete
  2. Hi,I'm Ahsan. I succesfully ran the basic onoff switch example and now i am trying to add function to control fans therefore will be using the adafruit's slider to control the fan's speed . Can you help me how to add the analogwrite function in the code.

    ReplyDelete
  3. Just WhatsApp me at 82000 79034
    Will try to find solutions

    ReplyDelete