Send Real-Time Sensor Data to Google Firebase with ESP8266

In this blog, I will show you,

  • How to Create the project on the firebase.
  • how to write/update the data to the real-time database on the firebase.

Now, let’s get started and understand how to do it. Firstly let’s create the project on firebase.

Creating a project on Firebase

Firstly, go to https://firebase.google.com/ and sign in to get started. After that click on “Go to console” as shown here,

After that, you will see all the project if you have already created before but if you are creating it for the first time then it will redirect you to create a project but if it doesn’t redirect you automatically the click on “Add project” as shown below,

Then, it will ask you to enter the project name which should be unique. Also, it will suggest the names if the name provided by you is not available.

Then, on the next page it will ask you to enable for “Google Analytics for your Firebase project”, enable it and press on “continue”.

After that, it will ask you to select the account. Select the account you are using and then click on “Create project”.

Then, it will create a project for you with in a minute.

So, the project is ready and now you can create your real-time database to read/update/write the data to the database. Now click on “Database”, which will open a new webpage for you and on to that webpage click on “Create database”.

It will open a pop up window for you and in that window select “Start in test mode” and then click on “Next”.

Then on the next page click on “Done” and your real-time database is ready for use.

Now select “Realtime Database”,

Now you can write the code for NodeMCU in Arduino IDE for reading/writing/updating the database.

You can also check this for more clarification,

Now, we have setup the real-time database on firebase. So, it’s time to write the code for the integration of Firebase with NodeMCU.

Writing/Updating the data to Firebase

Once you are ready with the project on firebase with a realtime database. Now we can go ahead and write the code that needs to be uploaded to the Nodemcu to write/update the data to the realtime database on firebase. Before that one more thing you have to set up, that is, to interface any input device/component with the NodeMCU whose value you are going to upload to the database. In our case, we will be using the IR(Infrared) sensor to read the signal and to further update it. So let’s first interface the NodeMCU with the IR sensor.

Interfacing IR sensor to NodeMCU
Now you have to interface NodeMCU with IR sensor as shown below in the circuit diagram.

Circuit Connections:

NodeMCU PinsIR Sensor Pins
D1OUT
VINVCC(+)
GNDGND(-)

Once you are done with the circuit interface. We can move ahead to write the program as per the circuit interfaced.

Firstly, open Arduino IDE and follow the path
Sketch > Include Library > Manage Libraries…
and search for “Arduino JSON” and install it as shown here.

After, the installation check out this link to download FirebaseArduino

Download Firebase-Arduino library

Now, add the downloaded zip file to Arduino by following this path
Sketch > Include Library > Add .ZIP Library…
this will add the FirebaseArduino library to the Arduino path.
Now write the code given below:


#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>

#define FIREBASE_HOST "******************.firebaseio.com"
#define FIREBASE_AUTH "***********************************"
#define WIFI_SSID "***********"
#define WIFI_PASSWORD "**************"

int sensor=D1;


void setup()
{
Serial.begin(115200);
pinMode(sensor,INPUT);

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Serial.print("Connecting to ");
Serial.print(WIFI_SSID);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}

Serial.println();
Serial.print("Connected");
Serial.print("IP Address: ");
Serial.println(WiFi.localIP()); //prints local IP address
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); // connect to the firebase

}

void loop()
{
int h = digitalRead(sensor);

if (isnan(h)) // Checking sensor working
{
Serial.println(F("Failed to read from sensor!"));
return;
}

if (h==0){
Firebase.pushString("Light", “OFF”); //setup path to send light readings
}
else{
Firebase.pushString("Light", “ON”);
}
if (Firebase.failed())
{

Serial.print("pushing /logs failed:");
Serial.println(Firebase.error());
return;
}
delay(2000);
}

In here you have to enter the FIREBASE_HOST and FIREBASE_AUTH that you will get from the firebase. Check this to know more:

Once you are done with this code. Connect the NodeMCU to the system and then upload the code.

This is how you can easily connect the firebase to the NodeMCU and apply Internet of things to your devices.

You can also check this for more clarification,

This is all for this blog. I hope you liked this blog. If have any query regarding the topic then feel free to leave a comment below.
Happy Learning. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *