My Own DIY Home Automation System
- Arjun Preetham
- Apr 2, 2019
- 7 min read
Updated: Apr 15, 2019
As a young boy who loved Iron Man, I've always wanted to build my own home automation setup.
This was back in 2010. Not being equipped with the knowledge of how to execute the task in front of me, I got to work. It took 8 years of hard work aided by my natural curiosity of how these electrical systems work, to accomplish what I have today. During this process I was introduced to the world of Arduino and Android App Development. Seeing how user friendly it was and discovering the amount of documentation available, I knew that my task could be easily accomplished.
That being said, let's get started.
Objective
My end goal was to control my bedroom lights with my phone. But you may ask, why? Well, it was simply cool to do so. :) And because I felt little bit like Iron Man :P
Design & Thinking Process
From a user's perspective, there are a few factors that must really stand out for me personally. They are:
User friendly interaction with the system
Ease of integration
Safety
Reliability
Scalability
Responsiveness
Now, let me give you a brief explanation as to why these factors are paramount,
- User Friendly Interaction With The System:
I think it's safe to say that people will use things/tools/gadgets that are easy to use simply because the learning curve is almost non-existent and also because the human race loves convenience.
- Ease of Integration:
Once the technology is created, its integration with the larger system (in this case, the electrical system of my house) must be easy, flawless and it must not affect the functionality of the larger system.
- Safety:
This factor is pretty self-explanatory. Whatever a person builds/invents must not cause any harm to its user or to its environment.
- Reliability:
The systems/tools/gadgets a person builds must be reliable all the time because there might be situations where you really need to use a system but at that moment if it's not available, then what's the point of it? Was it really worth all that money you spent on it? We truly value something in a situation which demands its use and the system/gadget/tool comes through and serves its purpose.
- Scalability:
Any device/system which is added to a larger parent system should not decrease the performance of the larger system but increase it. This is scalability.
- Responsiveness:
Responsive systems are always preferred because I feel that it gives the user what they want with the least time. This applies to the app too.
Keeping these factors in mind, one part of the system was designed and also influenced the technical design of the system.
The technical design on the software side of things was made as simple as possible in terms of flow of data between the app and the control board. On the hardware side, components and microprocessors were selected in such a way that they could reliably perform once the code was uploaded to it.
The System
The system consists of 4 components, i.e, the Android app, the server, the control board and a WiFi router. WiFi is the medium through which the other 3 components talk to each other. The number of control boards can vary but there obviously has to be a minimum of 1 board for this system to work.
Here, the Android app, the server and the control board are the major components of this system.
Let's begin by defining the function of each of the major components,
Android App: The app acts as an interface between the user and the home automation system. It was coded in Java using Android Studio.

Right now, only the "CONTROL" and "SWITCHES" section of app is functional. More functionality will be added as I make new devices. The app connects to the server via WiFi and communications take place through socket.io.
Server: The server is a Raspberry Pi 3 running a NodeJS file which listens to messages from the control board and app and takes actions based on those messages. This device has a fixed IP address so that it can the app knows how to the reach the server all the time. This is basically a middleman that forwards messages from the app to control board and vice versa.

Control Board: The control board's function is to turn on/off AC loads within 15A. This can limit can be changed based on the type on the specification of the electromechanical relays begin used. This board connects to the server via WiFi and communication takes place via MQTT (Message Queuing Telemetry Transport). The board was coded in C# using the Arduino IDE.

Now, let's start off with how the software functions.
Process Flow Diagram
The following process flow diagram depicts what happens when the user pressed a button on the app to turn on a switch.

Connection Monitoring
1, App - Server:
The socket.io library used has in-built listeners to check for the states of connection, reconnection and disconnection, And whenever this listeners are called, connection state updates are sent to the dashboard (screenshot shown above) and subtle UI messages are displayed to notify the user of the connection state.

2. App - Control Board:
MQTT has something called a "Last Will" message, which is a custom message that is sent to the server when the connection is between the server and MQTT client (control board) is terminated. When this message is received, it is relayed to the app via socket.io. This message is understood by the app and sends a request for the UI of the dashboard to be updated.

Notice the red dot on the top right corner of the Sentinel card. This indicates that the control board is offline.
This pretty much sums up the software part. Moving on to the hardware.
Control Board Schematic & PCB Design
A prototype of the circuit was designed and tested out manually using a Wemos D1 Mini, Relay Module, 2N2222 transistors and 4700 Ohm resistors. Once the circuit was tested and verified, I used AutoDesk's EagleCAD to design the PCB for the control board.
Schematic:

Disclaimer: Some of the connections had to be changed in this diagram to obtain the PCB connects I wanted.
PCB Design:

Working Of The Control Board
Let's split the working into 2 parts, i.e, conversion of AC to DC and control of the relays using the ESP-12E microcontroller.
1. AC to DC:
I'm going to be honest, I don't know much about converting high voltage AC to low voltage DC. Keeping safety in mind, I didn't want to risk the safety of my family and house by designing my own circuit for the first time. So I turned to already available solutions in the market and found out that the Hi-Link HLK PM01 was a perfect fit for my project.
So the the HLK PM01 is powered using using AC and it's output is 5V 0.6A DC. This 5V is then stepped down to 3.3V using a LDO. The output of this LDO (3.3V) goes directly to the ESP-12E, to power it.
2. Relay control using ESP-12E:
Here, I used two 2N2222 transistors connected to the ESP-12E to control the on/off state of the 2 relays along with four 4.7k Ohm resistors (a pair for each relay). The base current for the transistors was supplied from the microcontroller's GPIO pins 4 and 5. For more detail, refer to the circuit diagram shown above.
The 1N4007 power diodes are used to suppress any back current generated by the current flowing through the relays. This is done to protect the DC side of the circuit.
Components Used
Here's a list of components used to make the control board,
ESP-12E x 1
Single sided copper clad board x 1
PCB Etching Solution x 1
Hi-Link HLK PM01 x 1
1N4007 Power Diodes x 2
5V Relay x 2 (Choose current rating according to your application)
Male Header Pins x 6
5V to 3.3V LDO x 1
2 Block Screw Terminals x 2
2N2222 SMD Transistors x 2
4700 Ohm SMD Resistors x 4
The ESP-12E was used mainly because of it's in-built WiFi capabilities and the available MQTT libraries.
PCB Manufacturing
To manufacture the PCBs, I employed a method known as Toner Transfer method.
For this you will require the circuit design to be printed on magazine paper or 130 Photo Glossy Paper using a LASER PRINTER. For this to work, you must use a laser printer!
Let's get started.
1. First, cut out the circuit diagram from the glossy paper and sketch the dimensions onto the copper clad board. Cut the board along the lines.

2. Sand the copper board to remove any oxidized copper from the surface. I used 120 grit sand paper. You can use any grade of sand paper.

3. Place the circuit diagram over the sanded copper board and iron it using an iron/laminator.


Make sure the paper sticks onto the copper board properly.
4. Place the board under running water and using your thumb, gently remove the paper from the copper so that you get a board as shown on the right.


If the toner transfer didn't happen properly, you can always remove the toner from the copper board using a piece of cotton dipped in acetone/nail polish remover.
5. Create the etching solution by mixing ferrous chloride and water in the ratio of 1:2 respectively. Place the copper board into the solution and remove when all exposed copper has been etched. This process takes 2-3 hrs.


6. After the board has been etched, use a cotton dipped in acetone/nail polish remover to remove the toner from the copper board. Your PCB is now ready for use!


This is what your board should finally look like!
Disclaimer: I over etched my board, so there are a few traces missing from the board. Please monitor the process continuously when you etch your own boards!
Assembly
Now that the PCB is ready,
1. Use a 1 - 2mm drill bit to drill holes within the green circles (refer to PCB design picture) for the through hole components.
2. Place the through hole components at their respective positions (take precaution while placing the 1N4007 power diodes and the 5V to 3.3V LDO, refer to the schematic if in doubt) and solder.
3. Place the resistors and transistors on the contacts and solder.

Blue rectangles: resistors must be placed and soldered.
Yellow polygon: transistors must be placed and soldered.
4. Finally, place the ESP-12E on the board as solder it.
That's it! The control board is ready to be programmed.
The end result,


Uploading Code
The board is programmed with the help of an Arduino Uno.

Make the connections as shown above.
TX (board) -> TX Uno
RX (board) -> RX Uno
RESET Uno -> GND Uno
Open up the code in the arduino IDE and select the the ESP-12E board in the board manager and hit upload.
With respect to the wiring, the screw terminal block in front of the HLK PM01 block must be used to connect the live and neutral wires. The other screw terminals must be used to connect the load lines.
Once the control board is wired up and the mains are turned on, the board should connect to your WiFi and you should get a notification on your phone once the app is opened and it connects to the server.
Result
All the code and hardware have been tested successfully over a period of 2 months without any safety issues. The objective of this project was met, I can now turn on/off my bedroom lights with my phone! :)
Comments