Complete Guide to Home Automation

Updated: December 7, 2016


Home automation is the residential extension of building automation and involves the control and automation of lighting, heating, ventilation, air conditioning (HVAC), appliances, and security. Modern systems generally consist of switches and sensors connected to a central hub sometimes called a “gateway” from which the system is controlled with a user interface that is interacted either with a wall-mounted terminal, mobile phone software, tablet computer or a web interface (Wikipedia).

There are various commercial Home Automation systems are available which may cost handsome amount of money to spend for controlling only few appliances and control is limited to the options set by vendor. This guide is intended to establish such an efficient system with unlimited options according to users needs while keeping the budget as low as possible. Let’s dive in the world of Internet of Things¬† ūüėé .

As mentioned earlier, all the appliances are connected to central controlling unit which is Raspberry Pi 2, so let’s start from the beginning, The Brain.

1) Central Control System:

The Hardware:

Prepare SD card according by downloading Raspbian Jessie Full and following the instruction given on this link. There is no need of external display to setup Raspbian Jessie which comes with SSH access pre-enabled. Just plug in SD card with Raspbian Jessie installed and LAN Cable to Raspberry Pi (RPi).

For SSH access, We need to know the IP address of RPi. Consult your router documentation or use this guide¬†and¬†login to RPi using Putty, A free SSH Client for Windows, with pi as user name and raspberry as password. In picture below, you can see pi@server, where server is the Hostname of RPi in my case, it will be Raspberry in your case.¬†After login, let’s install our necessary software.


The Software:

First of all, update the system with these commands.

As our main controll system is java-based, NPM is¬†the default package manager for the JavaScript runtime environment Node.js. Raspbian Jessie comes with Node-Red and Node.js pre-installed. Let’s install other required software by issuing following commands.



Follow this tutorial to install Mosquitto MQTT Broker (MQ Telemetry Transport)


Nginxweb server is required for OTA (Over the Air) updates of our sensor nodes.

Now Nginx is installed and our web server location is root /var/www/html .


Samba is a free software re-implementation of the SMB/CIFS networking protocol which will help us uploading binary files of OTA to servers.

The last command typed above will open Nano editor to create a new configuration file. Paste the contents of Samba-Configuration¬†file¬†in to nano editor by clicking “Right Mouse Button”. Save the configuration file by pressing “Ctrl” and “X” key followed by “Y” and “Return” key. After that add user Pi to gain access to Pi File System through Samba.

Bu issuing the command above, system will ask for password, type your password, press “Return”¬†key, retype the same password and again press “Return” key. Now open the IP address of your Pi in your computer “Windows Explorer”. For example;


There will be two folders available, namely “root” and “www”. “www” is the folder where we’ll upload our OTA update binary files.

Node-RED Nodes:

Now We have completed almost all the software part but there are few¬†additional tools¬†still need to be loaded over Node-RED. Node-RED nodes comes with the name of “node-red-contrib-node”, where “node” is name used for¬†certain node. For installing additional nodes, We have to point towards the node-red installation directory, where our additional nodes will be installed and will be available in Node-RED.

Pointing towards right directory is crucial for availability of newly installed node with in Node-RED. Let’s install few useful nodes for Node-RED.

After installation of additional Node-RED nodes, always restart Node-RED with following command.

“node-red-contrib-bigtimer” will be used for automation purposes while “node-red-dashboard” will provide us web-based user interface to control our sensor nodes.

2) Sensor Node:

The Hardware:

The Software:

UPDATE December 7, 2016:

Use ESPMetRed, An Arduino IDE compatible library for ESP8266 based platforms to communicated between Node-Red and ESP8266 Sensor Node.

Install Arduino core for ESP8266 according to this post. ESP8266WiFi, ESP8266httpUpdate, ESP8266HTTPClient and FS Libraries come with Arduino core for ESP8266. Download PubSubClient MQTT and ArduinoJson Libraries from Githab (by clicking on certain library name) and install according to the instructions given here.

Now it comes the programming part. Open your favourite Arduino IDE, Select your relevant board i.e. Generic ESP8266 Module | ModeMCU 1.0 | WeMos D1, R1 & mini, from the Tools > Board. Also select appropriate port from¬†Tools > Port. Now We are ready to write our code in Arduino IDE.¬†I have separated the code in to different Arduino tabs for easy¬†understanding so that one may¬†use it easily in it’s¬†own projects. Download the example code from this link. There are four¬†tabs, one is the basic “sample” sketch tab, second tab comprises various functions, and “Config.h” and “Init.h” are two headers tabs which are attached to our main .ino file (Soon I’ll try to write a library). We need to change configuration parameters in “Config.h” tab and modify main “sample” sketch tab according to our project. Do not change any thing in “Functions” and “Init.h” tabs or wherever mentioned in the main sketch.

First of all We’ll include our required libraries in main “sample” sketch tab by ¬† #include <ourLibrary.h>¬†statement. These are the header files for our required libraries.

All the libraries discussed earlier have their particular functions and documentation can be found on their respective links. Now Let’s discuss the “Config.h” tab first which is consisted of ¬†“Connection” and “Database” parameters. All the code in this tab is self explanatory. The variables which we need to store in database in order to preserve their values even after rebooting ESP8266 module are mentioned in database parameters section (We’ll discuss¬†practical aspects of storing the values later).

In the example above, we are storing three different variables “Reboot” which is required for WiFi connection management (We’ll discuss it later and should be left in position one in the array), “12” and “14” are not the integer variables as We’ll be using Json for storing the values in our database. db_array_len¬† is the number of variables, We are going to store in our database i.e. “3” in our example.¬†“Init.h” tab includes various other variables separated in a tab to make our main sketch cleaner and does not not any modification. Now it comes our¬† void setup()¬†function.

In the example above, We are using a “for loop” to declare a pin as output and set it’s state according to value retrieved from database (If a value does not exist, It sets the default value¬†configured in “Database parameters” section in¬†“Config.h” tab). Where¬† db_array[i].toInt()¬†converts the pin which is a “String” type of data to “integer” data type to be used to trigger a pin state. As connecting to WiFi router may takes about 10 seconds and to trigger a pin state according to last saved state just after the booting with out any delay, We’ll connect to WiFi network¬†at the end of void setup()¬†¬†function.

We receive command from our “Server” via MQTT through¬† void callback()¬†function as shown above. Here We’ll use “for loop” again to fetch the complete message and convert into String data. After that message received is passed to ¬† incomming(message)¬†function¬†to perform certain¬†tasks¬†such as “OTA Update” etc. according to the command in message. We’ll discuss all available commands later on. We¬†can also perform our¬†own custom tasks right after the line mentioned above i.e.¬† //add your custom code for incomming message below this line.

Then it comes the standard  void loop()  function where We call keeplive()  function necessary for connection to the WiFi and MQTT server. Burn this sample code to any ESP8266 unit (Any of ESP8266 ESP-12E | F | NodeMCU| WeMos) and your sensor node is ready to go. 


  • Don’t use longer loop more than 20 milliseconds, If necessary it is wise to use delay() or yield() functions
  • Don’t forget to change information in “config.h” tab according to your own situation


3) Making all the stuff talk to each other:

Here it comes the fun part, a real Internet of Things where our main server and sensor nodes talk to each other simultaneously according to the definition of MQTT. Connect for example a Dual Channel Relay to ESP8266 Unit according to the diagram below.


Power up the sensor node and open nodered¬†URL in your computer browser. For example in this IP Address of our Pi is so We’ll put in to our browser address bar and YAHOOO, Welcome to the world of Node-RED¬†:-).¬†Let’s wire the things in node-red.


Adding MQTT Node to Node-Red:

1. Click on mqtt node on the left pan and drag it in to the Flow 1 Window.

node-red-mqtt2. Double click on mqtt node choose¬†“Add new mqtt-broker” and click on “Pencil” icon.

3. In “Connection” tab, enter the “Server” address which is the address of pi i.e. and “Port” 1883.


4. Click “Security” tab and input the user name and password you chose while following this tutorial¬†and click on “Add” button.


5. Input the “MQTT PUBLISH TOPIC” of ¬†our sensor node which is “sample-out” and hit “OK” button.


Deploy | Save the Changes We have made so far:

Every time We make any change to Node-Red such as adding a Node etc. We need to deploy that changes. For this purpose, click on “down arrow” right to the “Deploy” button and choose “Modified Nodes”. By choosing “Modified Nodes” every time when We’ll “Deploy” only that modified node will be affected. After that hit “Deploy” button and a message will pop up with success.


Now our “sample-out” mqtt node is connected to our mqtt server and can receive any message published on this topic. Let’s wire¬†a debug node to mqtt node we already added to our Node-Red and see What happens (Don’t forget to “Deploy” the changes after adding or editing any node). Just reboot your ESP8266 unit and you can see information in debug column i.e. “sample-bootup”.¬†




4) Node Red User Interface (UI):

Scroll down to “dashboard” in left panel and grab the “switch”¬†icon to editor and follow the instruction given on the pictures below.









Now We have successfully added a switch to Node Red UI.¬†Open the Node Red UI URL in the browser i.e.¬†Let’s have a look on our very first control switch¬† ūüėé


And How can I forget, The User Interface in mobile phone browser¬† ūüėČ .


Try this tutorial and Let me know the results, It¬†will¬†be¬†pleasure to help you out if something is not working while following this tutorial. Soon I’ll update the code and it’s user friendly API, stay tuned and good luck¬†8-).

Relatest posts


  1. Reply

    Suggestion required
    I want to control 5 relay switch , three sensor and send data to gateway over the WiFi. so which micro-controller do you think is suitable for this?

    • Reply

      It depends upon type of sensors you want to incorporate in your sensor node setup. However, You may use ESP8266 SMD or ESP8266 based Development boards such as NodeMCU or WeMos but it has only one ADC pin. You can attach ADC-1115, a 16 bit analog to digital converter and much more accurate. Moreover, follow few precautions such as USE GPIO-16 (D0 in NodeMCU) as input (because it may be connected to RST pin and using it as output may cause your sensor node reboot on switching), do not use GPIO-0 (D3 in NodeMCU) as INPUT (because of sensor output, your development board may start in boot-loader mode during boot-up) etc. etc.

      You can also use Arduino UNO or Nano but both of them need further hardware to communicate with your gateway and personally I am not a big fan of using this kind of setup and prefer using ESP-8266 SMD or Development Boards.

      If you are new to IOT world, I recommend you to use Development Boards in place of using SMD or DIP versions of micro-controllers. Good luck for your project.

  2. Reply

    Thanks for replay bro.can you please provide your email for further guidance Thanks

    • Reply

      Contact us anytime at info[@] (Remove[] from address).