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.
(adsbygoogle = window.adsbygoogle || []).push({});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.
- Nodemcu board x 1 — Amazon / Our Store
- LED x 1 — Amazon / Our Store
- 180-ohm Resistor — Amazon / Our Store
- Breadboard x 1 — Amazon / Our Store
- Jumper wires — Amazon / Our Store
Step 1
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
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.

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.

Step 2
Now, click the “Get Started” button. After, click the “New project” button.


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.


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
I am Brijesh Dungrani. I am facing problem in the project of NODE MCU with Firebase. I need your support.
what is your issue?
i do all settings as per your instructions but still my LED is not blinking.
Please double-check the program.
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”);
}
}