ESP32IOT Projects

ESP32 Firebase Tutorial, Send Sensor Data to Google Firebase Database

ESP32 Firebase Project Description:

ESP32 Firebase- Before I am going to explain how to use the ESP32 with Firebase, first, I am going to talk about some of my previous projects based on the Google Firebase. In my last tutorial on the Google Firebase Database, I explained how to send a sensor value from the Nodemcu ESP8266 Wifi Module to the Google Firebase Database. If you want to use Nodemcu ESP8266 WiFi Module with the Google Firebase then read my article “Nodemcu Firebase Database Tutorial“, this is a beginners level project and explains the very basics like for example, how to setup your own Google Firebase account, How to connect an analog sensor with the Nodemcu ESP8266, and send the sensor data to the Firebase Database.

I also uploaded an advanced version based on the Nodemcu ESP8266 and Firebase Database. The advanced version is about the students’ attendance system using GSM sim900A module, RFID MFRC522, 16×2 LCD, and the Google’s Firebase Database. This is really an amazing project, you will learn a lot of new things like for example, how to use an RFID module, GSM Module, and how to send attendance data from the Arduino to the Google Firebase Database. But before you are going to make the advanced version first I recommend read my getting started tutorial on the Nodemcu Firebase Database.

In today’s tutorial, we will be using the ESP32 Wifi + Bluetooth Module by the Espressif Systems with the Google Firebase database. This is the same company that created the ESP8266 series of chips, modules, and development boards. Unlike the Nodemcu ESP8266 Wifi Module, the ESP32 Wifi + Bluetooth module is becoming very famous as it consist of more analog pins; while in Nodemcu ESP8266 there is only one Analog pin A0.


About the Sponsor “PCBWay”:

ESP32 Firebase

High quality & Only 24 Hours Build time

This ESP32 Power Supply PCB board is sponsored and manufactured by the PCBway Company, which is one of the most experienced PCB and PCB assembly manufacturer. They create high-quality PCBs at reasonable prices, Only 5 dollars for 10 PCBs and 30 dollars in total for 20 PCBs assembly; besides this the new members also get a 5 Dollars bonus. As you can see the quality is really great, the silkscreen is quite clear and the black solder mask looks amazing. I am 100% satisfied with their work.

Download Gerber files:

As this is a getting started tutorial based on the ESP32 and Firebase, I will try to keep things simple so that you can easily follow this tutorial. I will be using a variable resistor or potentiometer as the Sensor. As you can easily arrange a variable resistor or potentiometer; later you can replace a variable resistor with any analog sensor you want.

In this tutorial, we will cover,

  1. About the Google Firebase
  2. Circuit Diagram Explanation
  3. ESP32 Firebase programming and finally
  4. Testing

Without any further delay let’s get started!!!



The components and tools used in this project can be purchased from Amazon, the components Purchase links are given below:

ESP32 WiFi + Bluetooth Module(Recommended)

LM7805 Voltage Regulator:

470uf capacitor:

DC Female Power Jack:

Female Headers:

Male Headers:

Potentiometer:

Other Tools and Components:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!


About the Google Firebase Database:

Google Firebase is a Google-backed application development software used for creating, managing, and modifying data generated from any android/IOS application, web services, IoT sensors & Hardware. To learn more about the Google Firebase Console, you can read the official Google Firebase Documentation from Google Firebase.

Some of the most commonly asked questions about the Google Firebase Database.

What is Google Firebase Database?

The Firebase Realtime Database is a cloud-hosted database. When you build cross-platform apps with our iOS, Android, and JavaScript SDKs, all of your clients share one realtime Database instance and automatically receive updates with the newest data.

What type of Database is Firebase?

Firebase is a Backend As A Service or BAAS, meaning it is a NoSQL data store that is in the cloud that your clients can access directly in realtime. Now this means Firebase has it’s own Realtime Database, Cloud Storage, Authentication, Hosting and many other services.

Is Firebase better than SQL?

Unlike SQL there’s no schema for the database, no tables, no columns, it’s just a combination of key/value pairs. Firebase is based on a data structure used by the NoSQL database is vastly different from those used in a relational database. Some operations are faster in NoSQL than relational databases like MySQL.

Which is better Firebase or AWS?

If you’re a small team setting up, Firebase might serve you better than a heavy AWS setup. It also has a lower learning curve than AWS. On the flip side, Firebase makes it difficult to query larger datasets. Their database also doesn’t provide relational data, which could spell trouble for some newer companies.


Can I use Firebase for Free?

Yes, it’s free. You can use Analytics for advanced messaging – out of the box. Which products are paid? Firebase’s paid infrastructure products are the Realtime Database, Firebase Storage, Hosting, and Test Lab.

Is Firebase good for large database?

The real-time functions in Firebase are not only suited for, but designed for large data sets. The fact that records stream in real-time is perfect for this. Performance is, as with any large data app, only as good as your implementation.

Is Firebase a DBMS?

FireBase It’s a cloud service Also FireBase is a NoSQL database. In FireBase Data stored/processed in a cloud. FireBase is suitable for real time applications. MySQL is a relational database management system (RDBMS)

Which language is used in Firebase?

The Firebase SDK supports programming in C++, Java, JavaScript, JavaScript/Node. js, Objective-C, and Swift. Angular, Backbone, Ember and React are supported through bindings to the database.

Can Firebase Handle million users?

This is possible since firebase is just a bucket of data that can be easily allocated for each user. Queries with limited sorting and filtering functionality can be performed with the firebase database. Cloud firestore assures automatic scaling and can handle 1 million concurrent connections and 10,000 writes/second.

Is Firebase secure?

1 Answer. The short answer is yes: by authenticating your users and writing security rules, you can fully restrict read / write access to your Firebase data. In a nutshell, Firebase security is enforced by server-side rules, that you author, and govern read or write access to given paths in your Firebase data tree.

Why Firebase is used in Android?

Firebase allows you to build apps which need authentication, database, file storage, analytics and server side functionality without having to own and manage infrastructure and software required for server side support. Using firebase, you can access google cloud store for storing images from your app.

How fast is Firebase real-time database?

Firebase uses a document storage (JSON Documents), similar to MongoDB, so there are documents with fields and sub documents, and nothing like rows or tables. Back to your question, yes Firebase is blazingly fast. I’ve used Firebase in building two apps of mine, and the response is amazing.

Is learning Firebase easy?

Yes it is really easy for beginners. Firebase has very good documentation. It is easy to connect you application to Firebase. If you are an android developer using Android Studio, you can connect your app to Firebase with just a few clicks using assistant, without using a single line of code.


Google Firebase Account Setup for the ESP32?

You can start by opening Google search engine and search for the Firebase Console.

ESP32 Firebase

After you click on the Firebase Console, this will take you to the Firebase Console main page.

ESP32 Firebase

To start a new project, you can click on the add project. This will open a new page.

ESP32 Firebase



In the first step, I entered my project name as the esp32andfirebase. You can select any project name as you want. After you enter the project name then you can click on the Continue button; this will open the step 2.

ESP32 Firebase

In step 2, you don’t have to do anything, as you can see the Enable Google Analytics for this project is already enabled, if not you can enable this. Now, Simply click on the Continue button. This will take you to the final step 3.

ESP32 Firebase

Click on the drop down menu and select my site.

ESP32 Firebase

 

Click on the Create project button, this will start loading, you can wait for a few seconds.

ESP32 Firebase


After your project is ready, now you can click on the button.

ESP32 Firebase

As you can see a project with the name esp32andfirebase has been created successfully. You can click on this project to continue with the setup.

ESP32 Firebase

On the left side under the Develop, you can see Authentication, Database, Storage, Hosting, Functions, and ML Kit. For now, you can click on the Database.

ESP32 Firebase

Scroll down and search for the Realtime Database, click on the Create database button. This will open the Security rules for Realtime Database.

ESP32 Firebase

Under the Security rules for Realtime Database you are provided with two options

  • Start locked mode
  • Start in test mode


Select the start in test mode and then click on the enable button. After some loading, a new page will open.

ESP32 Firebase

Click on the Plus icon to expand.

ESP32 Firebase

Enter the Name as data and value as 0 “Zero” and then click on the add button.

ESP32 Firebase

Under  the project name esp32andfirebase you can see the default value of 0 stored in the variable data. As currently I am using only one sensor so that’s why I will go with only variable data. If you want you can add multiple variables for storing multiple sensors data. At this point we are done with the Firebase database setup. Now to link this firebase database account with the ESP32, copy the link as you can see in the picture below.

ESP32 Firebase


On the left side you can see the Project Overview, next to it is the settings button, click on this button.

ESP32 Firebase

On the left side you can see the Project Overview, next to it is the settings button, click on this button.

ESP32 Firebase

As you can see, under the Settings we have different menus General, Cloud Messaging, Integrations, Service accounts, Data privacy, and Users and permissions. For now you will have to click on the Service accounts.

ESP32 Firebase

Copy the link under the Firebase service account and paste it in notepad file. We will not use this on the ESP32 side, but may be you will need this for some other project, if you want to use the same account. On the left side you can see the Database secrets, click on this.

ESP32 Firebase


Copy the Database Secret and paste it in the notepad with the other two links.

ESP32 Firebase

For now we will only use the first one and the third one in the ESP32 programming. Don’t share this information with anyone. I am sharing this information with you guys, as I will never use this id and account again.

Anyhow, our Firebase Database settings are completed. Now you can click on the Database button under the Develop as you see on the left side.

ESP32 Firebase

This is ready. Now all we need is to program the ESP32 and use the links that we copied. But before we start the programming, first let me explain the circuit diagram.


ESP32 Firebase, Analog Sensor with ESP32 Circuit Diagram:

ESP32 Firebase

Let’s first start with the regulated 5v power supply based on the LM7805 voltage regulator. This is the same 5v regulated power supply I have been using for the Nodemcu ESP8266 Wifi module.

J1 is the female power jack and this is where we connect a 12v adaptor, battery or a solar panel. Two 470uf capacitors are connected at the input and output sides of the voltage regulator. A 330 ohm resistor is connected in series with a 2.5v led. This is a current limiting resistor. The output of the voltage regulator is connected with the 5v pin of the ESP32 module and the ground of the power supply is connected with the ground of ESP32 module.


About the ESP32 Power Supply PCB Board:

ESP32 Firebase

 

This is the ESP32 power supply board Manufactured by the PCBWay Company. I have already explained the designing and making of this board.

ESP32 Firebase

Finally, I connected a Potentiometer with the Analog pin A0 of the ESP32 Wifi + Bluetooth Module as per the circuit diagram already explained, and connected the ESP32 with the laptop to upload the code given below.

Before, you start the programming; first of all, make sure that you download all the necessary libraries.

Download ESP32 Board Manager URL link, Programming, ESP32 Eagle library, ESP32 Datasheet:



Firebase ESP32 Programming:

/*
 * Firebase ESP32
 * https://www.electroniclinic.com/
 */

#include <WiFi.h>
#include <FirebaseESP32.h>


#define FIREBASE_HOST "https://esp32andfirebase.firebaseio.com/"
#define FIREBASE_AUTH "gga1JXWY5rgyBHk56RSXLn3FPpajWfcq6itIM3nI"
#define WIFI_SSID "AndroidAP7DF8"
#define WIFI_PASSWORD "electroniclinic"


//Define FirebaseESP32 data object
FirebaseData firebaseData;
FirebaseJson json;
int Vresistor = A0; 
int Vrdata = 0; 

void setup()
{

  Serial.begin(115200);
 
 pinMode(Vresistor, INPUT);


  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to Wi-Fi");
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(300);
  }
  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
  Firebase.reconnectWiFi(true);

  //Set database read timeout to 1 minute (max 15 minutes)
  Firebase.setReadTimeout(firebaseData, 1000 * 60);
  //tiny, small, medium, large and unlimited.
  //Size and its write timeout e.g. tiny (1s), small (10s), medium (30s) and large (60s).
  Firebase.setwriteSizeLimit(firebaseData, "tiny");

  /*
  This option allows get and delete functions (PUT and DELETE HTTP requests) works for device connected behind the
  Firewall that allows only GET and POST requests.
  
  Firebase.enableClassicRequest(firebaseData, true);
  */

  //String path = "/data";
  

  Serial.println("------------------------------------");
  Serial.println("Connected...");
  
}

void loop()
{
   Vrdata = analogRead(Vresistor);
 int Sdata = map(Vrdata,0,4095,0,1000);
 Serial.println(Sdata); 
delay(100); 
  json.set("/data", Sdata);
  Firebase.updateNode(firebaseData,"/Sensor",json);

}

After uploading the above program, go back to your Google Firebase account, make sure you connect the ESP32 with the internet through Wifi and you will be able to see the potentiometer value on the screen.

ESP32 Firebase

I was able to monitor the potentiometer in real-time. For the practical demonstration and step by step explanation watch video given below.


Watch Video Tutorial:

 

 

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

2 Comments

  1. Hi, I would like the esp32 to send the data to firebase database but that they remain saved, what could I do?

Leave a Reply

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

Back to top button