Easily Send Real-Time Sensor Data to Google Firebase with ESP8266

OVERVIEW

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”.

Real-Time database on firebase

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

Now select “Real-time 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 real-time 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 real-time 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 Real-Time 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,

Time to wrap up now. Hope you liked our content on How to send real-time sensor data to google firebase. See you in our next blog, thanks for reading our blog and do leave a comment below to help us improve the content to serve you all of our experience with you. Stay tuned with us for more Rasa Chatbot contents.

Also check out our other playlist Rasa Chatbot, Internet of things, Docker, Python Programming, etc.
Become a member of our social family on youtube here.

Stay tuned and Happy Learning. 🙂

Ashish Saini
Ashish Saini

I am a software developer for Python/Machine Learning. Also, I’m Rasa Hero at Rasa. Also, I’m a youtuber where I regulary uploads the videos to help this generation learn about the technology just like they are playing games.

2 thoughts on “Easily Send Real-Time Sensor Data to Google Firebase with ESP8266

  1. Code does not compile.

    In file included from C:\Users\cagan\Documents\Arduino\libraries\firebase-arduino-master\src/Firebase.h:30:0,
    from C:\Users\cagan\Documents\Arduino\libraries\firebase-arduino-master\src/FirebaseArduino.h:22,
    from C:\Users\cagan\Desktop\sketch_apr11c\sketch_apr11c.ino:2:
    C:\Users\cagan\Documents\Arduino\libraries\firebase-arduino-master\src/FirebaseObject.h:109:11: error: StaticJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to upgrade your program to ArduinoJson version 6
    std::shared_ptr<StaticJsonBuffer> buffer_;
    ^
    In file included from C:\Users\cagan\Documents\Arduino\libraries\firebase-arduino-master\src/FirebaseArduino.h:22:0,
    from C:\Users\cagan\Desktop\sketch_apr11c\sketch_apr11c.ino:2:
    C:\Users\cagan\Documents\Arduino\libraries\firebase-arduino-master\src/Firebase.h:86:11: error: StaticJsonBuffer is a class from ArduinoJson 5. Please see arduinojson.org/upgrade to learn how to upgrade your program to ArduinoJson version 6
    std::shared_ptr<StaticJsonBuffer> buffer_;
    ^
    exit status 1
    Error compiling for board NodeMCU 1.0 (ESP-12E Module).

Leave a Reply

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