Overview: Home Automation using Google Firebase & ESP8266
In this project, we will learn how to make IoT Based Home Automation Project using Google Firebase & NodeMCU ESP8266. One of the most common & popular hobby projects you will come across the internet is Home Automation Project. By Home Automation we mean controlling home appliances without a manual switch. When connected with the Internet, home devices are an important constituent of the Internet of Things (“IoT”) Application.
Earlier we learned about Home automation using Blynk Application, WebServer, Amazon Alexa & Arduino IoT cloud and also AWS IoT Core. But in this project we will use Google Firebase. Google Firebase is a Google-backed application development software used for creating, managing,and modifying data generated from any android/IOS application, web services, IoT sensors & Hardware. To learn more about the Google Firebase Console, you can read the official Google Firebase Documentation from Google Firebase
We will design a Home Automation Android Application using MIT APP Inventor. Using the Firebase Host & Authentication Key, we will connect the Android Application with Google Firebase. We will then send the ON/OFF command as 1/0 from App. The data from the App is sent to Google Firebase. Now the Google Firebase data is updated to NodeMCU Board via Internet Connection. Thus the digital GPIO Pins becomes high & low turning ON/OFF the appliances connected to Relay.
Bill of Materials
Following are the components required for making this project. All the components can be easily purchased from Amazon. The component purchase link is given below.
| S.N. | Components Name | Quantity | Purchase Links |
|---|---|---|---|
| 1 | NodeMCU ESP8266 | 1 | Amazon | AliExpress |
| 2 | Relay 5V | 4 | Amazon | AliExpress |
| 3 | 7805 Voltage Regulator IC | 1 | Amazon | AliExpress |
| 4 | Female DC Power Jack DCJ0202 | 1 | Amazon | AliExpress |
| 5 | Diode 1N4007 | 4 | Amazon | AliExpress |
| 6 | Resistor 330-ohm | 5 | Amazon | AliExpress |
| 7 | NPN Transistor BC547 | 4 | Amazon | AliExpress |
| 8 | Terminal Block | 4 | Amazon | AliExpress |
| 9 | LED 5mm Any Color | 1 | Amazon | AliExpress |
| 10 | Female Header | 2 Set | Amazon | AliExpress |
Circuit Diagram & Connection
The circuit diagram for Google Firebase Based Home Automation using ESP8266 is given below. Using this circuit diagram you can assemble the circuit on Breadboard using 4 channel Relay and NodeMCU Board.
Home Automation PCB Gerber File & PCB Ordering Online
If you don’t want to assemble the circuit on breadboard and you want PCB for the project, then here is the PCB for you. The PCB Board for the Home Automation Project is designed using EasyEDA online Circuit Schematics & PCB designing tool. The front side and back side of the PCB is given below.


The Gerber File for the PCB is given below. You can simply download the Gerber File and order the PCB from ALLPCB at 1$ only.
You can use this Gerber file to order high quality PCB for this project. To do that visit the ALLPCB official website by clicking here: https://www.allpcb.com/.
You can now upload the Gerber File by choosing the Quote Now option. From these options, you can choose the Material Type, Dimensions, Quantity, Thickness, Solder Mask Color and other required parameters.
After filling all details, select your country and shipping method. Finally you can place the order.
Setting up Google Firebase Console Database
Now the main thing that we need to do is Setting up Google Firebase Console Database. Once the setup is done, we can then Send Real-Time Data from Android Application to Google Firebase.
But I won’t be explaining here how you can set up the Google Firebase Console Database because I have already explained the entire process in the previous tutorial. You can check the following tutorial to learn how to do the setup.
You can also follow the Video Tutorial below to follow the Setup Work. Once the setup is done you will get the following window.
You need Firebase Host & Authentication Token. These both parameters are required in Arduino Code as well as Firebase App.
Designing & Setting Up Android Application
Now we need to design an Android Application that can send data to Google Firebase. The easiest way to design an Android Application is using MIT App Inventor. The MIT APP Inventor lets you develop applications for Android phones using a web browser and either the connected phone or emulator. The App Inventor servers store your work and help you keep track of your projects.
I have simply designed an Interface for Home Automation project using Google Firebase. The App has 4 pairs on ON/OFF Switch to Send the 0 and 1 command to Google Firebase.
Similarly the block editor which contains assignment and programming information is given below.
While designing the App, you need to enter the Firebase Host & Authentication Token in the App database. Follow the Video instruction below to do so.
Build/Compile the Application and export it to your Android Phone. You can install the APK file on your phone.
The .aia file for this app is given below. You can import this file to MIT App Inventor window and modify it by changing the Host & Token to the Database.
Adding Firebase & JSON Library to Arduino IDE
1. ArduinoJSON Library
So first go to the library manager and search for “JSON” & install the library as shown in the figure below.
Note: The latest JSON library might not work with the code. So you may need to downgrade the library to version v5.13.5
2. Google FirebaseExtended Library
Now you need to install Google Firebase library as well. So, download the library from below link and add it to Library folder after extraction.
Source Code: Home Automation using Google Firebase & ESP8266
After adding the Library to Arduino IDE you can upload the code to the NodeMCU Board.
|
1 2 3 4 |
#define FIREBASE_HOST "**********" // Your Firebase Project URL #define FIREBASE_AUTH "************" // Your Firebase Database Secret #define WIFI_SSID "**************" // your WiFi SSID #define WIFI_PASSWORD "********" // your WiFi PASSWORD |
But before uploading the code make changes to the WiFi SSID, Password, Firebase Host & Authentication Token.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
#include <ESP8266WiFi.h> #include <FirebaseArduino.h> #define FIREBASE_HOST "**********" // Your Firebase Project URL #define FIREBASE_AUTH "************" // Your Firebase Database Secret #define WIFI_SSID "**************" // your WiFi SSID #define WIFI_PASSWORD "********" // your WiFi PASSWORD #define Relay1 12 //D6 int value1; #define Relay2 14 //D2 int value2; #define Relay3 4 //D1 int value3; #define Relay4 5 //D5 int value4; void setup() { Serial.begin(115200); 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.setInt("S1",0); Firebase.setInt("S2",0); Firebase.setInt("S3",0); Firebase.setInt("S4",0); } void firebasereconnect() { Serial.println("Trying to reconnect"); Firebase.begin(FIREBASE_HOST); } void loop() { if (Firebase.failed()) { Serial.print("setting number failed:"); Serial.println(Firebase.error()); firebasereconnect(); return; } value1=Firebase.getString("S1").toInt(); //Reading the Status of Variable 1 from the firebase if(value1==1) { digitalWrite(Relay1,LOW); Serial.println("Relay 1 ON"); } else if(value1==0) { digitalWrite(Relay1,HIGH); Serial.println("Relay 1 OFF"); } value2=Firebase.getString("S2").toInt(); //Reading the Status of Variable 2 from the firebase if(value2==1) { digitalWrite(Relay2,LOW); Serial.println("Relay 2 ON"); } else if(value2==0) { digitalWrite(Relay2,HIGH); Serial.println("Relay 2 OFF"); } value3=Firebase.getString("S3").toInt(); //Reading the Status of Variable 3 from the firebase if(value3==1) { digitalWrite(Relay3,LOW); Serial.println("Relay 3 ON"); } else if(value3==0) { digitalWrite(Relay3,HIGH); Serial.println("Relay 3 OFF"); } value4=Firebase.getString("S4").toInt(); //Reading the Status of Variable 4 from the firebase if(value4==1) { digitalWrite(Relay4,LOW); Serial.println("Relay 4 ON"); } else if(value4==0) { digitalWrite(Relay4,HIGH); Serial.println("Relay 4 OFF"); } } |
Home Automation using Google Firebase & ESP8266
After uploading the code the NodeMCU will connect to the WiFi Network. Using the Firebase Authentication Token & Host the Android App as well as Nodemcu Board connects to the Google Firebase.
Now you can send ON/OFF Command from Mobile Application to turn ON/OFF the home appliances.















12 Comments
I have a very similar project at home but this one is dependent on an NTP server for the timer. It is set up to turn off/on lights by using relays also at a predetermined time of day and night. The only problem is after a power failure, the esp8266 gpio04 pin is reset to default state. Any idea how to prevent this in the software programming side, asside from privision of battery power supply. Thanks in advance.
Sir we used JSON 5.x library at place of JSON 6.x library. But it’s not working then what problem is there??
‘WiFi’ was not declared in this scope
please help
Trying to reconnect
setting number failed
my ESP8266 is not connecting please help
pls solve fingerprint problem
Bro there is some issues is this code.
I have one updated code if you want then tell me i will give you.
I am getting error like “no matching function for call to ‘FirebaseESP8266::setInt(const char[3], int)
if u have any code that is working fine, can you give it to me.
[email protected] this is my mail id. mail me on this number i will send you my code
I have sent an email to you
have you got any solution for this issue???
Yaa i have one….
please any one help me my code not connecting with firebase this same code