- Home automation
- Central Control System
- Sensor Node
- Making all the stuff talk to each other
- Node Red User Interface
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:
- Raspberry Pi 3 | Raspberry Pi Zero W
- MicroSD Card (At least 8Gb)
- LAN cable (preferred) / Wireless USB Adapter
- Power supply (2A)
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 [email protected], 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.
First of all, update the system with these commands.
sudo apt-get update sudo apt-get upgrade -y
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.
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 .
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.
bash <(curl -sL https://raw.githubusercontent.com/node-red/raspbian-deb-package/master/resources/update-nodejs-and-nodered)
Enable Node-red service and start the application.
sudo systemctl enable nodered.service sudo systemctl start nodered.service
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.
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.
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:
- ESP8266 (12E/ 12F/ Node MCU/ WeMos NodeMCU)
- 5v Power Supply (1A/ 2A – Depending upon Node Function)
- Arduino IDE with Arduino core for ESP8266
- Drivers for PL2303HX UART cable | CP2102 adapter | NodeMCU | WeMos
- ESP8266WiFi Library
- ESP8266httpUpdate Library
- ESP8266HTTPClient Library
- PubSubClient MQTT Library
- ArduinoJson Library
- FS Library
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 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.
Adding MQTT Node to Node-Red:
1. Click on mqtt node on the left pan and drag it in to the Flow 1 Window.
2. 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.
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. 192.168.0.30:1880/ui. 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-).