Introduction
Home automation is revolutionizing the way we interact with our living spaces, bringing convenience, security, and efficiency into our daily lives. In this article, we guide you through creating a sophisticated home automation system using Google Firebase and the NodeMCU microcontroller.
Google Firebase is a robust platform for building mobile and web applications, offering real-time database capabilities, seamless cloud storage, and powerful machine learning features. By combining Firebase with the NodeMCU, a low-cost, open-source IoT platform based on the ESP8266 Wi-Fi module, you can develop a highly responsive and scalable home automation system.
We demonstrate how to connect and control various household appliances and devices using Firebase and NodeMCU. This provides a practical and flexible solution for modern smart homes. Whether you’re a hobbyist, a student, or an enthusiast looking to explore the world of home automation, this article equips you with the essential knowledge and tools to get started.
Setting Google Firebase Database
Setting up a Google Firebase Database in the Firebase Console is straightforward. Follow these steps to get started:
Step 1: Create a Firebase Project
Go to the Firebase Console:
- Visit the Firebase Console.
- Log in with your Google account if prompted.
Add a New Project:
- Click on the “Add project” button.
- Enter your project name and click “Continue”.
- You can choose to enable Google Analytics for your project (optional). If you do, configure your analytics settings and click “Create project”.
- Wait for Firebase to Set Up Your Project:
Firebase will set up your project, which may take a few seconds. Once done, click “Continue” to be redirected to your project’s dashboard.
Step 2: Set Up Firebase Database
Navigate to the Database Section:
- In the Firebase Console, click on “Build” in the left-hand menu.
- Select “Realtime Database” or “Firestore Database” based on your preference. For this example, we’ll use Realtime Database.
Create a Database:
- Click on the “Create Database” button.
- Choose your database location and click “Next”.
- Select the security rules for your database. You can start in “Locked mode” for higher security or “Test mode” for easier development access. Click “Enable” to create your database.
Android App using MIT App Inventor
- Go to MIT app inventor2 (HERE)
- Login using your Google account.
- Please follow the video tutorial to know how to create an android project and design your android app.
Hardware Requirements
Build your own 4 channels relay board
- 5v Relay – 4nos
- BC547 npn transistor – 4nos
- 1K resistor – 8nos
- 1N4007 diode – 4nos
- 3mm RED LED – 4nos
- 2-2 Terminal – 5nos
Build your own External Power supply
- Center tap 9-0-9 transformer
- 1N4007 diodes – 4 nos.
- 1000uF and 220uF capacitor 35v – 1no each
- 1K resistor – 2nos
- 5mm RED LED – 2nos
- 7805 and 1117 voltage regulators. – 1no each
Circuit Diagram for External Power Supply

Software Requirements
- Arduino IDE.
- Arduino Firebase Library (HERE).
- ArduinoJson Library (Download it from the IDE steps given below)
- HOST and AUTH details from Google Firebase.
- Android app using MIT app inventor2 (Download aia file HERE).
We also require the ArduinoJson Library in the most recent upgrades to the Firebase Arduino library. Follow these instructions to download the ArduinoJson library.
- Open Arduino IDE
- Click Sketch — Click Include Library –– Click Manage Libraries and search ArduinoJson on the top search bar. Select version 5.13.5 otherwise it will give an error.

Block Diagram

Android app to Google Firebase: The block diagram shows how the Android app will communicate with Google Firebase in order to establish a variable against the pressed button and save its initial value of 0. So, if you were to click all four buttons at once, four variables—FB1, FB2, FB3, and FB4—would be created, each with a starting value of 0.
NodeMCU to Google Firebase: Using the same valid HOST name and AUTH key, NodeMCU will now retrieve the same variable data from Google Firebase. Consequently, it will activate the relay that is linked to the appropriate pins.
Circuit diagram

Source Code
#include <ESP8266WiFi.h>
#include<FirebaseArduino.h>
#define FIREBASE_HOST "smarthome-52bcf.firebaseio.com" //Your Firebase Project URL goes here without "http:" , "\" and "/"
#define FIREBASE_AUTH "e7gJrjWWVsPlblm7uuhaC7ib3PNt34H3dB4W9iwL" //Your Firebase Database Secret goes here
#define WIFI_SSID "TP-LINK_FDBA" //your WiFi SSID for which yout NodeMCU connects
#define WIFI_PASSWORD "jaishrikrishna~12" //Password of your wifi network
#define Relay1 14 //D5
#define Relay2 12 //D6
#define Relay3 13 //D7
#define Relay4 15 //D8
int rel1,rel2,rel3,rel4;
void setup()
{
Serial.begin(115200); // Set the baud rate 115200 in Serial Monitor to display debug messages
pinMode(Relay1,OUTPUT);
pinMode(Relay2,OUTPUT);
pinMode(Relay3,OUTPUT);
pinMode(Relay4,OUTPUT);
digitalWrite(Relay1,HIGH);
digitalWrite(Relay2,HIGH);
digitalWrite(Relay3,HIGH);
digitalWrite(Relay4,HIGH);
WiFi.begin(WIFI_SSID,WIFI_PASSWORD);
Serial.print("connecting");
while (WiFi.status()!=WL_CONNECTED){
Serial.print(".");
delay(500);
}
Serial.println();
Serial.print("connected:");
Serial.println(WiFi.localIP());
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
//Here the varialbe"FB1","FB2","FB3" and "FB4" needs to be the one which is used in our Firebase and MIT App Inventor
Firebase.setInt("FB1",0);
Firebase.setInt("FB2",0);
Firebase.setInt("FB3",0);
Firebase.setInt("FB4",0);
}
void firebasereconnect()
{
Serial.println("Trying to reconnect");
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // initialize the firebase with HOST name anf AUTH key
}
void loop()
{
if (Firebase.failed())
{
Serial.print("setting number failed:");
Serial.println(Firebase.error());
firebasereconnect();
return;
}
rel1=Firebase.getString("FB1").toInt(); //Reading the value of the varialble Status from the firebase
rel2=Firebase.getString("FB2").toInt(); //Reading the value of the varialble Status from the firebase
rel3=Firebase.getString("FB3").toInt(); //Reading the value of the varialble Status from the firebase
rel4=Firebase.getString("FB4").toInt(); //Reading the value of the varialble Status from the firebase
if(rel1==1) // If, the Status is 1, turn on the Relay1
{
digitalWrite(Relay1,LOW);
Serial.println("Relay 1 ON");
}
if(rel1==0) // If, the Status is 0, turn Off the Relay1
{
digitalWrite(Relay1,HIGH);
Serial.println("Relay 1 OFF");
}
if(rel2==1) // If, the Status is 1, turn on the Relay2
{
digitalWrite(Relay2,LOW);
Serial.println("Relay 2 ON");
}
if(rel2==0) // If, the Status is 0, turn Off the Relay2
{
digitalWrite(Relay2,HIGH);
Serial.println("Relay 2 OFF");
}
if(rel3==1) // If, the Status is 1, turn on the Relay3
{
digitalWrite(Relay3,LOW);
Serial.println("Relay 3 ON");
}
if(rel3==0) // If, the Status is 0, turn Off the Relay3
{
digitalWrite(Relay3,HIGH);
Serial.println("Relay 3 OFF");
}
if(rel4==1) // If, the Status is 1, turn on the Relay4
{
digitalWrite(Relay4,LOW);
Serial.println("Relay 4 ON");
}
if(rel4==0) // If, the Status is 0, turn Off the Relay4
{
digitalWrite(Relay4,HIGH);
Serial.println("Relay 4 OFF");
}
}