How to fetch data from Firebase to Nodemcu | Step by step instructions

How to fetch data from Firebase to Nodemcu | Step by step instructions

Hello guys, welcome back. In this tutorial, we are going to learn how to fetch data from Firebase to Nodemcu. Also, an LED ON and OFF example is used to explain this tutorial. This can be simply described as an IoT project. Because the Internet is required to turn this LED on and off. You may remember that we used the Blynk app for this in a previous lesson. Today we will use Firebase software instead. Click this link for more information about Nodemcu ESP8266.

What is the Firebase?

Firebase is simply software used to manage and modify Android / IOS applications, web services, IoT sensors, and hardware. This is a service provided by Google. Also, that is an open-source web application software. Just having a Google account is enough to work with this. Also, The following steps will show you how to set up the firebase.

OK, let’s learn this lesson step by step. So, the required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, connect these components, For that, use the circuit diagram below.

How to fetch data from Firebase to Nodemcu | Step by step instructions

Step 3

Thirdly, let’s set up the Firebase and required Program. For that, follow the steps below.

  • First, open your internet browser and search for “Firebase” here. Then, go to the Firebase website and sign in to your Gmail account.
How to fetch data from Firebase to Nodemcu | Step by step instructions
  • Now, click the “Get Started” button. After, click the “New project” button.
  • Next, enter your project name and click the “Continue” button. Click again the “Continue” button.
  • OK, select your account now, then select “Default Account for Firebase”. Then click the “Create Project” button. After, you can see how your project is set up. It only takes a few seconds. Now, your project is ready. If all this is correct, click the “Continue” button.
  • Now, click on the “Project Settings” button and then the “Service Account” button. After, click the “Database Secrets” button on this page.
  • Well, now we will pause the Firebase setup and set up the Arduino IDE program. The code is as follows.
  • The complete program of this project – Download
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
 
#define FIREBASE_HOST " " // Firebase host
#define FIREBASE_AUTH " " //Firebase Auth code
#define WIFI_SSID " " //Enter your wifi Name
#define WIFI_PASSWORD " " // Enter your password
int fireStatus = 0;
 
void setup() {
  Serial.begin(9600);
  pinMode(D1, OUTPUT);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.println("Connected.");
  Serial.println(WiFi.localIP());
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.set("LED_STATUS", 0);
}
 
void loop() {
  fireStatus = Firebase.getInt("LED_STATUS");
  if (fireStatus == 1) {
    Serial.println("Led Turned ON");
    digitalWrite(D1, HIGH);
  }
  else if (fireStatus == 0) {
    Serial.println("Led Turned OFF");
    digitalWrite(D1, LOW);
  }
  else {
    Serial.println("Command Error! Please send 0/1");
  }
} 

Code explanation

Firstly, include the WIFI library and Firebase library. Download these libraries using the links below.

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

Secondly, define the variables that should be included in Firebase Details and WIFI Connection Details. We will include this information later.

#define FIREBASE_HOST ” ” // Firebase host
#define FIREBASE_AUTH ” ” //Firebase Auth code
#define WIFI_SSID ” ” //Enter your wifi Name
#define WIFI_PASSWORD ” ” // Enter your password

Thirdly, the LED pin is set as an OUTPUT pin. Later, the WIFI library and Firebase library were begun in the void setup.

void setup() {
  Serial.begin(9600);
  pinMode(D1, OUTPUT);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print(“Connecting”);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(“.”);
    delay(500);
  }
  Serial.println();
  Serial.println(“Connected.”);
  Serial.println(WiFi.localIP());
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.set(“LED_STATUS”, 0);
}

In the void loop, the values received via Firebase are inserted into a variable called “fireStatus”. Then if that value is 1, the LED will be ON. If 0, the LED turns off.

void loop() {
  fireStatus = Firebase.getInt(“LED_STATUS”);
  if (fireStatus == 1) {
    Serial.println(“Led Turned ON”);
    digitalWrite(D1, HIGH);
  }
  else if (fireStatus == 0) {
    Serial.println(“Led Turned OFF”);
    digitalWrite(D1, LOW);
  }
  else {
    Serial.println(“Command Error! Please send 0/1”);
  }
} 
  • Okay, now let’s go back to the Firebase side. Then copy and paste this code to the program.
  • Afterwards, click the “Create Database” button. Then select the Database location and click the “Next” button. Now, select the “Start in test mode” and click the “Enable” button.
  • Now, copy and paste this Firebase host link into the program.
  • Then, click the “+” icon and enter “LED_STATUS” for the name and “0” for the value. Next, click the “Add” button.

Step 4

Now, enter your WIFI SSID and password.

How to fetch data from Firebase to Nodemcu | Step by step instructions

Step 5

Now, let’s download and install the JSON 5.13.5 library which is an essential library to work with Firebase. For that, use the pictures below.

Step 6

Finally, select the correct board and port, After, upload this program to the Nodemcu board. OK, now we can ON and OFF the LED using Firebase. For that, open the Firebase again and update the values to 1 and 0.

OK, enjoy this project. The full video guide is below. So, we hope to see you in the next project or tutorial. Have a good day.

How to fetch data from Firebase to Nodemcu | Step by step instructions

Similar Posts

5 Comments

  1. I am Brijesh Dungrani. I am facing problem in the project of NODE MCU with Firebase. I need your support.

  2. I have an issue, I followed your steps and made a little bit change to work with ESP8266.
    the led always ON for about 30 sec and become OFF for just 1 sec however the real time database in Firebase is 1 or 0.
    that is my code

    //#include
    //#include
    //#include
    //#include
    //
    //#define FIREBASE_HOST “gtech2-default-rtdb.firebaseio.com/”
    //#define FIREBASE_AUTH “5HaHBleY7FL6WLnu0adS9ag28yj5Q9wQU3CmXXV3”
    //FirebaseData fbdo;
    //const char* ssid = “Gemo”;
    //const char* password = “227HectorHector”;

    #include
    #include

    #define FIREBASE_HOST “gtech2-default-rtdb.firebaseio.com/” // Firebase host
    #define FIREBASE_AUTH “5HaHBleY7FL6WLnu0adS9ag28yj5Q9wQU3CmXXV3” //Firebase Auth code
    #define WIFI_SSID “Gemo” //Enter your wifi Name
    #define WIFI_PASSWORD “227HectorHector” // Enter your password
    int fireStatus = 0;
    FirebaseData fbdo;
    void setup() {
    Serial.begin(115200);
    pinMode(D7, OUTPUT);
    WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
    Serial.print(“Connecting”);
    while (WiFi.status() != WL_CONNECTED) {
    Serial.print(“.”);
    delay(500);
    }
    Serial.println();
    Serial.println(“Connected.”);
    Serial.println(WiFi.localIP());
    Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
    }

    void loop() {
    fireStatus = Firebase.getInt(fbdo,”LED_STATUS”);
    if (fireStatus == 1) {
    Serial.println(“Led Turned ON”);
    digitalWrite(D7, HIGH);
    }
    else if (fireStatus == 0) {
    Serial.println(“Led Turned OFF”);
    digitalWrite(D7, LOW);
    }
    else {
    Serial.println(“Command Error! Please send 0/1”);
    }
    }

Leave a Reply

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