0% found this document useful (0 votes)
3 views52 pages

ESP32 - Firebase

The document provides a guide on how to get started with Firebase for app development, specifically using the ESP32 to interact with a Realtime Database. It outlines steps for creating a Firebase project, setting up authentication, and programming the ESP32 to store and read data from the database. The guide emphasizes using tools like VS Code with PlatformIO for programming and includes instructions for installing necessary libraries and writing code for data operations.

Uploaded by

K SD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views52 pages

ESP32 - Firebase

The document provides a guide on how to get started with Firebase for app development, specifically using the ESP32 to interact with a Realtime Database. It outlines steps for creating a Firebase project, setting up authentication, and programming the ESP32 to store and read data from the database. The guide emphasizes using tools like VS Code with PlatformIO for programming and includes instructions for installing necessary libraries and writing code for data operations.

Uploaded by

K SD
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

ESP32: Getting Started

with

(Real-time Database)
What is Firebase?
• It is Google’s Mobile Application Development Platform

“Firebase is a toolset to “Build, Improve, and Grow Your App”, and the
tools it gives you cover a large portion of the services that developers would
normally have to build themselves but don’t really want to build because
they’d rather be focusing on the app experience itself.
This includes things like Analytics, Authentication, Databases,
Configuration, File Storage, Push Messaging, and the list goes on.
The services are hosted in the cloud and scale with little to no effort on the
part of the developer.”
Task 1:
Task 2:
This task 1 is divided into three sections.

Step 1:
• Create a Firebase Project - set up a Firebase project and create a
realtime database

Step 2:
• ESP32: Store data to the Firebase Realtime Database

Step 3:
• ESP32: Read data from the Firebase Realtime Database
Set Up a Firebase Account and Create a
New Project
Creating a New Project
Follow the instructions to create a new project on Firebase.

1. Go to Firebase and sign in using a Google Account.


2. Go to the Firebase Console and create a new project.
3. Give a name to your project, for example: ESP-Project, and click
Continue.
Enable or Disable AI
assistance for your
project (This is optional)

Click on “Continue”
Disable the option Enable
Google Analytics for this
project, as it is not needed.

Then, click “Create


project”.
It will take a few seconds to set up
your project.

Click “Continue” when it’s ready.


You’ll be redirected to your Project Console Page.
Set Authentication Methods
We need to set authentication methods for your app.

“Most apps need to know the identity of a user.


In other words, it takes care of logging in and identifying the users.
Knowing a user’s identity allows an app to securely save user data in the
cloud …”
On the left sidebar, click on Build > Authentication and then on Get started.
There are several authentication methods like email and password, Google Account, Facebook account, and others.
Select Email/Password
Enable that authentication method. Then, click Save.
At the top, click on the Users tab. Then, click on Add user.
Create a new user with an email and password.
Finally, click on Add user.
Creating a Realtime Database

Click on Build > Real-time Database and then, click on Create Database.
Select your database location. It should be the closest to your location
Set up security rules for your database. For testing purposes, select “Start in test mode” and Click on “Enable”
Your database is now created. You need to copy and save the database URL

The Realtime Database is all set.


Now, you also need to get your project API key.
Get Project API Key
Copy the API Key to a safe place because you’ll need it later
Now, you have
everything ready to
interface the
ESP32 with the
database.
Program the ESP32 to Interface with Firebase

• To program the ESP32, you can use Arduino IDE, VS Code with
the PlatformIO extension or pioarduino, or other suitable software.

• For Firebase projects, we recommend using VS Code with the


PlatformIO or pioarduino extension.

• If you plan to develop a web application to connect the ESP32


with Firebase, VS Code offers all the tools you need.
Installing PlatformIO in VS Code
Installing the FirebaseClient Library

• Select the Libraries


tab.
• Search for
“FirebaseClient”.
• Select the Firebase
Client Library by
Mobitz.
Click Add to Project and select the project you’re working on.
Installation – Arduino IDE

• Search
for “FirebaseClient” in
Library Manager
• Install
the “FirebaseClient” by
Mobizt.
ESP32 Store Data to Firebase Database
• Write the code to your Arduino IDE.

• Here in our program we inserts an int, a float


number, and a string into the database every
10 seconds.

• This is a simple example that shows how to


connect the ESP32 to the database to store
data.
If everything works as expected, the
values should be stored in the database,
and you should get success messages.
Go to your project’s Firebase Realtime database,
and you’ll see the values saved on the different
node paths.
Every 10 seconds, it saves a new value.
The database blinks when new values are saved.
ESP32 Read From Firebase Database
• We’ll read the data stored in the previous section.
• Remember that we saved
• an int value in the test/int path,
• a float value in the test/float
• a String value in the test/string path.

• There are different ways to get values from the database.


1. SYNCHRONOUSLY and
2. ASYNCHRONOUSLY.
ESP32 – Read From Firebase RTDB (Async Mode)

• It connects and authenticates to Firebase and sets


up the database connection.
• Then, it gets the data from the database in async
mode.
• The results of the operation (the values of the
variables) are then handled in the callback function.
Upload the code to your board.
Then, open the Serial Monitor at a baud
rate of 115200.
After a few seconds, it will print the
values saved in the database.
ESP32 – Read From Firebase RTDB (No Async Mode)
• For simpler applications where you don’t need to queue tasks and
blocking code is not critical while waiting for a database operation,
you can use the get() function in a simplified format.
• This allows you to directly retrieve a value from the Firebase Realtime
Database and immediately store it in a variable.

For Example:
int intValue = Database.get<int>(aClient, "/test/int");
• Congratulations!, you’ve created a Firebase project with a
Realtime Database and learned how to store and read data from
the database using the ESP32.

• To keep things simple, we’ve stored sample values on the database.


The idea is to save useful data like sensor readings or GPIO states.

You might also like