Complete Guide to Home Automation

Update: JULY 7, 2018

Contents:


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.

sudo apt-get update
sudo apt-get upgrade -y

 

Mosquitto:

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

Nginx:

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

sudo apt-get install nginx
cd /etc/nginx/sites-available
sudo cp default autohome
sudo ln -s /etc/nginx/sites-available/autohome /etc/ginx/sites-enabled/autohome
sudo rm /etc/nginx/sites-enabled/default
sudo /etc/init.d/nginx reload

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

Samba:

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.

Node-Red:

Raspbian comes with Node-Red and Node.js pre-installed with an outdated version. Update the software by following our guide Raspberry Pi: Node-Red Installation.

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.

cd ~/.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.

npm i node-red-contrib-bigtimer
npm install node-red-dashboard

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

sudo systemctl stop nodered.service
sudo systemctl start nodered.service

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

esp8266-relay-setup_resized

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

node-red-main

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. 192.168.0.30 and “Port” 1883.

node-red-add-mqtt-broker

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

node-red-add-mqtt-broker-security

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

node-red-add-mqtt-broker-subscribe-topic

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.

node-red-deploy-setting

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

node-red-deploy-successful

node-red-debug

 node-red-debug-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.

node-red-debug-switch-add

node-red-switch-add-new-ui-group

node-red-switch-add-new-ui-tab

node-red-switch-save-new-ui

node-red-switch-save-new-ui-group

node-red-switch-save-switch

node-red-mqtt-output-edited-11-sep-16

node-red-switch-complete

Now We have successfully added a switch to Node Red UI. Open the Node Red UI URL in the browser i.e. 192.168.0.30:1880/ui. Let’s have a look on our very first control switch  😎

node-red-ui

And How can I forget, The User Interface in mobile phone browser  😉 .

node-red-ui-mobile

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

0 0 votes
Article Rating
Subscribe
Notify of
guest

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
trackback
ESPMetRED A blend of ESP8266 MQTT and Node-Red – Hobbytronics Pakistan
7 years ago

[…] Add server settings according to this post […]

Irfan Ali
Irfan Ali
7 years ago

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?
Thanks

Irfan ali
Irfan ali
7 years ago

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

Hannan Ahmad
Hannan Ahmad
5 years ago

The home automation system’s main job is to connect controlled devices together with a central hub or so-called digital-brain of the building management system. Different protocols and data flow algorithms involve in this process, and scientists/engineers working hard to refine them, due to which better solutions coming every next day. What we need to do is to leave space for up gradation, when a new system gets installed, because otherwise the whole system will need to be replaced, which can be costlier at times.

6
0
Would love your thoughts, please comment.x
()
x