Building my smart home
Tags: android, ios, javascript, reactnative
Published: 2019-10-04 10:26:58

In this article that is focused on the entire public, I will explain experiences, anecdotes about a personal project of how I turned my home into smart and how can I also control the different electronic devices, enter in home by phone with a simple gesture or open the building door using a Siri voice command.

Basically the idea was started with an Arduino kit that I had purchased and I had started to fiddle with the different modules joysticks, relays, motors, displays, etc .. So I decided to turn my home into a smart and control it from the phone. Although it could also be integrated with GoogleHome / HomePod, but for the moment I postponed it for another phase.

These have been the objectives of the project that I set before starting:

* Power on/off, control the intensity and program the house lights as well the plugs.
* Measure the indoor and outdoor temperature to regulate it.
* Opening the building door with voice commands via Siri.
* Opening the home door via fingerprint and detect its closure. In addition add a button to facilitate opening from inside.
* Notification system and historical records of all door activity
* Installation of several security measures, to guarantee the integrity of the home.

The advantage of opening the home door from the mobile device apart from providing access and control of who enters and leaves, increases security since many families don’t usually lock the door, just close it using the latch. So it makes the work of thieves more easy and we don’t want that.

To do this, I used the Rasberry Pi a small single-board computer where you would end up connecting and controlling all the devices in the home that you would like to add through the Domoticz automation system.

In the market there are several options when we talk about smart devices of many brands and for all pockets. From a light bulb to cameras, thermostats, refrigerators, etc. At the end anything no matter how small, that can be connected to WiFi/bluetooth is called an IoT device.

![IoT device types](https://thepracticaldev.s3.amazonaws.com/i/jbbpornj94v69dvawiq2.png)

However, apart from acquiring some and installing them, they can also be created by oneself depending on the needs we have. In my case I used the ESP8266 chips together with the relay module powered at 5V for example to turn on/off the electrical circuit of a mini water fountain.

Moreover for all the control home temperature, the Node MCU chip has been used for taking measurements every X time interval updating data to Domoticz, which would be able to start up the heating system if is necessary. Also I decided to add a display to visualize temperature and time.

![On the left the module ESP8266 and on the right the Node MCU](https://thepracticaldev.s3.amazonaws.com/i/82kb5r2gqqfeo4w5f4xm.png)

## Dashboar

Once all the devices are configured, this would be the Domoticz interface, also accessible from the mobile. Basically from here we can control all connected devices, add, delete and much more. Accessing of course, only from the local network.

In addition we can program each device and add additional logic, for example when it should turn on, off, with specific intensity. Depends on our case we may be interested in one state or another.

![Desktop dashboard](https://thepracticaldev.s3.amazonaws.com/i/1krnx0ijrozx8pvqff56.png)

In my case as you can see, I have relevant information about the Raspberry Pi, the temperature outside and inside the house. Also some plugs, lights from different rooms or areas of the house.

Also the possibility of enabling/disabling the door mechanism, and some other options.

## 3D Modeling and Printing

![Sketch](https://thepracticaldev.s3.amazonaws.com/i/4mo9v35mjnwd0oetphtt.png)

To open the home door, a signal that arrives is not enough. That signal has to launch an action, in this case turn the key, an action that we perform daily. Although I saw some “smart lock” mechanisms, but none fitted with the type of my door.

Moreover who wants a battery-based system? We wouldn’t like him to run out of battery with us outside the home. So the system should be directly connected to the power and only in case of emergency be powered by an electric accumulator.

For do this, it was necessary to begin to take measures and build something from scratch that made it possible to turn the key, taking into account that it is a reinforced door.

After making the plastic structure and the handlebar, I began to design the sustenance of the servo motor. My main intention was to split the key in 2 parts to make all the support more compact, but when I couldn’t do it, I decided to leave the idea. There was also the possibility of making a gear system but taking into account the durability that this required and the turning force I decided to opt for something more simple. I started to design the 3D pieces and then print them and after some readjusting this is the final result.

![Assembly of all the components, in red it would be other parts like the servo and the key](https://thepracticaldev.s3.amazonaws.com/i/rfsh5bqn3fs53askh7b8.png)

These components are accessible and can be downloaded from the website thingiverse.com (website dedicated to the sharing of user-created digital design files).

## Assemble

The assemble may take a long time but we don’t want to see the cables hanging because apart from being unsightly can cause greater consequences such as short circuits. To ensure that doesn’t happen, we have to use a property cable holding.

This would be the result in my case, trying to make the cables barely notice even though they travel through the door frame until they reach the lock.

![On the left we can see the intercom that is connected to the Raspberry Pi. And on the right the servo motor hold by the piece printed in 3D.](https://thepracticaldev.s3.amazonaws.com/i/0foiuadazlc2xqnrxdud.png)

At the same time, a button can be seen halfway up the door, it is the DashButton he has the responsibility to open the door from inside in a very simple way.


## Electrical Circuit

![Diagram of connection of the different components](https://thepracticaldev.s3.amazonaws.com/i/bqwih8i0l7v0pdmo3o9h.png)

Once seen the assemble part I will explain the components that form it, as well as the connection with the Raspberry Pi. To be able to build it, I perform different proof of concept on a small scale, testing different configurations before making the final assembly.

This circuit could be perfectly soldered instead of a breadboard, although it can always be done at another step.

These are the different components used for its construction:

* 1x Servo Motor: Whose function is to turn the key to open and close the door.
* 1x Power supply: 9V DC current supplied to the servo motor for proper operation.
* 1x Magnetic Switch: Detects when the door closes and send the command to the servo to turn the key.
* 2x Relays: One responsible for closing the intercom button and the other to power the servo only when it required.
* 2x Transistors NPN: Solving common Raspberry Pi problem of stepping up 
* 3.3 GPIO to 5V for power the relay.

This sketch is accessible and can be downloaded from the website fritzing.com (open-source hardware initiative that makes electronics accessible as a creative material for anyone).

## Mobile Application

![The different parts of the application.](https://thepracticaldev.s3.amazonaws.com/i/kltpnlaufkm1zt4ht3ke.png)

To maximize the security for all entire engine, I decided to create an application where all the information passed through my own server, in order to register and control all the traffic, as well the access, instead of using Domoticz for this critical part. I used React-Native framework for this development.

The application is not public and is completely for internal use, consisting of the following functionalities:

* Fingerprint scanner when trying to launch an action
* Add Siri shortcut command only available for the building door
* Notifications and alerts system

## Demo

The door opening engine has gone through several phases throughout the development from alpha versions, to the current one, paying special attention to security and giving stability to work properly. Since we don’t want the engine to stop responding or fail. So today and with all the members of the family using daily the engine has been functioning correctly for more than a month.

Here is the demo of how it works:

[![Demo prototype door engine](https://thepracticaldev.s3.amazonaws.com/i/ae3ph5lsrf2lsja3d020.png)](https://www.youtube.com/watch?v=_gGbyX5ESdY "Demo prototype door engine")

Basically the intercom system keeps the building door opened for a few seconds after receiving the action sended from Siri command, enough to be able to enter from the outside. This action is received by the Raspberry Pi that closes the intercom system button circuit through the relay, executing the same logic as if we had pressed it.

On the other hand, the action of opening the reinforced door can only be released by fingerprint and the door will remain open until the magnetic switch detects the closure of the door, as can be seen.

## Technologies and libraries

In terms of technologies used in the project, the server and the Raspberry Pi running in nodeJS. Using SSE (Server Send Events) for such communication, together with AWS Lambdas for the execution of different actions.

Without forgetting the PM2 (Process Manager2) production runtime and process manager to allow to keep applications alive forever, to reload them without downtime and facilitate common DevOps tasks.

To control the GPIO (General Purpose Input Output) have used node library pigpio, a wrapper for the pigpio C library. Although other alternatives like johnny-five used for IoT and Robotics programming.

The mobile application use a specific npm libraries for the following features:

* Fingerprint scanner feature: https://www.npmjs.com/package/react-native-fingerprint-scanner
* Siri commands feature: https://www.npmjs.com/package/react-native-siri-shortcut

The driver control of the different microchips used for ESP8266 and Node MCU in the assembly, as well as the different device modules connected to them, such as the temperature and humidity sensor (DTH11), 4-digit display (TM1637), relay module and others, have used with specific libraries recommended by the manufacturer that some of them can be found here.


## Software

To build the different parts of the project such as the system of monitoring and devices controller, 3D modeling and printing, the design and programming the circuit, I have been used different third-party software like:

* Automation System: Domoticz
* Electronic prototyping: Arduino, Fritzing
* 3D Printing: Ultimaker Cura, Simplify3D
* 3D Modeling: Fusion360, SketchUp


## Summary

![Summary](https://thepracticaldev.s3.amazonaws.com/i/p3tad6b2xbh0f9hoqjkj.jpg)

Nowadays more and more people talking about smart home, companies are involve their efforts to conquer a growing market and makes people life easier. For this purpose they design and sell many kinds of devices and kits like sensors, controllers, lights, switches and more .. So everyone could make their own smart home and control it by their phone, voice and gestures.

Think about having the possibility of scheduling the temperature before arriving at home, being able to monitor your home through an installed security camera, controlling the different electronic devices by voice commands, lifting the blinds at the morning automatically and why not, enter in your home without keys only from mobile phone.

Although the possibilities are endless as one wants. The technology is available to be used to facilitate our lives.

Support me:

[![Buy me a tea](https://thepracticaldev.s3.amazonaws.com/i/keul6rqj3dbgqdoj0kea.png)](https://www.buymeacoffee.com/vmadalin "Buy me a tea")


Thanks for your time and reading.

V.Madalin