ESP32: Getting Started With Firebase (Realtime Database)

This guide will get you started quickly with Firebase using the ESP32 board. Firebase is Google’s mobile application development platform that includes many services to manage data from IOS, Android, or web applications. You’ll create a Firebase project with a realtime database (RTDB), and you’ll learn how to store and read values from the database with your ESP32.

Getting Started with ESP32 with Firebase: Realtime Database Arduino IDE

In a later tutorial, you’ll learn how to create a Firebase web app that you can access from anywhere to monitor and control your ESP32 using firebase’s realtime database:

  • ESP32 with Firebase – Creating a Web App

We have a similar tutorial for the ESP8266 board: Getting Started with Firebase (Realtime Database)

What is Firebase?

Firebase logo Realtime Database

Firebase is Google’s mobile application development platform that helps you build, improve, and grow your app. It has many services used to manage data from any android, IOS, or web application.

The following paragraph clearly explains the advantages of using Firebase:

“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.”

This paragraph was taken from this article, and we recommend that you read that article if you want to understand better what firebase is and what it allows you to do.

You can use the ESP32 to connect and interact with your Firebase project, and you can create applications to control the ESP32 via Firebase from anywhere in the world.

In this tutorial, we’ll create a Firebase project with a realtime database, and we’ll use the ESP32 to store and read data from the database. The ESP32 can interact with the database from anywhere in the world as long as it is connected to the internet.

This means that you can have two ESP32 boards in different networks, with one board storing data and the other board reading the most recent data, for example.

ESP32 Firebase Project Introduction realtime database

In a later tutorial, we’ll create a web app using Firebase that will control the ESP32 to display sensor readings or control outputs from anywhere in the world.

Firebase ESP32 Project Web App Project Example

Project Overview

In this tutorial, you’ll learn how to create a Firebase project with a realtime database and store and read data from the database using the ESP32.

To follow this project, first, you need to set up a Firebase project and create a realtime database for that project. Then, you’ll program the ESP32 to store and read data from the database. This tutorial is divided into three sections.

  1. Create a Firebase Project
  2. ESP32: Store data to the Firebase Realtime Database
  3. ESP32: Read data from the Firebase Realtime Database

Let’s get started!

Set Up a Firebase Account and Create a New Project

1.Create a New Project

Follow the next instructions to create a new project on Firebase.

  1. Go to Firebase and sign in using a Google Account.
  2. Click Get Started, and then Add project to create a new project.
  3. Give a name to your project, for example: ESP32 Firebase Demo.Set Up Firebase Project for ESP32 and ESP8266 Step 1
  4. Disable the option Enable Google Analytics for this project as it is not needed and click Create project.Set Up Firebase Project for ESP32 and ESP8266 Step 2
  5. It will take a few seconds setting up your project. Then, click Continue when it’s ready.Set Up Firebase Project for ESP32 and ESP8266 Step 3
  6. You’ll be redirected to your Project console page.Set Up Firebase Project for ESP32 and ESP8266 Step 4

2. Set Authentication Methods

You 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 identify the users (in this case, the ESP32). Knowing a user’s identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user’s devices.” To learn more about the authentication methods, you can read the documentation.

  1. On the left sidebar, click on Authentication and then on Get started. Set Up Firebase Project for ESP32 and ESP8266 Authentication Methods
  2. There are several authentication methods like email and password, Google Account, Facebook account, and others.Set Up Firebase Project for ESP32 and ESP8266 Authentication Methods List
  3. For testing purposes, we can select the Anonymous user (require authentication without requiring users to sign in first by creating temporary anonymous accounts). Enable that option and click Save.Set Up Firebase Project for ESP32 and ESP8266 Authentication Methods Anonymous User

3. Creating a Realtime Database

The next step is creating a Realtime Database for your project. Follow the next steps to create the database.

  1. On the left sidebar click on Realtime Database and then, click on Create Database.Set up realtime database firebase ESP32 ESP8266 Step1
  2. Select your database location. It should be the closest to your location.Set up realtime database firebase ESP32 ESP8266 Select Location
  3. Set up security rules for your database. For testing purposes, select Start in test mode. In later tutorials you’ll learn how to secure your database using database rules.Set up realtime database firebase ESP32 ESP8266 Set Security Rules
  4. Your database is now created. You need to copy and save the database URL—highlighted in the following image—because you’ll need it later in your ESP32 code.Set up realtime database firebase ESP32 ESP8266 Database Created

The Realtime Database is all set. Now, you also need to get your project API key.

4. Get Project API Key

  1. To get your project’s API key, on the left sidebar click on Project Settings.Get Firebase Project API Key
  2. Copy the API Key to a safe place because you’ll need it later.Firebase Project API key

Now, you have everything ready to interface the ESP32 with the database.

Program the ESP32 to Interface with Firebase

Now that the Firebase Realtime Database is created, you’ll learn how to interface the ESP32 with the database.

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

Note: for firebase projects, we recommend using VS Code with the PlatformIO extension because if you want to develop a web application to make the bridge between the ESP32 and Firebase, VS Code provides all the tools to do that. However, we won’t build the web application in this tutorial, so you can use Arduino IDE.

Install the Firebase-ESP-Client Library

There is a library with lots of examples to use Firebase with the ESP32: the Firebase-ESP-Client library. This library is compatible with both the ESP32 and ESP8266 boards.

In this tutorial, we’ll look at simple examples to store and read data from the database. The library provides many other examples that you can check here. It also provides detailed documentation explaining how to use the library.

Installation – VS Code + PlatformIO

If you’re using VS Code with the PlatformIO extension, click on the PIO Home icon and then select the Libraries tab. Search for “Firebase ESP Client“. Select the Firebase Arduino Client Library for ESP8266 and ESP32.

Install Firebase ESP Client Library VS Code

Then, click Add to Project and select the project you’re working on.

Add Firebase ESP Client Library to Project VS Code

Also, change the monitor speed to 115200 by adding the following line to the platformio.ini file of your project:

monitor_speed = 115200

Installation – Arduino IDE

If you’re using Arduino IDE, follow the next steps to install the library.

  1. Go to Sketch > Include Library > Manage Libraries
  2. Search for Firebase ESP Client and install the Firebase Arduino Client Library for ESP8266 and ESP32 by Mobitz.

Note: We are using version 2.3.7. If you have issues compiling your code with more recent versions of the library, downgrade to version 2.3.7.

Install Firebase Arduino Client Library for ESP8266 and ESP32 by Mobitz

Now, you’re all set to start programming the ESP32 board to interact with the database.

ESP32 Store Data to Firebase Database

ESP32 Firebase store data realtime database project example

Copy the following code to your Arduino IDE. This sketch inserts an int and a float number into the database every 15 seconds. This is a simple example showing you how to connect the ESP32 to the database and store data. This is also compatible with ESP8266 boards.

Note: We are using version 2.3.7 of the Firebase ESP Client library. If you have issues compiling your code with more recent versions of the library, downgrade to version 2.3.7.

/* Rui Santos Complete project details at our blog. - ESP32: https://RandomNerdTutorials.com/esp32-firebase-realtime-database/ - ESP8266: https://RandomNerdTutorials.com/esp8266-nodemcu-firebase-realtime-database/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Based in the RTDB Basic Example by Firebase-ESP-Client library by mobizt https://github.com/mobizt/Firebase-ESP-Client/blob/main/examples/RTDB/Basic/Basic.ino */ #include <Arduino.h> #if defined(ESP32) #include <WiFi.h> #elif defined(ESP8266) #include <ESP8266WiFi.h> #endif #include <Firebase_ESP_Client.h> //Provide the token generation process info. #include "addons/TokenHelper.h" //Provide the RTDB payload printing info and other helper functions. #include "addons/RTDBHelper.h" // Insert your network credentials #define WIFI_SSID "REPLACE_WITH_YOUR_SSID" #define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD" // Insert Firebase project API Key #define API_KEY "REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY" // Insert RTDB URLefine the RTDB URL */ #define DATABASE_URL "REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL" //Define Firebase Data object FirebaseData fbdo; FirebaseAuth auth; FirebaseConfig config; unsigned long sendDataPrevMillis = 0; int count = 0; bool signupOK = false; void setup(){ Serial.begin(115200); 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(); /* Assign the api key (required) */ config.api_key = API_KEY; /* Assign the RTDB URL (required) */ config.database_url = DATABASE_URL; /* Sign up */ if (Firebase.signUp(&config, &auth, "", "")){ Serial.println("ok"); signupOK = true; } else{ Serial.printf("%s\n", config.signer.signupError.message.c_str()); } /* Assign the callback function for the long running token generation task */ config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h Firebase.begin(&config, &auth); Firebase.reconnectWiFi(true); } void loop(){ if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)){ sendDataPrevMillis = millis(); // Write an Int number on the database path test/int if (Firebase.RTDB.setInt(&fbdo, "test/int", count)){ Serial.println("PASSED"); Serial.println("PATH: " + fbdo.dataPath()); Serial.println("TYPE: " + fbdo.dataType()); } else { Serial.println("FAILED"); Serial.println("REASON: " + fbdo.errorReason()); } count++; // Write an Float number on the database path test/float if (Firebase.RTDB.setFloat(&fbdo, "test/float", 0.01 + random(0,100))){ Serial.println("PASSED"); Serial.println("PATH: " + fbdo.dataPath()); Serial.println("TYPE: " + fbdo.dataType()); } else { Serial.println("FAILED"); Serial.println("REASON: " + fbdo.errorReason()); } } }

View raw code

You need to insert your network credentials, URL database, and project API key for the project to work.

This sketch was based on the basic example provided by the library. You can find more examples here.

How the Code Works

Continue reading to learn how the code works, or skip to the demonstration section.

First, include the required libraries. The WiFi.h library to connect the ESP32 to the internet (or the ESP8266WiFi.h in case of the ESP8266 board) and the Firebase_ESP_Client.h library to interface the boards with Firebase.

#include <Arduino.h> #if defined(ESP32) #include <WiFi.h> #elif defined(ESP8266) #include <ESP8266WiFi.h> #endif #include <Firebase_ESP_Client.h>

You also need to include the following for the Firebase library to work.

//Provide the token generation process info. #include "addons/TokenHelper.h" //Provide the RTDB payload printing info and other helper functions. #include "addons/RTDBHelper.h"

Include your network credentials in the following lines.

#define WIFI_SSID "REPLACE_WITH_YOUR_SSID" #define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD"

Insert your firebase project API key—the one you’ve gotten in section 4.1.

#define API_KEY "REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY"

Insert your database URL—see section 3.4.

#define DATABASE_URL "REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL"

setup()

In the setup(), connect your board to your network.

Serial.begin(115200); 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();

Assign the API key and the database URL to the Firebase configuration.

/* Assign the api key (required) */ config.api_key = API_KEY; /* Assign the RTDB URL (required) */ config.database_url = DATABASE_URL;

The following lines take care of the signup for an anonymous user. Notice that you use the signUp() method, and the last two arguments are empty (anonymous user).

/* Sign up */ if (Firebase.signUp(&config, &auth, "", "")){ Serial.println("ok"); signupOK = true; } else{ Serial.printf("%s\n", config.signer.signupError.message.c_str()); }

Note: in the anonymous user signup, every time the ESP connects, it creates a new anonymous user.

If the sign-in is successful, the signupOK variable changes to true.

signupOK = true;

The library provides examples for other authentication methods like signing in as a user with email and password, using the database legacy auth token, etc. You can check all the examples for other authentication methods here. If you end up using other authentication methods, don’t forget that you need to enable them on your firebase project (Build > Authentication > Sign-in method).

loop()

In the loop(), we’ll send data to the database periodically (if the signup is successful and everything is set up).

if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)){

Send Data to the Database

As mentioned in the library documentation, to store data at a specific node in the Firebase RTDB (realtime database), use the following functions: set, setInt, setFloat, setDouble, setString, setJSON, setArray, setBlob, and setFile.

These functions return a boolean value indicating the success of the operation, which will be true if all of the following conditions are met:

  • Server returns HTTP status code 200.
  • The data types matched between request and response.Only setBlob and setFile functions that make a silent request to Firebase server, thus no payload response returned.

In our example, we’ll send an integer number, so we need to use the setInt() function as follows:

Firebase.RTDB.setInt(&fbdo, "test/int", count)

The second argument is the database node path, and the last argument is the value you want to pass to that database path—you can choose any other database path. In this case, we’re passing the value saved in the count variable.

Here’s the complete snippet that stores the value in the database and prints a success or failed message.

if (Firebase.RTDB.setInt(&fbdo, "test/int", count)) { Serial.println("PASSED"); Serial.println("PATH: " + fbdo.dataPath()); Serial.println("TYPE: " + fbdo.dataType()); } else { Serial.println("FAILED"); Serial.println("REASON: " + fbdo.errorReason()); }

We proceed in a similar way to store a float value. We’re storing a random float value on the test/float path.

// Write an Float number on the database path test/float if (Firebase.RTDB.setFloat(&fbdo, "test/float", 0.01 + random(0, 100))) { Serial.println("PASSED"); Serial.println("PATH: " + fbdo.dataPath()); Serial.println("TYPE: " + fbdo.dataType()); } else { Serial.println("FAILED"); Serial.println("REASON: " + fbdo.errorReason()); }

Demonstration

Upload the code to your ESP32 board. Don’t forget to insert your network credentials, database URL path, and the project API key.

After uploading the code, open the Serial Monitor at a baud rate of 115200 and press the ESP32 on-board reset button so it starts running the code.

If everything works as expected, the values should be stored in the database, and you should get success messages.

ESP32 ESP8266 Store value firebase database Serial Monitor Success

Go to your project’s Firebase Realtime database, and you’ll see the values saved on the different node paths. Every 15 seconds, it saves a new value. The database blinks when new values are saved.

ESP32 Store value firebase database Success

Congratulations! You’ve successfully stored data in Firebase’s realtime database using the ESP32. In the next section, you’ll learn to read values from the different database’s node paths.

ESP32 Read From Firebase Database

ESP32 Firebase read data realtime database

In this section, you’ll learn how to read data from the database. We’ll read the data stored in the previous section. Remember that we saved an int value in the test/int path and a float value in the test/float path.

The following example reads the values stored in the database. Upload the following code to your board. You can use the same ESP32 board or another board to get the data posted by the previous ESP32.

Note: We are using version 2.3.7 of the Firebase ESP Client library. If you have issues compiling your code with more recent versions of the library, downgrade to version 2.3.7.

/* Rui Santos Complete project details at our blog. - ESP32: https://RandomNerdTutorials.com/esp32-firebase-realtime-database/ - ESP8266: https://RandomNerdTutorials.com/esp8266-nodemcu-firebase-realtime-database/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. Based in the RTDB Basic Example by Firebase-ESP-Client library by mobizt https://github.com/mobizt/Firebase-ESP-Client/blob/main/examples/RTDB/Basic/Basic.ino */ #include <Arduino.h> #if defined(ESP32) #include <WiFi.h> #elif defined(ESP8266) #include <ESP8266WiFi.h> #endif #include <Firebase_ESP_Client.h> //Provide the token generation process info. #include "addons/TokenHelper.h" //Provide the RTDB payload printing info and other helper functions. #include "addons/RTDBHelper.h" // Insert your network credentials #define WIFI_SSID "REPLACE_WITH_YOUR_SSID" #define WIFI_PASSWORD "REPLACE_WITH_YOUR_PASSWORD" // Insert Firebase project API Key #define API_KEY "REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY" // Insert RTDB URLefine the RTDB URL */ #define DATABASE_URL "REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL" //Define Firebase Data object FirebaseData fbdo; FirebaseAuth auth; FirebaseConfig config; unsigned long sendDataPrevMillis = 0; int intValue; float floatValue; bool signupOK = false; void setup() { Serial.begin(115200); 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(); /* Assign the api key (required) */ config.api_key = API_KEY; /* Assign the RTDB URL (required) */ config.database_url = DATABASE_URL; /* Sign up */ if (Firebase.signUp(&config, &auth, "", "")) { Serial.println("ok"); signupOK = true; } else { Serial.printf("%s\n", config.signer.signupError.message.c_str()); } /* Assign the callback function for the long running token generation task */ config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h Firebase.begin(&config, &auth); Firebase.reconnectWiFi(true); } void loop() { if (Firebase.ready() && signupOK && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0)) { sendDataPrevMillis = millis(); if (Firebase.RTDB.getInt(&fbdo, "/test/int")) { if (fbdo.dataType() == "int") { intValue = fbdo.intData(); Serial.println(intValue); } } else { Serial.println(fbdo.errorReason()); } if (Firebase.RTDB.getFloat(&fbdo, "/test/float")) { if (fbdo.dataType() == "float") { floatValue = fbdo.floatData(); Serial.println(floatValue); } } else { Serial.println(fbdo.errorReason()); } } }

View raw code

Don’t forget to insert your network credentials, database URL, and API key.

How the Code Works

The code is very similar to the previous section’s example, but it reads data from the database. Let’s take a look at the relevant parts for this section.

Data at a specific node in Firebase RTDB can be read through the following functions: get, getInt, getFloat, getDouble, getBool, getString, getJSON, getArray, getBlob, getFile.

These functions return a boolean value indicating the success of the operation, which will be true if all of the following conditions were met:

  • Server returns HTTP status code 200
  • The data types matched between request and response.

The database data’s payload (response) can be read or access through the following Firebase Data object’s functions: fbdo.intData, fbdo.floatData, fbdo.doubleData, fbdo.boolData, fbdo.stringData, fbdo.jsonString, fbdo.jsonObject, fbdo.jsonObjectPtr, fbdo.jsonArray, fbdo.jsonArrayPtr, fbdo.jsonData (for keeping parse/get result), and fbdo.blobData.

If you use a function that doesn’t match the returned data type in the database, it will return empty (string, object, or array).

The data type of the returning payload can be determined by fbdo.getDataType.

The following snippet shows how to get an integer value stored in the test/int node. First, we use the getInt() function; then, we check if the data type is an integer with fbdo.dataType(), and finally, the fdbo.intData() gets the value stored in that node.

if (Firebase.RTDB.getInt(&fbdo, "/test/int")) { if (fbdo.dataType() == "int") { intValue = fbdo.intData(); Serial.println(intValue); } } else { Serial.println(fbdo.errorReason()); }

We use a similar snippet to get the float value.

if (Firebase.RTDB.getFloat(&fbdo, "/test/float")) { if (fbdo.dataType() == "float") { floatValue = fbdo.floatData(); Serial.println(floatValue); } } else { Serial.println(fbdo.errorReason()); }

Demonstration

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 on the database.

ESP32 Store value firebase database Serial Monitor Success

Wrapping Up

Congratulations! In this tutorial, 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.

Then, you can access the database with another ESP32 to get the data or create a Firebase web app to use that data to display sensor readings or control the ESP32 GPIOs from anywhere in the world. We cover the basics of how to create a Firebase Web App in this tutorial.

We hope you find this tutorial useful. If you want to learn more about Firebase with the ESP32 and ESP8266 boards, check out our new eBook:

  • Firebase Web App with ESP32 and ESP8266

If you want to learn more about the ESP32, check our courses:

  • Learn ESP32 with Arduino IDE (eBook + video course)
  • Build Web Servers with ESP32 and ESP8266 eBook (2nd Edition)
  • More ESP32 Projects and Tutorials …

Từ khóa » Thư Viện Firebase Arduino