This is an old revision of the document!
In this lab assignment we'll explore hot to leverage online databases to write and read sensor data from an IoT application and access this data from anywhere in the world. We'll use two similar DB platforms: InfluxDB and Firebase.
Firebase is Google’s set of hosting services for mobile applications, which comprises a wide range of solutions to manage data from Android, iOS or web applications. We will use the real-time database solution from Firebase (RTDB) and we will store and read values from the DB using our ESP32 boards.
We can send and receive data with the ESP32 board to/from Firebase from anywhere in the world, as long as we have an Internet connection. This, coupled with the fact that we can create a web app that can be accessed anywhere with an Internet connection makes Firebase a very powerful tool, as you can manage, monitor, automate and send commands to an IoT application remotely.
First step is to create a Firebase project with a real-time database and use the ESP32 to write and read data from this database.
If everything went well, you should be able to see your project page displayed in a few seconds.
We need to set an authentication method for our app.
In the left sidebar, click on Build → Authentication → Get Started
There are several authentication methods from different sign-in providers. For the purpose of this app where the ESP32 board connects directly to the Firebase app, we can select to have no authentication. Select Anonymous from the list, click to enable and the Save.
In the left sidebar, click on Build → Realtime Database → Create Database. In the pop-up window that appears, select the DB location (recommended to select the Belgium site) and “Start in Test Mode” in the next tab, then click Enable.
Your database is now created, you will need to copy the URL in the web page, as we will be using it from the ESP32 side to connect to our database.
You'll also need to get the API key, by clicking on the cog next to Project Overview:
With this, you're all set up on the DB side!
We will use the Firebase-ESP-Client library, which we will need to install.
Let's first write an application that can connect to the Firebase DB and send some data.
Use the code below, replace the credentials at the beginning of the code (WiFi AP, password, DB URL and API Key) and the upload it to your ESP32 Sparrow board.
#include <Arduino.h> #include <WiFi.h> #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()); } } }