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

30 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.

Needed to put you that little observation so as to say thanks a lot once again with your amazing opinions you have shared on this website. It's certainly strangely generous of you to offer easily what a lot of folks could possibly have distributed as an ebook to help with making some bucks for themselves, mostly considering the fact that you might well have done it in the event you considered necessary. These pointers in addition worked to become great way to comprehend many people have similar interest much like my very own to know significantly more with reference to this condition. I know there are several more pleasant sessions in the future for many who see your blog.

After study a couple of of the blog posts on your website now, and I truly like your way of blogging. I bookmarked it to my bookmark website listing and will probably be checking back soon. Pls take a look at my web site as well and let me know what you think.

A lot of thanks for all your valuable labor on this blog. Debby really loves conducting internet research and it is simple to grasp why. Many of us hear all relating to the powerful manner you present priceless guidance through the website and as well as strongly encourage participation from other individuals on that point plus our favorite child is certainly understanding so much. Enjoy the rest of the year. Your performing a really great job.

Thank you a lot for providing individuals with an extremely marvellous opportunity to read critical reviews from this web site. It's always so superb plus stuffed with amusement for me personally and my office acquaintances to visit your blog nearly three times in one week to learn the newest tips you have. And lastly, I'm just at all times motivated for the astounding information you serve. Selected 3 tips in this posting are without a doubt the most suitable I have had.

Thank you for each of your hard work on this website. My aunt really likes doing investigations and it is easy to see why. My partner and i learn all regarding the compelling way you convey effective guidance through this web site and even recommend contribution from other people about this idea and our princess is without question discovering so much. Take advantage of the rest of the year. You are doing a terrific job.

I am commenting to let you understand what a exceptional experience my wife's child found visiting your blog. She mastered a wide variety of pieces, which included how it is like to possess an excellent teaching mindset to get other individuals without problems thoroughly grasp a number of complicated topics. You truly did more than her desires. Many thanks for delivering such necessary, healthy, explanatory and also cool guidance on your topic to Janet.

I wish to express my appreciation to the writer for bailing me out of such a condition. Right after looking through the the web and meeting views which are not productive, I figured my life was gone. Living without the presence of strategies to the issues you've fixed by means of your main guideline is a serious case, as well as the kind which might have in a wrong way damaged my career if I had not discovered your web page. Your main mastery and kindness in maneuvering every part was very useful. I'm not sure what I would have done if I had not discovered such a thing like this. I'm able to at this time look forward to my future. Thanks a lot so much for this reliable and effective guide. I will not be reluctant to endorse the website to any individual who ought to have assistance on this situation.

Needed to put you this little observation to help say thanks yet again on the precious secrets you have contributed above. It is really shockingly open-handed with people like you to allow extensively all that a number of us could possibly have supplied as an e book to end up making some money on their own, notably considering that you could possibly have tried it if you ever considered necessary. Those things in addition worked as the fantastic way to be certain that the rest have the same passion just as my own to know the truth lots more around this problem. I'm certain there are thousands of more pleasant moments in the future for folks who look into your website.

I wish to show some thanks to this writer just for rescuing me from such a difficulty. After surfing throughout the internet and getting solutions that were not powerful, I assumed my entire life was over. Being alive without the solutions to the problems you've solved by means of this website is a critical case, as well as those that would have in a wrong way damaged my entire career if I had not discovered your blog post. Your main mastery and kindness in handling the whole lot was vital. I am not sure what I would have done if I hadn't encountered such a thing like this. I'm able to at this point look ahead to my future. Thanks for your time so much for the reliable and sensible help. I won't hesitate to endorse your site to any person who should receive assistance about this subject.

I wanted to put you this bit of note to help thank you yet again relating to the splendid ideas you've shared here. It's so particularly generous with people like you to provide unhampered all many of us might have advertised as an electronic book in order to make some dough for themselves, chiefly considering the fact that you could have tried it in case you desired. Those pointers likewise acted as the good way to realize that many people have the same keenness the same as my personal own to realize lots more with regards to this condition. I am sure there are millions of more enjoyable sessions in the future for individuals that scan your site.

Nice post. I study one thing more difficult on different blogs everyday. It'll all the time be stimulating to learn content material from different writers and follow a little bit one thing from their store. I抎 choose to use some with the content on my blog whether or not you don抰 mind. Natually I抣l provide you with a hyperlink on your web blog. Thanks for sharing.

Can I just say what a reduction to find somebody who really knows what theyre speaking about on the internet. You definitely know the best way to deliver a difficulty to gentle and make it important. Extra individuals must learn this and understand this aspect of the story. I cant imagine youre not more standard because you definitely have the gift.

The following time I learn a blog, I hope that it doesnt disappoint me as much as this one. I imply, I do know it was my option to learn, but I really thought youd have one thing interesting to say. All I hear is a bunch of whining about something that you could fix in case you werent too busy on the lookout for attention.

My husband and i ended up being really joyful Jordan could deal with his inquiry using the precious recommendations he obtained using your weblog. It's not at all simplistic just to find yourself making a gift of thoughts that many the others may have been selling. Therefore we fully grasp we have the website owner to appreciate for that. The illustrations you have made, the easy web site navigation, the friendships you will help create - it's all powerful, and it's really assisting our son and the family believe that the concept is entertaining, which is especially important. Many thanks for all!

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.