Skip to content
Home » IoT » Google Firebase and NodeMCU – Control LED

Google Firebase and NodeMCU – Control LED

Featured image showing NodeMCU ESP8266 development board alongside Google Firebase logo with NerdyElectronics watermark for the Firebase LED control tutorial
Embedded Systems Learning Path
Part 92 of 129View Full Path →

KEY TAKEAWAYS

  • Firebase Realtime Database provides a cloud-hosted JSON database accessible via REST API
  • NodeMCU can send data to Firebase using HTTPS requests with the Firebase host and auth token
  • Real-time data updates from Firebase can trigger actions on the NodeMCU (e.g., turning an LED on/off)
  • Firebase is a good alternative to AWS IoT for simpler IoT projects with real-time sync
In this post we will see how to control an LED connected to NodeMCU using Google Firebase. We will see how to create a database in firebase and then establish communication from NodeMCU to Firebase to control the LED from anywhere.

What is Google Firebase ?

Firebase is Google’s database platform which is used to create, manage and modify data generated from any android application, web services, sensors etc. For more information on Firebase please visit the official website of Firebase.

Benefits of Using Google Firebase

  • You can operate LED from anywhere using Internet (on Global Sever)
  • Your Data will be stored and can be accessed anytime from anywhere
  • Google Firebase is easy to understand and free for up to 10 GB data, which is sufficient for Home based Projects

Setting Up NodeMCU With LED

Before starting with Google Firebase, we need to set up the LED to NodeMCU. So, refer the components required section and connection figure below.

Components required

  1. NodeMCU ESP8266(12E)
  2. Resistor (100 Ω)
  3. An LED

LED Connection from NodeMCU

  • Positive terminal of LED to D3 Pin of NodeMCU (Via Resistor)
  • LED Negative terminal to GND Pin of NodeMCU
Refer the fig. for Connection :

Adding Required libraries for Firebase Connection

Their are two libraries which need to be downloaded for connection to Google Firebase in your Arduino IDE
  • Arduino JSON
  • Firebase Arduino
To Download Arduino JSONOpen Arduino IDE << Go to Tools << Library Manager << Search “arduino json” << Download “ArduinoJson by Benoit Blanchon”Refer the Fig for Library: To Download Firebase ArduinoOpen Arduino IDE << Go to Tools << Manage Library << Download ZIP file of Firebase Arduino library from GitHub << ADD ZIP FileGitHub Link for Firebase Arduino Library : https://github.com/FirebaseExtended/firebase-arduino/blob/master/src/Firebase.h 

Setting Up Firebase Console

Create a Project by going to : https://firebase.google.com/google firebaseOnce your Project will created you’ll see :After that create Realtime Database by going to Realtime Database Console :google firebase realtime databaseSet the database in “Test Mode” :google firebase security ruleOnce your Realtime database is created , Copy the Database URL and Create a Node with Name “LED_STATUS” keeping in “off” , Refer the figure below :Now copy the Authentication Database Secret key by going into Project settings, refer the fig below :Copy the Database URL and Authentication Database Secrets key in Notepad, since they will be required in programming

Programming NodeMCU for using Google Firebase

Firstly include the libraries for using NodeMCU and firebase
#include <ESP8266WiFi.h
#include <FirebaseArduino.h
Define Firebase Database URL and Authentication Database Secrets Key
#define FIREBASE_HOST "your-project.firebaseio.com"             // Database URL
#define FIREBASE_AUTH "06dEpqanFg***************qAwnQLwLI"       // the secret key generated from firebase Database Secrets
Define SSID (WIFI Name) and Password for Internet Connection
#define WIFI_SSID "xxxxxxxxxxxxx"                   // input your home or public wifi name 
#define WIFI_PASSWORD "xxxxxxxxxxxxxx"             //password of wifi ssid
Initialize Wifi and Firebase Connection
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);             //try to connect with wifi
 while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);            // connect to firebase
Setting Firebase String and Get request commands
Firebase.setString("LED_STATUS", "OFF");                  //send initial string of led status
fireStatus = Firebase.getString("LED_STATUS");              // get ld status input from firebase

 if (fireStatus == "ON" || fireStatus == "on") {                                   // compare the input of led status received from firebase
    Serial.println("Led Turned ON");                         
    digitalWrite(LED_BUILTIN, HIGH);                             // make bultin led ON
    digitalWrite(led, HIGH);                                    // make external led ON
  } 

 else if (fireStatus == "OFF" || fireStatus == "off") {                                // compare the input of led status received from firebase
    Serial.println("Led Turned OFF");
    digitalWrite(LED_BUILTIN, LOW);                                // make bultin led OFF
    digitalWrite(led, LOW);                                         // make external led OFF
  }

 else {
    Serial.println("Wrong Credential! Please send ON/OFF");
    }
After Flashing this code you can go to your Realtime database console and change the LED_STATUS to “on” and observe that your LED will glow

Full Code:

#include <ESP8266WiFi.h>                                                // esp8266 library
#include <FirebaseArduino.h>                                             // firebase library

#define FIREBASE_HOST "your-project.firebaseio.com"                         // the project name address from firebase id
#define FIREBASE_AUTH "06dEpqanFg***************qAwnQLwLI"                    // the secret key generated from firebase
#define WIFI_SSID "xxxxxxxxxxxxx"                                          // input your home or public wifi name 
#define WIFI_PASSWORD "xxxxxxxxxxxxxx"                                    //password of wifi ssid

String fireStatus = "";                                                     // led status received from firebase
int led = D3;                                                                // for external led
void setup() {
  Serial.begin(9600);
  delay(1000);
  pinMode(LED_BUILTIN, OUTPUT);      
  pinMode(led, OUTPUT);                 
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);                                      //try to connect with wifi
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }
  Serial.println();
  Serial.print("Connected to ");
  Serial.println(WIFI_SSID);
  Serial.print("IP Address is : ");
  Serial.println(WiFi.localIP());                                                      //print local IP address
  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);                                       // connect to firebase
  Firebase.setString("LED_STATUS", "OFF");                                          //send initial string of led status
}

void loop() {
  fireStatus = Firebase.getString("LED_STATUS");                                      // get ld status input from firebase
  if (fireStatus == "ON") {                                                          // compare the input of led status received from firebase
    Serial.println("Led Turned ON");                         
    digitalWrite(LED_BUILTIN, LOW);                                                  // make bultin led ON
    digitalWrite(led, HIGH);                                                         // make external led ON
  } 
  else if (fireStatus == "OFF") {                                                  // compare the input of led status received from firebase
    Serial.println("Led Turned OFF");
    digitalWrite(LED_BUILTIN, HIGH);                                               // make bultin led OFF
    digitalWrite(led, LOW);                                                         // make external led OFF
  }
  else {
    Serial.println("Wrong Credential! Please send ON/OFF");
  }
}
 

📖 Related: How to Create a "Thing" in AWS IoT

Leave a Reply

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