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

 How to fetch data from Firebase to Nodemcu

        Hello guys, welcome back. In this tutorial, we are going to learn how to fetch data from Firebase to Nodemcu. Also, a 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 think for more details in the Nodemcu ESP8266. 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.

Step 1

Firstly, identify these components.

Nodemcu board.

LED.

180-ohm Resistor.

Breadboard.

Jumper wires.




Step 2

Secondly, connect these components, For that, use the circuit diagram below.
How to fetch data from Firebase to Nodemcu

Step 3

Thirdly, let’s set up the firebase and Arduino IDE Program. For that, follow the steps below.

Step 1

OK, first open your internet browser and search for “Firebase” here. Then, click on the second result on the page.
How to fetch data from Firebase to Nodemcu

Step 2

Now, click the “Get Started” button. After, click the “New project” button.
How to fetch data from Firebase to Nodemcu

Step 3

Next, enter your project name and click the “Continue” button. Click again the “Continue” button.

Step 4

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.
How to fetch data from Firebase to Nodemcu




Step 5

Now, click on the “Project Settings” button and then the “Service Account” button. After, click the “Database Secrets” button on this page.

Step 6

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, included the WIFI library and firebase library. Download these libraries using the links below.
WIFI library — Download
Firebase library —  Download
#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-connected pin is configured 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);
}
 
Lastly, in the invalid loop, the values received through the Firebase are entered into a variable called “fireStatus”. Then if that value is 1 the LED will turn 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”);
  }
 




Step 7

Okay, now let’s go back to the Firebase side. Then copy and paste this code on the Arduino IDE program.

Step 8

Afterward, click the “Realtime Database” button on the left side. Next, click the “Create Database” button. Then select the Database location and click the “Next” button. Next step select the “Start in test mode” after, clicks the “Enable” button.

Step 9

Now, copy and paste this Firebase host link into the program.




Step 10

Then, click the “+” icon and enter “LED_STATUS” for the name and “0” for the value. Next, click the “Add” button. OK, now set up the Firebase software is enough.

Step 11

Now, fill in the program’s WIFI connection information. For that. Enter the WIFI name and WIFI password.

Step 12

Now, let us 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 13

Lastly, 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.




Below is the full video guide. We will meet in the next tutorial.

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

Similar Posts

4 Comments

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

Leave a Reply

Your email address will not be published.