IOT

IoT Based LED Control using Google Firebase & ESP8266

We can operate IoT devices such as LEDs from anywhere in the globe using IoT hardware and a cloud platform. This small IoT project uses Google Firebase Console and a NodeMCU ESP8266 wifi module to control LEDs. There are several ways to control the LED, including via a Web server or webpage, the Blynk application, and other API-based applications. But here we will only focus on Google Firebase

Google Firebase is a Google-backed application development platform that allows you to create, manage, and alter data generated by any Android/IOS application, online services, IoT sensors, and hardware. To learn more about the Google Firebase Console, you can read the official Google Firebase Documentation from Google Firebase

Google Firebase

Hardware Required

  • ESP8266-12E Board
  • 5mm LED of Any Color
  • Resistor-220 ohm
  • 5V, 1A Power Adapter
  • Breadboard

Setting Up Hardware & NodeMCU LED Circuit

The circuit design for connecting LEDs to the NodeMCU ESP8266 Board is shown below. As shown in the diagram, connect the positive terminal of the LED to GPIO5, i.e. D1 of the NodeMCU ESP8266, using a 220ohm resistor.

ESP8266 LED Control

Installing Firebase & JSON Library

FirebaseArduino is a library that makes it easier for Arduino clients to connect to the Firebase database. It’s a full abstraction of Firebase’s REST API, accessible in a wiring-friendly form through C++ functions. The library handles all JSON parsing, and you can work with pure C/Arduino types.

The library is not self-contained. As a result, you must also include the ArduinoJSON library.

1. ArduinoJSON Library

So first go to the library manager and search for “JSON” & install the library as shown in the figure below.

Note that the code may not work with the most recent JSON library. As a result, you may need to downgrade to version 5.13.5 of the library.

2. Google FirebaseExtended Library

You must now install the Google Firebase library. So, after extracting the library from the URL below, place it in the Library folder.

Download Google FirebaseExtended Library

Setting up Google Firebase

To use Google Firebase and the Nodemcu ESP8266 to control the LED, you must first set up Google Firebase. The step is a little lengthy, but don’t worry; I’ve broken it down for you below.

Step 1: If you have a Gmail id then you are already Sign Up for firebase. But if you don’t have Gmail id then first Sign Up for Gmail: https://gmail.com/

Step 2:Now visit https://firebase.google.com/ and click on Go to Console on the Top Right.

Step 3: Click on “Create a Project”.

Step 4: Fill in your “Project name,” then check the box next to “I Accept the Firebase Terms” and click “Continue.”

Step 5: A new window will now appear. So press the “Continue” button.

Step 6: Choose the “Google Analytics Account” created using your Gmail ID. Then select “Create Project” from the drop-down menu.

Step 7: Your project is now complete. As a result, the following window will appear. Select “Continue.”

Step 8: Now click on “Project Setting”.

Step 9: Under Project Setting, Click on “Service Accounts”. Copy the secret code from the list below. In the Arduino Code, the code is required.

Step 10: Click on “Create Database”.

Step 11: Choose “Start in Test Mode” and then click on “Next”.

Step 12: Now click on “Done” & from the left side click on “Database”.

Step 13: From the Database List, choose “Realtime Database.”

Step 14: Your final project is now complete. From the list, you can change the “ON/OFF” line.

Step 14: Your final project is now complete. From the list, you can change the “ON/OFF” line.“https://your_project_name.firebaseio.com/

LED Control using Google Firebase & ESP8266

Both the hardware and Google Firebase setup are now complete. The code can be uploaded to the NodeMCU ESP8266 Board. The entire code is shown below.

NodeMCU ESP8266 LED Google Firebase

Using Firebase, you can switch on the LED by writing “ON.” Similarly, you must write “OFF” to turn off the LED.

LED ON OFF ESP8266 Google Firebase

Source Code: IoT Based LED Control using Google Firebase & ESP8266

The following is a source code/program for controlling LEDs with Google Firebase Control and the ESP8266. Copy and paste the code onto the NodeMCU ESP8266 Board.

#define FIREBASE_HOST “my1stproject-34e8e.firebaseio.com”
#define FIREBASE_AUTH “KeiqJV41s********************LdNXL”
#define WIFI_SSID “Alexahome”                                          
#define WIFI_PASSWORD “12345678”

Before uploading the code, make changes to the following parameters like FIREBASE_HOST, FIREBASE_AUTH, WIFI_SSID & WIFI_PASSWORD.



#include <ESP8266WiFi.h>                                          
#include <FirebaseArduino.h>                                        
 
#define FIREBASE_HOST “my1stproject-34e8e.firebaseio.com”              // the project name address from firebase id
#define FIREBASE_AUTH “KeiqJV41s********************LdNXL”   // the secret key generated from firebase
#define WIFI_SSID “Alexahome”                                          
#define WIFI_PASSWORD “12345678”                                  
 
String fireStatus = “”;                                                 // led status received from firebase
int led = 5;  
                                                              
void setup()
{
  Serial.begin(9600);
  delay(1000);    
  pinMode(led, OUTPUT);            
  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 to “);
  Serial.println(WIFI_SSID);
  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, 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, LOW);                                                     // make external led OFF
  }
  else
  {
    Serial.println(“Command Error! Please send ON/OFF”);
  }
}


Code Explanation

First, we include the libraries for using ESP8266 and firebase.

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

Then we define the two parameters FIREBASE_HOST & FIREBASE_AUTH. These parameters are obtained from Google Firebase Setup. These two parameters are critical for interacting with Firebase. This allows data to flow between the ESP8266 and the Firebase.

#define FIREBASE_HOST “my1stproject-34e8e.firebaseio.com”  
#define FIREBASE_AUTH “KeiqJV41s********************LdNXL”

The WiFi SSID and Password are then defined. Replace SSID and password with the SSID and password for your network. The Nodemcu connects to the internet and communicates with the Google Firewall.

#define WIFI_SSID “Alexahome”                                          
#define WIFI_PASSWORD “12345678”

This code will connect the Nodemcu ESP8266 to the network. It will display the WiFi SSID name once connected.

WiFi.begin(WIFI_SSID, WIFI_PASSWORD);                          
  Serial.print(“Connecting to “);
  Serial.print(WIFI_SSID);
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(“.”);
    delay(500);
  }

The Nodemcu can now connect to the Firebase server with this statement. It will connect successfully if the host address and permission key are right.

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

The firebase library provides this class for sending a string to the firebase server. This is used to modify the status of an LED.

Firebase.setString(“LED_STATUS”, “OFF”);

After sending one status string to the firebase path, write this statement to receive the LED status from the same path and save it to a variable in the loop function.

fireStatus = Firebase.getString(“LED_STATUS”);                  =
  if (fireStatus == “ON”) {                                                      
    Serial.println(“Led Turned ON”);                    
    digitalWrite(led, HIGH);                                                    
  }
  else if (fireStatus == “OFF”) {                                        
    Serial.println(“Led Turned OFF”);
    digitalWrite(led, LOW);                                                    
  }
  else {
    Serial.println(“Command Error! Please send ON/OFF”);
  }

Depending on whether the String is “ON” or “OFF,” the LED will turn on or off. The serial monitor will display any other characters sent.“Command Error! Please send ON/OFF

This was all about the controlling of LED using Google Firebase. 

Conclusion

I hope all of you had understand how to design an IoT Based LED Control using Google Firebase & ESP8266. We MATHA ELECTRONICS will be back soon with more informative blogs.

Leave a Reply

Your email address will not be published.