Complete Guide to Home Automation

Update: JULY 7, 2018


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 Raspbian Stretch Headless Setup on Raspberry Pi Zero, Zero W, Pi 3 and plug it in to Raspberry Pi (RPi). 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.



Follow Installing secure Mosquitto broker on Raspberry Pi with Raspbian Jessie or Stretch along with systemd start up guide 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 .


Install samba by following our post Password Protected Raspberry Pi File Server – Samba. It will help us in uploading binary files to servers for OTA implementation.


Raspbian Jessie comes with Node-Red and Node.js pre-installed. Let’s install other required software by issuing following commands. Use the following bash script to upgrade node-red. Wait for few minutes to let script complete the various tasks for you.

Enable Node-red service and start the application.

Note: Once you have used this script to upgrade, then you cannot use apt-get to upgrade the pre-installed version of Node-RED. For more information visit official node-red website here.

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:


Install Arduino core for ESP8266 according to ESP8266 Basic Setup and Program using Arduino IDE guide. 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. Open up Examples > ESPMetRED > Examples > Relay sketch and burn it to your ESP8266 for demonstration purpose. For instructions of using ESPMetRED library visit our ESPMetRED an Arduino IDE compatible library to communicate ESP8266 and Node-Red via MQTT guide.


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-).

5 thoughts on “Complete Guide to Home Automation

  1. Pingback: ESPMetRED A blend of ESP8266 MQTT and Node-Red – Hobbytronics Pakistan

  2. Irfan Ali says:

    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?

    • Ahmed says:

      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.

Leave a Reply

Your email address will not be published. Required fields are marked *

For better user experience, please clear your browser cookies and cache at least for once. Dismiss