Make A Smart Home From Home

   I was completely into hardware development for the past couple of years. My first IoT project was a smart switch board, which depends completely on third party applications such as Blynk and Adafruit.io. As part of my career I started learning software development and I understood that such an application is not a big task in the perspective of a software developer.

   The ultimate aim was to make an IoT platform for the automation, monitoring and controlling of electrical and electronic devices over the internet in real time. The project comprises of a flutter mobile application, React web application and Firebase services as the backend. The hardware section is driven by an esp (Espressif) Wifi development board.

   For better convenience I am dividing the entire project into three blocks Back-end, Front-end and Hardware. The Backend is authenticated with firebase authentication and for the real time communication I used firebase realtime database. The Application (both mobile and web) can consume and update the database. The hardware section is also configured to listen for any changes in the database and synchronise the state of devices connected to it.

4 Terminal IoT Device for controlling electrical appliances

Back-end

I configured Firebase authentication service and real-time database from the google developer console. Then I created a database structure like this.

This database structure satisfies only basic requirements. The device id (12,13,14,15) in the above picture represents the digital pin number of the wifi module at the hardware section. Identifying the connected devices with the hardware pin is more convenient than identifying by its name – even if the name of the connected device changes, the pin number remains the same. I linked the database to the front-end applications and created the logic for making a new branch in the database with the user id for every new user login.

Database Rule

{"rules":
 {"users":
   {"$uid": 
     {".write": "$uid === auth.uid",".read": "$uid === auth.uid"}
    }
  }
}

Then I defined a database rule so that, only authorised users can control the devices.

Front-End

The Front-End Application development is a different topic not relevant in this blog. I am not including more descriptions about the front-end application development. I created an application with these basic functionalities:

  1. Authenticate a user with Firebase authentication service (Email and password signup)
  2. Consume the data from database and display the status of devices in real time
  3. Create a branch for the user in the database with his user id after successful login
  4. Create different sections and add devices to the sections
  5. Edit and Delete the Sections/Devices
  6. Update the state of devices according to the user input


Documentations for Firebase authentication and real-time database:-
https://firebase.google.com/docs/auth
https://firebase.google.com/docs/database

App Screenshots:

Hardware

I used an esp8266 development board for the hardware. It is a highly integrated low cost WiFi development board which can be programmed in the famous Arduino ide. The ESP8266EX micro controller integrates a Tensilica L106 32-bit RISC processor, which achieves extra-low power consumption and reaches a maximum clock speed of 160MHz. The Real-Time Operating System (RTOS) and WiFi stack allow about 80% of the processing power to be available for user application programming and development. The device operates in 3.3v and maximum output voltage is 3.3v. So for controlling electronic components such as a relay which works in (5v to 12v), additional circuitry for stepping up the voltage is required.

Circuit Diagram

Node MCU Pin out Diagram

Components Required

Circuit Explanation

The 230v AC supplied to the board is converted to 5v DC with AC-DC converter and supplied to the esp8266 development board. Digital Pins 12,13,14,15 (D6,D7,D5,D8) are connected to the Base of the transistors through a 10k resistor. Emitter of the transistors are connected to the ground and collector of each transistor to the pin number 5 of each relay. Pin number 2 of all relays are connected to the 5v input (VCC). Electrical appliances can be connected to the screw terminal just like wiring a simple switch:- Connect the Neutral to the common neutral port and phase to any of the 4 output terminals. Whenever the micro controller update the state of digital pin connected to any of the transistors, the relay connected to that transistor will complete the circuit and the phase will get connected to the external device.


Programming the Development Board

1) Download and install Arduino ide:- ​ https://www.arduino.cc/en/Main/Software

2) Add the esp8266 boards manager to the arduino ide
● Start Arduino and open the Preferences window.
● Enter https://arduino.esp8266.com/stable/package_esp8266com_index.json into
  Additional Board Manager URLs field. You can add multiple URLs, separating
  them with commas.
● Open Boards Manager from Tools > Board menu and find esp8266 platform.
● Select the version you need from a drop-down box.
● Click the install button.
● Don’t forget to select your ESP8266 board from Tools > Board menu after
  installation.

3) Connect the development board to the pc and select the connected port from the
Arduino ide

4) Clone or Download the files from git:- https://gitlab.com/arunneoito/smart-home.git

5) Copy the libraries to your Arduino/libraries folder

6) Open the .ino file in Arduino ide

7) Replace the Firebase Credentials, WiFi ssid and password with yours

8) Upload the code


Configuring the device
Programming the board every time for changing the section name or the user id is not an easy task. So I created the logic for switching between two modes. The device can work in 2 different modes—one for configuration and one for normal working. For configuring the device, restart the device while pressing the button attached to the pin number 4 (D2), when the built-in LED starts to blink release the button. The device will create a new access point with credentials provided in step 5. Connect to the access point, open a browser and type the ip address 192.168.4.1. A web page with fields to enter the configuration details will be provided. Enter the following details.

  1. User Id:- generated while login
  2. Section Name:- Name of section created from the application
  3. WiFi SSID & PASSWORD:- The SSID and PASSWORD of the WiFi network to which
    the device should be connected

Press Submit. Restart the device without pressing the button. You are ready to go.

Subscribe to newsletter
Need more tech news? Tune in to our weekly newsletter to get the latest updates