IoT Based LED control using Google Firebase and ESP32 NodeMCU

IoT Based LED control using Google Firebase and ESP32 NodMCU

ESP32 is a powerful hardware platform for IoT applications and is widely used for prototyping and development of IoT applications. In some of our previous sessions, we have used ESP32 with many IoT cloud platforms:

 

In this article we will see how we can control LED with Goggle firebase console and ESP32.

Firebase is Google’s database platform which is used to create, manage and modify data generated from any android application, web services, sensors etc. Its basically a mobile and web application development platform which has many services like Firebase cloud messaging, Firebase auth, Realtime database, etc. In realtime database we can see realtime data on firebase cloud and can control any peripheral from anywhere using Internet.

To control LED from Google firebase, first we will setup Google firebase console and then ESP32 module.

 

Requirements

  1. ESP32 module
  2. USB Cable
  3. Breadboard
  4. LED
  5. Jumper wires
  6. Resistor 1K

 

Circuit Diagram

Circuit Diagram for IoT Based LED Control using Google Firebase and ESP32 NodMCU

 

Setting up Firebase Console for ESP32

If you are using firebase for the first time then you have to create account for firebase or can directly signup using Google Account:

  1. Open your browser and go for https://firebase.google.com
  2. At the right top corner click on “Go to Console”.

Setting up Firebase Console for ESP32

 

  1. Click on “Add Project”.

Add Project to Google Firebase for IoT Based LED Control using ESP32 NodMCU

 

  1. Input your project name as you want and click on create project.

Create Project to Google Firebase for IoT Based LED Control using ESP32 NodMCU

 

  1. Now your project is created and click on “Continue”.

 Google Firebase Based LED Control using ESP32 NodMCU

 

  1. Now you will need host name and authorization key/secret key for this project while programming your ESP32; so now we will see how these parameters can be taken from this.
  2. Go to setting icon and click on “Project Setting”.

 Setup Google Firebase for IoT Based LED Control using ESP32 NodMCU

 

  1. Now click on Service accounts and then Database secrets.

 Setup Google Firebase Account for IoT Based LED Control using ESP32 NodMCU

 

  1. On clicking on Database Secrets you will find a secret key, copy this key and save it in notepad, this is your firebase authorization key which you will need later.

Setup Google Firebase Account Key for IoT Based LED Control using ESP32 NodMCU

 

  1.  Now click on “Database” at left control bar.

 Setup Google Firebase Account Database-for IoT Based LED Control using ESP32 NodMCU

 

  1.  Now scroll down and click on “Create database”.

Create Database on Google Firebase for IoT Based LED Control using ESP32 NodMCU

 

  1.  Now choose “Start in test mode” and click on Enable.

 Testing Google Firebase for IoT Based LED Control using ESP32 NodMCU

 

  1.  Now your database is created and you will have to come here again to control your LED, for now just copy the given URL without slash and http in notepad this is your firebase host which you will be required later.

 Google Firebase Host for IoT Based LED Control using ESP32 NodMCU

 

Setting up ESP32 module with Firebase

To work with ESP32 using Google firebase you will need a firebase library so firstly download that library using below link and save it in Arduino library files.

https://github.com/ioxhop/IOXhop_FirebaseESP32

Now open your Arduino IDE and go to Sketch---> include Library--> Add .ZIP library and add the file you downloaded from the above link.

 Setting up ESP32 with Firebase for Controlling LED

 

After installing the library you are ready to work with Google firebase using ESP32.

Now go to Tools and select ESP32 Dev board and appropriate COM port and copy the code given below and edit it for network credentials, firebase secret key and firebase host. After editing the upload the code into ESP32 using Arduino IDE.

 

Programming ESP32 for Google Firebase

Complete code for controlling LED using ESP32 is given at the end of the tutorial

Firstly include the libraries for using ESP32 and firebase.

#include <WiFi.h>
#include <IOXhop_FirebaseESP32.h>

 

Now enter your firebase host, secret key and network credentials.

#define FIREBASE_HOST "esp32led.firebaseio.com"   
#define FIREBASE_AUTH "4QHdeFZquTh4fdXZkum2EPt2A50gXXXXXXXXX"   
#define WIFI_SSID "XXXXXX"               
#define WIFI_PASSWORD "XXXXXXXXXX"

 

In the setup function, define output pin, delay, baud rate and connect to your Wi-Fi.

void setup() {
  Serial.begin(9600);
  delay(1000);
  pinMode(2, OUTPUT);                
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);                                  
  Serial.print("Connecting to ");
  Serial.print(WIFI_SSID);
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print(".");
    delay(500);
  }

 

The below statement tries to connect with the firebase server. If the host address and authorization key are correct then it will connect successfully.

Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);

 

Now this is the class provided by firebase library to send string to firebase server. With the help of this we can change the status of LED.

Firebase.setString("LED_STATUS", "OFF");

 

After sending one status string to firebase path, write this statement to get the status of LED from same path and save it to variable.

fireStatus = Firebase.getString("LED_STATUS");

 

If received string is “ON” or “on” then just turn on the output LED.

if (fireStatus == "ON")
 {                              
Serial.println("Led Turned ON");      
digitalWrite(2, HIGH);                                  
 }

 

If received string is “OFF” or “off” then just turn off the output LED.

else if (fireStatus == "OFF") {
 Serial.println("Led Turned OFF");
 digitalWrite(2, LOW);
}

 

If received string is not any of these then just ignore and print some error message.

else {
Serial.println("Wrong Credential! Please send ON/OFF");
  }

 

The complete code is given at the end of this article, you can check from there, edit it and then upload it.

 

Output

After uploading the code open your serial monitor and in your browser open firebase and go to console and then select your project that you created earlier. In this go to database and it will show initially LED STATUS is OFF, from here you can change the LED status by writing ON here, your LED will change to ON state and you can see your LED state in your serial monitor also.

Google Firebase Controlling LED using ESP32 NodMCU

 Controlling LED using Google Firebase and ESP32

Code

#include <WiFi.h>                                                // esp32 library

#include <IOXhop_FirebaseESP32.h>                                             // firebase library

#define FIREBASE_HOST "XXXXXX "                         // the project name address from firebase id

#define FIREBASE_AUTH "ztRgolkRUMKEZzaKXmyAC2ZUlGcuWojXXXXXXXXX"                    // the secret key generated from firebase

#define WIFI_SSID "Ashish"                                          // input your home or public wifi name

#define WIFI_PASSWORD "XXXXXXXX"                                    //password of wifi ssid

String fireStatus = "";                                                     // led status received from firebase

int led = 2;                                                               

void setup() {

  Serial.begin(9600);

  delay(1000);

  pinMode(2, 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 led status input from firebase

  if (fireStatus == "ON") {                         // compare the input of led status received from firebase

    Serial.println("Led Turned ON");                 

    digitalWrite(2, HIGH);                                                         // make output led ON

  }

  else if (fireStatus == "OFF") {              // compare the input of led status received from firebase

    Serial.println("Led Turned OFF");

    digitalWrite(2, LOW);                                                         // make output led OFF

  }

  else {

    Serial.println("Wrong Credential! Please send ON/OFF");

  }

}

Video

11 Comments

I simply wanted to post a simple message so as to appreciate you for all of the splendid guides you are sharing at this website. My long internet lookup has now been rewarded with reputable facts to share with my best friends. I would assume that most of us readers actually are really lucky to dwell in a magnificent community with very many special professionals with good points. I feel pretty happy to have come across the webpages and look forward to plenty of more entertaining moments reading here. Thanks a lot once again for everything.

I would like to express some appreciation to this writer just for rescuing me from this particular predicament. As a result of browsing throughout the search engines and seeing methods which are not powerful, I thought my entire life was done. Existing without the presence of approaches to the problems you have resolved through your good article content is a crucial case, and those which may have in a negative way damaged my entire career if I hadn't encountered your web blog. The competence and kindness in handling all the things was useful. I don't know what I would have done if I hadn't encountered such a subject like this. I can also at this moment look ahead to my future. Thanks for your time so much for your expert and amazing help. I won't hesitate to propose the sites to any individual who needs and wants care on this topic.

I happen to be commenting to let you be aware of of the amazing discovery my wife's girl gained visiting your webblog. She came to find lots of pieces, which included what it is like to possess a wonderful helping style to have other people with no trouble know various tortuous matters. You actually surpassed my expectations. Many thanks for churning out the practical, trustworthy, edifying and as well as unique tips on your topic to Emily.

I needed to send you this little observation to finally say thanks the moment again for these unique strategies you have documented at this time. This has been quite tremendously generous with you to grant openly all that numerous people would have offered for sale for an e book to get some profit for their own end, and in particular since you might have done it if you ever decided. These secrets additionally served to be a easy way to fully grasp that some people have the same keenness just as my own to understand lots more on the topic of this problem. I'm sure there are thousands of more enjoyable opportunities up front for individuals who take a look at your blog post.

I needed to create you the very little word to thank you very much over again considering the amazing things you have contributed in this article. It was certainly pretty open-handed of people like you to deliver freely all many individuals would've offered as an e-book to get some bucks on their own, most importantly since you might have done it if you decided. Those smart ideas also acted like the fantastic way to comprehend someone else have the same passion like my personal own to find out very much more pertaining to this issue. I am sure there are millions of more pleasant periods ahead for many who discover your blog.

A lot of thanks for all of the work on this web site. My niece enjoys engaging in research and it's really obvious why. All of us learn all regarding the compelling method you provide good guides on your web blog and even invigorate participation from others on the content and our favorite princess is always discovering a great deal. Enjoy the remaining portion of the new year. You're performing a wonderful job.

I wish to show my gratitude for your kind-heartedness giving support to people who need guidance on your concern. Your personal dedication to passing the message all-around has been exceptionally powerful and have in most cases helped workers like me to realize their pursuits. Your new useful advice can mean a lot a person like me and far more to my fellow workers. With thanks; from each one of us.

I have to express thanks to the writer for bailing me out of such a setting. As a result of checking throughout the internet and getting techniques which were not pleasant, I assumed my life was gone. Being alive devoid of the solutions to the problems you've resolved as a result of your post is a crucial case, and those that could have negatively damaged my career if I hadn't encountered your web site. Your primary know-how and kindness in controlling every aspect was important. I'm not sure what I would have done if I hadn't discovered such a solution like this. It's possible to at this time look forward to my future. Thanks so much for the specialized and effective guide. I will not hesitate to suggest your web site to anyone who should receive recommendations on this situation.

Thanks so much for giving everyone an exceptionally special possiblity to read critical reviews from this blog. It is usually so terrific and also stuffed with a good time for me personally and my office friends to search your web site not less than three times per week to learn the newest guidance you have got. And indeed, I'm always satisfied considering the impressive tactics you give. Certain two points in this post are easily the very best we've ever had.

I simply wanted to thank you very much all over again. I am not sure what I might have achieved without the entire points documented by you regarding this subject. Entirely was the challenging crisis for me, nevertheless noticing a well-written approach you resolved it made me to leap over happiness. Now i'm happy for this service and then sincerely hope you are aware of an amazing job that you're carrying out instructing the mediocre ones using your web site. I'm certain you haven't encountered all of us.

Add new comment

The content of this field is kept private and will not be shown publicly.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.