Introduction: Controlling Home Appliances Using Node MCU Via MQTT
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.Second Image shows the block diagram.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. This is shown in Third image.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.
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
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 shown in the image 5.
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(Image 6).
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(Image 7).
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 on off. Than click the create button. And click on the choose button front of your feed name. Than click on the next step( Image 8).
Than we need to provide what string to be sent when the button is ON and OFF. So, I have written 1 for ON and 0 for OFF(Image 9).
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(Image 10).
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(Image 11).
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(Image 12).
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)
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.