{"id":108623,"date":"2025-05-12T06:06:31","date_gmt":"2025-05-12T06:06:31","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=108623"},"modified":"2025-05-12T17:07:08","modified_gmt":"2025-05-12T17:07:08","slug":"esp32-data-logging-firebase-realtime-database","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp32-data-logging-firebase-realtime-database\/","title":{"rendered":"ESP32 Data Logging to Firebase Realtime Database"},"content":{"rendered":"\n<p>In this guide, you&#8217;ll learn how to log data with the ESP32 to the Firebase Realtime Database with timestamps (data logging) so that you have a record of your data history. As an example, we&#8217;ll log temperature, humidity, and pressure from a BME280 sensor and we&#8217;ll get timestamps from an NTP server. Then, you can access the data using the Firebase console, or build a web app to display the results (<a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-gauges-charts\/\">check this tutorial<\/a>).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" fetchpriority=\"high\" decoding=\"async\" width=\"1200\" height=\"675\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-ESP8266-BME280-Datalogging-Firebase.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Data Logging to Firebase Realtime Database\" class=\"wp-image-170057\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-ESP8266-BME280-Datalogging-Firebase.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-ESP8266-BME280-Datalogging-Firebase.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-ESP8266-BME280-Datalogging-Firebase.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-ESP8266-BME280-Datalogging-Firebase.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-ESP8266-BME280-Datalogging-Firebase.jpg?resize=1536%2C864&amp;quality=100&amp;strip=all&amp;ssl=1 1536w\" sizes=\"(max-width: 1200px) 100vw, 1200px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclgray\"><em>Updated 12 May 2025<\/em><\/p>\n\n\n\n<p> <strong style=\"font-size: inherit;\">Part 2:<\/strong><span style=\"font-size: inherit;\"> <\/span><a style=\"font-size: inherit;\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-gauges-charts\/\">ESP32\/ESP8266: Firebase Data Logging Web App (Gauges, Charts, and Table)<\/a><span style=\"font-size: inherit;\"> <\/span><\/p>\n\n\n\n<p>Other Firebase Tutorials with the ESP32\/ESP8266 that you might be interested in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\">ESP32: Getting Started with Firebase (Realtime Database)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-firebase-realtime-database\/\">ESP8266 NodeMCU: Getting Started with Firebase (Realtime Database)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-web-app\/\">ESP32 with Firebase \u2013 Creating a Web App<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-firebase-web-app\/\">ESP8266 NodeMCU with Firebase \u2013 Creating a Web App<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-authentication\/\">ESP32\/ESP8266 Firebase Authentication (Email and Password)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-bme280-rtdb\/\">ESP32\/ESP8266 Firebase: Send BME280 Sensor Readings to the Realtime Database<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-web-app-sensor\/\">ESP32\/ESP8266: Firebase Web App to Display Sensor Readings (with Authentication)<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is Firebase?<\/h2>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"196\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Firebase-Logo.png?resize=750%2C196&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Logo\" class=\"wp-image-169096\" style=\"width:375px;height:106px\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Firebase-Logo.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Firebase-Logo.png?resize=300%2C78&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Firebase is Google&#8217;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 like <a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-authentication\/\">authentication<\/a>, <a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\">realtime database<\/a>, <a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-web-app\/\">hosting<\/a>, etc.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>The following diagram shows a high-level overview of the project we&#8217;ll build.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"750\" height=\"573\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/Firebase-BME280-ESP32-Datalogging-N.png?resize=750%2C573&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 with BME280 Firebase Datalogging\" class=\"wp-image-170035\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/Firebase-BME280-ESP32-Datalogging-N.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/Firebase-BME280-ESP32-Datalogging-N.png?resize=300%2C229&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<ol class=\"wp-block-list\">\n<li>The ESP32 authenticates as a user with email and password (that user must be set on the Firebase authentication methods);<\/li>\n\n\n\n<li>After authentication, the ESP gets the user UID;<\/li>\n\n\n\n<li>The database is protected with security rules. The user can only access the database nodes under the node with its user UID. After getting the user UID, the ESP can publish data to the database;<\/li>\n\n\n\n<li>The ESP32 gets temperatrure, humidity and pressure from the BME280 sensor.<\/li>\n\n\n\n<li>It gets epoch time right after gettings the readings (timestamp).<\/li>\n\n\n\n<li>The ESP32 sends temperature, humidity, pressure and timestamp to the database.<\/li>\n\n\n\n<li>New readings are added to the database periodically. You&#8217;ll have a record of all readings on the Firebase realtime database.<\/li>\n<\/ol>\n\n\n\n<p>These are the main steps to complete this project:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#create-firebase-project\">Create Firebase Project<\/a><\/li>\n\n\n\n<li><a href=\"#Set-Authentication-Methods\">Set Authentication Methods<\/a><\/li>\n\n\n\n<li><a href=\"#firebase-project-api-key\">Get Project API Key<\/a><\/li>\n\n\n\n<li><a href=\"#Set-up-Realtime-Database\">Set up Realtime Database<\/a><\/li>\n\n\n\n<li><a href=\"#Set-up-Database-Security-Rules\">Set up Database Security Rules<\/a><\/li>\n\n\n\n<li><a href=\"#ESP-Datalogging-RTDB\">ESP32 Datalogging (Firebase Realtime Database)<\/a><\/li>\n<\/ol>\n\n\n\n<p>You can continue with the Firebase project <strong><a href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-authentication\/\">from this previous<\/a> tutorial<\/strong> or create a new project. If you use the Firebase project from that previous tutorial, you can skip to section <a style=\"font-size: inherit;\" href=\"#Set-up-Realtime-Database\">4) Set up Realtime Database<\/a> because the authentication methods are already set up.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Preparing Arduino IDE<\/h2>\n\n\n\n<p>For this tutorial, we&#8217;ll program the ESP32 board using the Arduino core. So, make sure you have the ESP32 add-on installed in your Arduino IDE:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/installing-esp32-arduino-ide-2-0\/\" title=\"\">Installing the ESP32 Board in Arduino IDE (Windows, Mac OS X, Linux)<\/a><\/li>\n<\/ul>\n\n\n\n<p>If you want to program the ESP32\/ESP8266 boards using VS Code with the pioarduino or PlatformIO extensions, follow these tutorials instead:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/vs-code-pioarduino-ide-esp32\/\">VS Code and pioarduino IDE: Programming the ESP32 (Windows, Mac OS X, Linux)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/vs-code-platformio-ide-esp32-esp8266-arduino\/\">Getting Started with VS Code and PlatformIO IDE for ESP32 and ESP8266<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-firebase-project\">1) <span style=\"font-size: inherit;\">Create a Firebase Project<\/span><\/h2>\n\n\n\n<p>Follow the next instructions to create a new project on Firebase.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <a href=\"https:\/\/firebase.google.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">Firebase <\/a>and sign in using a Google Account.<\/li>\n\n\n\n<li>Go to the <a href=\"https:\/\/console.firebase.google.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">Firebase Console<\/a> and create a new project.<\/li>\n\n\n\n<li>Give a name to your project, for example: <em>ESP-Project,<\/em> and click <strong>Continue<\/strong>.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105729\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/1-create-firebase-project.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Set Up Firebase Project for ESP32 and ESP8266 Step 1\"><\/figure><\/li>\n\n\n\n<li>Next, enable or disable AI assistance for your project. This is optional.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105729\" style=\"width: 739px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/2-create-firebase-project-ai-assistance.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Set Up Firebase Project for ESP32 and ESP8266 - Enable AI assistant\"><\/figure><\/li>\n\n\n\n<li>Disable the option <em>Enable Google Analytics<\/em> for this project, as it is not needed. Then, click <strong>Create project<\/strong>.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105730\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/3-create-firebase-project.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Disable Google Analytics for firebase project\"><\/figure><\/li>\n\n\n\n<li>It will take a few seconds to set up your project. Click <strong>Continue <\/strong>when it&#8217;s ready.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105731\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/5-Firebase-project-ready.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Project for ESP32 Ready\"><\/figure><br><\/li>\n\n\n\n<li>You&#8217;ll be redirected to your Project console page.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105732\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/6-Firebase-console-ESP-project.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase console project\"><\/figure><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Set-Authentication-Methods\">2) Set Authentication Methods<\/h2>\n\n\n\n<p>To allow authentication with email and password, first, you need to set authentication methods for your app. <\/p>\n\n\n\n<p>&#8220;Most apps need to know the identity of a user. In other words, it takes care of logging in and identifying the users (in this case, the ESP32). Knowing a user&#8217;s identity allows an app to securely save user data in the cloud and provide the same personalized experience across all of the user&#8217;s devices.&#8221; To learn more about the authentication methods, you can <a href=\"https:\/\/firebase.google.com\/docs\/auth\" target=\"_blank\" rel=\"noreferrer noopener\">read the documentation<\/a>.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>On the left sidebar, click on <strong>Build <\/strong>&gt; <strong>Authentication <\/strong>and then on <strong>Get started<\/strong>. <br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105735\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/7-set-authentication.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase project set authentication\"><\/figure><\/li>\n\n\n\n<li>There are several authentication methods like email and password, Google Account, Facebook account, and others.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105740\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/8-Firebase-authentication-methods.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"SFirebase authentication methods\"><\/figure><\/li>\n\n\n\n<li>Select <strong>Email\/Password<\/strong> and <strong>enable <\/strong>that authentication method. Then, click <strong>Save<\/strong>.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/9-enable-email-password-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Enable Email Password Sign in Firebase\"><\/figure><\/li>\n\n\n\n<li>Then, at the top, click on the <strong>Users <\/strong>tab. Then, click on <strong>Add user<\/strong>. <figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/10-add-user-email-password-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Create a new user\"><\/figure><\/li>\n\n\n\n<li>Create a new user with an email and password. The email can be your personal email. Create a password for that user (you need to remember the password later). Finally, click on <strong>Add user<\/strong>.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/11-add-user-email-pass.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase add user email and password\"><\/figure><\/li>\n\n\n\n<li>The User will show up on the list of users. You can see information about the user, like when it was created, the last time it signed in, and its user UID.<figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105741\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/05\/user-created-in-firebase-esp32.jpg?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase User Created\"><\/figure><\/li>\n<\/ol>\n\n\n\n<p>Firebase creates a unique UID for each registered user. The user UID allows us to identify the user and keep track of the user to provide or deny access to the project or the database. There&#8217;s also a column that registers the date of the last sign-in. At the moment, it is empty because we haven&#8217;t signed in with that user yet.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"firebase-project-api-key\">3) Get Project API Key<\/h2>\n\n\n\n<p>To interface with your Firebase project using the ESP32 or ESP8266 boards, you need to get your project API key. Follow the next steps to get your project API key.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>To get your project&#8217;s API key, on the left sidebar click on <strong>Project Settings<\/strong>.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105748\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/17-project-settings-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Project Settings\"><\/figure><\/li>\n\n\n\n<li>Copy the API Key to a safe place because you&#8217;ll need it later.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105749\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/18-project-api-key-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Project API Key\"><\/figure><\/li>\n<\/ol>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Set-up-Realtime-Database\">4) Set up the Realtime Database<\/h2>\n\n\n\n<p>Now, let&#8217;s create a realtime database and set up database rules for our project.<\/p>\n\n\n\n<p><strong>1)<\/strong> On the left sidebar, click on&nbsp;<strong>Realtime Database<\/strong>&nbsp;and then click on&nbsp;<strong>Create Database<\/strong>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"452\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/13-create-database-firebase.png?resize=750%2C452&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Project Create Database\" class=\"wp-image-169109\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/13-create-database-firebase.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/13-create-database-firebase.png?resize=300%2C181&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p><strong>2)<\/strong> Select your database location. It should be the closest to your location.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"743\" height=\"369\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/14-database-location-firebase.png?resize=743%2C369&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Set up location\" class=\"wp-image-169110\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/14-database-location-firebase.png?w=743&amp;quality=100&amp;strip=all&amp;ssl=1 743w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/14-database-location-firebase.png?resize=300%2C149&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 743px) 100vw, 743px\" \/><\/figure><\/div>\n\n\n<p><strong>3)<\/strong> Set up security rules for your database. You can select <strong>Start in test mode<\/strong>. We&#8217;ll change the database rules in just a moment.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"494\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/15-database-start-test-mode-firebase.png?resize=750%2C494&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Start in Test Mode\" class=\"wp-image-169111\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/15-database-start-test-mode-firebase.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/15-database-start-test-mode-firebase.png?resize=300%2C198&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p><strong>4)<\/strong> Your database is now created. You need to copy and save the database URL\u2014highlighted in the following image\u2014because you&#8217;ll need it later in your ESP32\/ESP8266 code.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"490\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/16-firebase-database-url.png?resize=750%2C490&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Real time database URL\" class=\"wp-image-169112\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/16-firebase-database-url.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/16-firebase-database-url.png?resize=300%2C196&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\" id=\"Set-up-Database-Security-Rules\">5) Set up Database Security Rules<\/h2>\n\n\n\n<p>Now, let&#8217;s set up the database rules. On the <strong>Realtime Database<\/strong> tab, select the <strong>Rules<\/strong> tab at the top. Then, click on <strong>Edit rules<\/strong>, copy the following rules and then click <strong>Publish<\/strong>.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ These rules grant access to a node matching the authenticated\n\/\/ user's ID from the Firebase auth token\n{\n  \"rules\": {\n    \"UsersData\": {\n      \"$uid\": {\n        \".read\": \"$uid === auth.uid\",\n        \".write\": \"$uid === auth.uid\"\n      }\n    }\n  }\n}<\/code><\/pre>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"782\" height=\"492\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/Firebase-Realtime-Database-Rules.jpg?resize=782%2C492&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Set Up Rules\" class=\"wp-image-169571\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/Firebase-Realtime-Database-Rules.jpg?w=782&amp;quality=100&amp;strip=all&amp;ssl=1 782w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/Firebase-Realtime-Database-Rules.jpg?resize=300%2C189&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/11\/Firebase-Realtime-Database-Rules.jpg?resize=768%2C483&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 782px) 100vw, 782px\" \/><\/figure><\/div>\n\n\n<p>These rules allow access only to the node that matches the authenticated user&#8217;s UID. This ensures that each user can access only their own data. In other words, a user can only read or write to the parts of the database located under their specific UID. Any data stored outside of their UID node will not be accessible to them.<\/p>\n\n\n\n<p>For example, imagine our user UID is <span class=\"rnthl rntliteral\">RjO3taAzMMXBB2Xmir2LQ<\/span>. With our security rules, it can read and write data to the database under the node <span class=\"rnthl rntliteral\">UsersData\/RjO3taAzMMXBB2Xmir2LQ<\/span>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"ESP-Datalogging-RTDB\">6) ESP32 Datalogging (Firebase Realtime Database)<\/h2>\n\n\n\n<p id=\"Firebase-Authentication-ESP32-ESP8266\">In this section, we&#8217;ll program the ESP32 board to do the following tasks:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Authenticate as a user with email and password (<a href=\"#Set-Authentication-Methods\">the user you set up in this section<\/a>);<\/li>\n\n\n\n<li>Get BME280 readings: temperature, humidity, and pressure;<\/li>\n\n\n\n<li>Get epoch time (timestamp) from an NTP server;<\/li>\n\n\n\n<li>Send sensor readings and timestamp to the realtime database as an authorized user.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Parts Required<\/h3>\n\n\n\n<p>For this project, you need the following parts*:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/esp32-dev-board-wi-fi-bluetooth\/\" target=\"_blank\" rel=\"noreferrer noopener\">ESP32<\/a> board (read <a href=\"https:\/\/makeradvisor.com\/esp32-development-boards-review-comparison\/\" target=\"_blank\" rel=\"noreferrer noopener\">best ESP32 development boards<\/a>);<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/bme280-sensor-module\/\" target=\"_blank\" rel=\"noreferrer noopener\">BME280<\/a> or any other sensor you&#8217;re familiar with;<\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/mb-102-solderless-breadboard-830-points\/\" target=\"_blank\" rel=\"noreferrer noopener\">Breadboard<\/a>;\u00a0 \u00a0 \u00a0 \u00a0 <a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\">\u00a0<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/makeradvisor.com\/tools\/jumper-wires-kit-120-pieces\/\" target=\"_blank\" rel=\"noreferrer noopener\">Jumper wires<\/a>.<\/li>\n<\/ul>\n\n\n\n<p>* you can also test the project with random values instead of sensor readings, or you can use <a href=\"https:\/\/randomnerdtutorials.com\/esp32-guides-sensors-modules\/\" title=\"\">any other sensor<\/a> you&#8217;re familiar with.<\/p>\n\n\n<p>You can use the preceding links or go directly to <a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\">MakerAdvisor.com\/tools<\/a> to find all the parts for your projects at the best price!<\/p><p style=\"text-align:center;\"><a href=\"https:\/\/makeradvisor.com\/tools\/?utm_source=rnt&utm_medium=post&utm_campaign=post\" target=\"_blank\"><img data-recalc-dims=\"1\" decoding=\"async\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2017\/10\/header-200.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\"><\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Schematic Diagram<\/h3>\n\n\n\n<p>In this tutorial, we&#8217;ll send BME280 sensor readings to the Firebase Realtime Database. So, you need to wire the BME280 sensor to your board.<\/p>\n\n\n\n<p>We&#8217;re going to use I2C communication with the BME280 sensor module. For that, wire the sensor to the default ESP32 SCL (<span class=\"rnthl rntclblue\">GPIO 22<\/span>) and SDA (<span class=\"rnthl rntclgreen\">GPIO 21<\/span>) pins, as shown in the following schematic diagram.<\/p>\n\n\n\n<p class=\"rntbox rntclblue\">Learn more about I2C with the ESP32: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-i2c-communication-arduino-ide\/\">ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals (Arduino IDE)<\/a>.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"675\" height=\"670\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit_f.png?resize=675%2C670&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 BME280 Sensor Temperature Humidity Pressure Wiring Diagram Circuit\" class=\"wp-image-99755\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit_f.png?w=675&amp;quality=100&amp;strip=all&amp;ssl=1 675w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit_f.png?resize=300%2C298&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/10\/ESP32-BME280-Sensor-Temperature-Humidity-Pressure-Wiring-Diagram-Circuit_f.png?resize=150%2C150&amp;quality=100&amp;strip=all&amp;ssl=1 150w\" sizes=\"(max-width: 675px) 100vw, 675px\" \/><\/figure><\/div>\n\n\n<p class=\"rntbox rntclblue\">Not familiar with the BME280 with the ESP32? Read this tutorial: <a href=\"https:\/\/randomnerdtutorials.com\/esp32-bme280-arduino-ide-pressure-temperature-humidity\/\"><strong>ESP32 with BME280 Sensor using Arduino IDE (Pressure, Temperature, Humidity)<\/strong><\/a>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing Libraries<\/h3>\n\n\n\n<p>For this project, you need to install the following libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\" title=\"\">FirebaseClient Library<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/adafruit\/Adafruit_BME280_Library\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit BME280 Library<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit Unified Sensor Library<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Installing Libraries &#8211; VS Code<\/h4>\n\n\n\n<p>Follow the next instructions if you&#8217;re using VS Code with the PlatformIO or pioarduino extension.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\"><strong>Install the FirebaseClient Library<\/strong><\/h5>\n\n\n\n<p>Click on the <strong>PIO Home<\/strong> icon and select the <strong>Libraries tab<\/strong>. Search for &#8220;<strong>FirebaseClient<\/strong>&#8220;. Select the <strong>Firebase Client Library<\/strong> by Mobitz.<\/p>\n\n\n\n<p>If you&#8217;re using VS Code with the PlatformIO extension, click on the <strong>PIO Home<\/strong> icon and then select the <strong>Libraries tab<\/strong>. Search for &#8220;<strong>FirebaseClient<\/strong>&#8220;. Select the <strong>Firebase Client Library<\/strong> by Mobitz.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"646\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Install-FirebsaeClient-Library-VS-Code.jpg?resize=750%2C646&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install FirebaseClient Library VS Code\" class=\"wp-image-169174\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Install-FirebsaeClient-Library-VS-Code.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Install-FirebsaeClient-Library-VS-Code.jpg?resize=300%2C258&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Then, click <strong>Add to Project<\/strong> and select the project you&#8217;re working on.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"341\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Add-FirebaseClient-library-to-project-VS-Code.jpg?resize=750%2C341&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Add FirebaseClient Library ro project in VS Code\" class=\"wp-image-169175\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Add-FirebaseClient-library-to-project-VS-Code.jpg?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/Add-FirebaseClient-library-to-project-VS-Code.jpg?resize=300%2C136&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Then, click <strong>Add to Project<\/strong> and select the project you&#8217;re working on.<\/p>\n\n\n\n<p><strong>Install the BME280 Library<\/strong> <\/p>\n\n\n\n<p>In the <strong>Libraries<\/strong> tab, search for <strong>BME280<\/strong>. Select the <strong>Adafruit BME280<\/strong> library.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"996\" height=\"661\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/09\/PlatformIO-VS-Code-Search-BME280-library.png?resize=996%2C661&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"PlatformIO VS Code Search for BME280 Library\" class=\"wp-image-99243\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/09\/PlatformIO-VS-Code-Search-BME280-library.png?w=996&amp;quality=100&amp;strip=all&amp;ssl=1 996w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/09\/PlatformIO-VS-Code-Search-BME280-library.png?resize=300%2C199&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/09\/PlatformIO-VS-Code-Search-BME280-library.png?resize=768%2C510&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 996px) 100vw, 996px\" \/><\/figure><\/div>\n\n\n<p> Then, click <strong>Add to Project<\/strong> and select the project you&#8217;re working on.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"745\" height=\"243\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/09\/PlatformIO-add-library-to-project.png?resize=745%2C243&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"PlatformIO with VS Code Add Library to Project\" class=\"wp-image-99245\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/09\/PlatformIO-add-library-to-project.png?w=745&amp;quality=100&amp;strip=all&amp;ssl=1 745w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2020\/09\/PlatformIO-add-library-to-project.png?resize=300%2C98&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 745px) 100vw, 745px\" \/><\/figure><\/div>\n\n\n<p>Also, change the monitor speed to 115200 by adding the following line to the <span class=\"rnthl rntliteral\">platformio.ini<\/span> file of your project:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>monitor_speed = 115200<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Installation &#8211; Arduino IDE<\/h4>\n\n\n\n<p>Follow this section if you&#8217;re using Arduino IDE. <\/p>\n\n\n\n<p>You need to install the following libraries:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\" title=\"\">FirebaseClient Library<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/adafruit\/Adafruit_BME280_Library\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit BME280 Library<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/github.com\/adafruit\/Adafruit_Sensor\" target=\"_blank\" rel=\"noreferrer noopener\">Adafruit Unified Sensor Library<\/a><\/li>\n<\/ul>\n\n\n\n<p>Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong>, search for the libraries&#8217; names, and install the libraries.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"790\" height=\"586\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?resize=790%2C586&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Install Firebase Client Library Arduino IDE\" class=\"wp-image-169118\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?w=790&amp;quality=100&amp;strip=all&amp;ssl=1 790w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?resize=300%2C223&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/install-Firebase-client-arduino-ide.png?resize=768%2C570&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 790px) 100vw, 790px\" \/><\/figure><\/div>\n\n\n<p>Now, you&#8217;re all set to start programming the ESP32 and ESP8266 boards to interact with the database.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span style=\"font-size: inherit;\">Datalogging\u2014Firebase Realtime Database<\/span> Code<\/h3>\n\n\n\n<p>Copy the following code to your Arduino IDE or to the <span class=\"rnthl rntliteral\">main.cpp<\/span> file if you&#8217;re using VS Code.<\/p>\n\n\n\n<p>You need to insert your network credentials, project API key, database URL, and the authorized user email and password.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*\r\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\r\n  Complete project details at our blog: https:\/\/RandomNerdTutorials.com\/esp32-data-logging-firebase-realtime-database\/\r\n  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.\r\n  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\r\n*\/\r\n\r\n#include &lt;Arduino.h&gt;\r\n#include &lt;WiFi.h&gt;\r\n#include &lt;WiFiClientSecure.h&gt;\r\n#include &lt;FirebaseClient.h&gt;\r\n#include &lt;Wire.h&gt;\r\n#include &lt;Adafruit_Sensor.h&gt;\r\n#include &lt;Adafruit_BME280.h&gt;\r\n#include &quot;time.h&quot;\r\n\r\n\/\/ Network and Firebase credentials\r\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\r\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\r\n\r\n#define Web_API_KEY &quot;REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY&quot;\r\n#define DATABASE_URL &quot;REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL&quot;\r\n#define USER_EMAIL &quot;REPLACE_WITH_FIREBASE_PROJECT_EMAIL_USER&quot;\r\n#define USER_PASS &quot;REPLACE_WITH_FIREBASE_PROJECT_USER_PASS&quot;\r\n\r\n\/\/ User functions\r\nvoid processData(AsyncResult &amp;aResult);\r\n\r\n\/\/ Authentication\r\nUserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);\r\n\r\n\/\/ Firebase components\r\nFirebaseApp app;\r\nWiFiClientSecure ssl_client;\r\nusing AsyncClient = AsyncClientClass;\r\nAsyncClient aClient(ssl_client);\r\nRealtimeDatabase Database;\r\n\r\n\/\/ Timer variables for sending data every 10 seconds\r\nunsigned long lastSendTime = 0;\r\nconst unsigned long sendInterval = 10000; \/\/ 10 seconds in milliseconds\r\n\r\n\/\/ Variable to save USER UID\r\nString uid;\r\n\r\n\/\/ Database main path (to be updated in setup with the user UID)\r\nString databasePath;\r\n\/\/ Database child nodes\r\nString tempPath = &quot;\/temperature&quot;;\r\nString humPath = &quot;\/humidity&quot;;\r\nString presPath = &quot;\/pressure&quot;;\r\nString timePath = &quot;\/timestamp&quot;;\r\n\r\n\/\/ Parent Node (to be updated in every loop)\r\nString parentPath;\r\n\r\nint timestamp;\r\n\r\nconst char* ntpServer = &quot;pool.ntp.org&quot;;\r\n\r\n\/\/ BME280 sensor\r\nAdafruit_BME280 bme; \/\/ I2C\r\nfloat temperature;\r\nfloat humidity;\r\nfloat pressure;\r\n\r\n\/\/ Create JSON objects for storing data\r\nobject_t jsonData, obj1, obj2, obj3, obj4;\r\nJsonWriter writer;\r\n\r\n\/\/ Initialize BME280\r\nvoid initBME(){\r\n  if (!bme.begin(0x76)) {\r\n    Serial.println(&quot;Could not find a valid BME280 sensor, check wiring!&quot;);\r\n    while (1);\r\n  }\r\n}\r\n\r\n\/\/ Initialize WiFi\r\nvoid initWiFi() {\r\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\r\n  Serial.print(&quot;Connecting to WiFi ..&quot;);\r\n  while (WiFi.status() != WL_CONNECTED) {\r\n    Serial.print('.');\r\n    delay(1000);\r\n  }\r\n}\r\n\r\n\/\/ Function that gets current epoch time\r\nunsigned long getTime() {\r\n  time_t now;\r\n  struct tm timeinfo;\r\n  if (!getLocalTime(&amp;timeinfo)) {\r\n    \/\/Serial.println(&quot;Failed to obtain time&quot;);\r\n    return(0);\r\n  }\r\n  time(&amp;now);\r\n  return now;\r\n}\r\n\r\nvoid setup(){\r\n  Serial.begin(115200);\r\n\r\n  initBME();\r\n  initWiFi();\r\n  configTime(0, 0, ntpServer);\r\n\r\n  \/\/ Configure SSL client\r\n  ssl_client.setInsecure();\r\n  ssl_client.setConnectionTimeout(1000);\r\n  ssl_client.setHandshakeTimeout(5);\r\n\r\n  \/\/ Initialize Firebase\r\n  initializeApp(aClient, app, getAuth(user_auth), processData, &quot;\ud83d\udd10 authTask&quot;);\r\n  app.getApp&lt;RealtimeDatabase&gt;(Database);\r\n  Database.url(DATABASE_URL);\r\n}\r\n\r\nvoid loop(){\r\n  \/\/ Maintain authentication and async tasks\r\n  app.loop();\r\n\r\n  \/\/ Check if authentication is ready\r\n  if (app.ready()){\r\n\r\n    \/\/ Periodic data sending every 10 seconds\r\n    unsigned long currentTime = millis();\r\n    if (currentTime - lastSendTime &gt;= sendInterval){\r\n      \/\/ Update the last send time\r\n      lastSendTime = currentTime;\r\n\r\n      uid = app.getUid().c_str();\r\n\r\n      \/\/ Update database path\r\n      databasePath = &quot;\/UsersData\/&quot; + uid + &quot;\/readings&quot;;\r\n\r\n      \/\/Get current timestamp\r\n      timestamp = getTime();\r\n      Serial.print (&quot;time: &quot;);\r\n      Serial.println (timestamp);\r\n\r\n      parentPath= databasePath + &quot;\/&quot; + String(timestamp);\r\n\r\n      \/\/ Get sensor readings\r\n      temperature = bme.readTemperature();\r\n      humidity = bme.readHumidity();\r\n      pressure = bme.readPressure()\/100.0;\r\n\r\n      \/\/ Create a JSON object with the data\r\n      writer.create(obj1, tempPath, temperature);\r\n      writer.create(obj2, humPath, humidity);\r\n      writer.create(obj3, presPath, pressure);\r\n      writer.create(obj4, timePath, timestamp);\r\n      writer.join(jsonData, 4, obj1, obj2, obj3, obj4);\r\n\r\n      Database.set&lt;object_t&gt;(aClient, parentPath, jsonData, processData, &quot;RTDB_Send_Data&quot;);\r\n    }\r\n  }\r\n}\r\n\r\nvoid processData(AsyncResult &amp;aResult){\r\n  if (!aResult.isResult())\r\n    return;\r\n\r\n  if (aResult.isEvent())\r\n    Firebase.printf(&quot;Event task: %s, msg: %s, code: %d\\n&quot;, aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());\r\n\r\n  if (aResult.isDebug())\r\n    Firebase.printf(&quot;Debug task: %s, msg: %s\\n&quot;, aResult.uid().c_str(), aResult.debug().c_str());\r\n\r\n  if (aResult.isError())\r\n    Firebase.printf(&quot;Error task: %s, msg: %s, code: %d\\n&quot;, aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());\r\n\r\n  if (aResult.available())\r\n    Firebase.printf(&quot;task: %s, payload: %s\\n&quot;, aResult.uid().c_str(), aResult.c_str());\r\n}\r\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Firebase-ESP\/raw\/main\/ESP32-Firebase-Datalogging\/ESP32-Firebase-Datalogging.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>Continue reading to learn how the code works, or skip to the <a href=\"#demonstration\">demonstration section<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Include Libraries<\/h4>\n\n\n\n<p>First, include the required libraries.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Arduino.h>\n#if defined(ESP32)\n    #include &lt;WiFi.h>\n#elif defined(ESP8266)\n    #include &lt;ESP8266WiFi.h>\n#endif\n#include &lt;WiFiClientSecure.h>\n#include &lt;FirebaseClient.h>\n#include &lt;Wire.h>\n#include &lt;Adafruit_Sensor.h>\n#include &lt;Adafruit_BME280.h>\n#include \"time.h\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Network Credentials<\/h4>\n\n\n\n<p>Include your network credentials in the following lines so that your boards can connect to the internet using your local network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Insert your network credentials\n#define WIFI_SSID \"REPLACE_WITH_YOUR_SSID\"\n#define WIFI_PASSWORD \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Firebase Project API Key, Firebase User, and Database URL<\/h4>\n\n\n\n<p>Insert your <a href=\"#firebase-project-api-key\">Firebase project API key<\/a>\u2014the one you&#8217;ve gotten <a href=\"#firebase-project-api-key\">in this section<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define Web_API_KEY \"REPLACE_WITH_YOUR_PROJECT_API_KEY\"<\/code><\/pre>\n\n\n\n<p>Insert the <a href=\"#Set-Authentication-Methods\">authorized email and the corresponding password<\/a>\u2014these are the details of the user you&#8217;ve added <a href=\"#Set-Authentication-Methods\">in this section<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Insert Authorized Email and Corresponding Password\n#define USER_EMAIL \"REPLACE_WITH_THE_USER_EMAIL\"\n#define USER_PASSWORD \"REPLACE_WITH_THE_USER_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert your <a href=\"#Set-up-Realtime-Database\">database URL<\/a> in the following line:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Insert RTDB URLefine the RTDB URL\n#define DATABASE_URL \"REPLACE_WITH_YOUR_DATABASE_URL\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Declaring Firebase Authentication and Components<\/h4>\n\n\n\n<p>The following line creates an authentication object using the project API key, the project user email, and password.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>UserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);<\/code><\/pre>\n\n\n\n<p>This creates a <span class=\"rnthl rntliteral\">FirebaseApp<\/span> instance called <span class=\"rnthl rntliteral\">app<\/span> that refers to the Firebase application.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>FirebaseApp app;<\/code><\/pre>\n\n\n\n<p>The following lines set up the asynchronous communication framework for interacting with Firebase&#8217;s Realtime Database. Basically, you create an SSL client using the <span class=\"rnthl rntliteral\">WiFiClientSecure<\/span> library. Then, you instantiate an Asynchronous client called <span class=\"rnthl rntliteral\">aClient<\/span> that enables secure HTTPS. This will allow you to handle network operations asynchronously.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>WiFiClientSecure ssl_client;\nusing AsyncClient = AsyncClientClass;\nAsyncClient aClient(ssl_client);<\/code><\/pre>\n\n\n\n<p>The following line creates a <span class=\"rnthl rntliteral\">RealtimeDatabase<\/span> object called <span class=\"rnthl rntliteral\">Database<\/span>, which represents the Firebase Realtime Database.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>RealtimeDatabase Database;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Timer and Data Variables<\/h4>\n\n\n\n<p>Then create variables to track the time. We&#8217;ll send sensor readings to the database every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Timer variables for sending data every 10 seconds\nunsigned long lastSendTime = 0;\nconst unsigned long sendInterval = 10000;<\/code><\/pre>\n\n\n\n<p>Create a variable to save the user UID. The <span class=\"rnthl rntliteral\">uid<\/span> variable will be used to save the user&#8217;s UID. We can get the user&#8217;s UID after the authentication.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Variable to save USER UID\nString uid;<\/code><\/pre>\n\n\n\n<p>Create variables to save the database path and specific database nodes. We&#8217;ll update these variables later in the code when we get the user UID.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Database main path (to be updated in setup with the user UID)\nString databasePath;\n\/\/ Database child nodes\nString tempPath = \"\/temperature\";\nString humPath = \"\/humidity\";\nString presPath = \"\/pressure\";\nString timePath = \"\/timestamp\";\n\n\/\/ Parent Node (to be updated in every loop)\nString parentPath;<\/code><\/pre>\n\n\n\n<p>We create a variable to save the timestamp and another to save the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int timestamp;\n\nconst char* ntpServer = \"pool.ntp.org\";<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">BME280 Variables<\/h4>\n\n\n\n<p>Then, create an <span class=\"rnthl rntliteral\">Adafruit_BME280<\/span> object called <span class=\"rnthl rntliteral\">bme<\/span>. This automatically creates a sensor object on the ESP32 or ESP8266 default I2C pins.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Adafruit_BME280 bme; \/\/ I2C<\/code><\/pre>\n\n\n\n<p>The following variables will hold the temperature, humidity, and pressure readings from the sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>float temperature;\nfloat humidity;\nfloat pressure;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">JSON Variables<\/h4>\n\n\n\n<p>We&#8217;ll send all data to the database in a JSON object. The FirebaseClient library has its own JSON methods. You should use them instead of other JSON libraries. To do that, we need to create four <span class=\"rnthl rntliteral\">jsonData<\/span> objects called <span class=\"rnthl rntliteral\">obj1<\/span>, <span class=\"rnthl rntliteral\">obj2<\/span>, <span class=\"rnthl rntliteral\">obj3<\/span>, and <span class=\"rnthl rntliteral\">obj4<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>object_t jsonData, obj1, obj2, obj3, obj4;<\/code><\/pre>\n\n\n\n<p>Then, we need to create an instance of the <span class=\"rnthl rntliteral\">JsonWriter<\/span> class, in this case called <span class=\"rnthl rntliteral\">writer<\/span>, that provides methods to create and combine JSON objects for Firebase.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>JsonWriter writer;<\/code><\/pre>\n\n\n\n<p>To learn more, we recommend<a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\/blob\/main\/resources\/docs\/json_writer.md\" target=\"_blank\" rel=\"noopener\" title=\"\"> reading this section of the documentation about the JsonWriter object<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">initBME()<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">initBME()<\/span> function will initialize the BME280 sensor. We&#8217;ll call it later in the <span class=\"rnthl rntliteral\">setup()<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Initialize BME280\nvoid initBME(){\n  if (!bme.begin(0x76)) {\n    Serial.println(\"Could not find a valid BME280 sensor, check wiring!\");\n    while (1);\n  }\n  Serial.print(\"BME280 Initialized with success\");\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">initWiFi()<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">initWiFi()<\/span> function will connect the ESP32 to your local network using the SSID and password defined earlier.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Initialize WiFi\nvoid initWiFi() {\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n  Serial.print(\"Connecting to WiFi ..\");\n  while (WiFi.status() != WL_CONNECTED) {\n    Serial.print('.');\n    delay(1000);\n  }\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">getTime()<\/h4>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">getTime()<\/span> function returns the current epoch time.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Function that gets current epoch time\nunsigned long getTime() {\n  time_t now;\n  struct tm timeinfo;\n  if (!getLocalTime(&amp;timeinfo)) {\n    \/\/Serial.println(\"Failed to obtain time\");\n    return(0);\n  }\n  time(&amp;now);\n  return now;\n}<\/code><\/pre>\n\n\n\n<p>To learn more about getting epoch time with the ESP32 board, you can check the following tutorial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/epoch-unix-time-esp32-arduino\/\">Get Epoch\/Unix Time with the ESP32 (Arduino IDE)<\/a><\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">setup()<\/h4>\n\n\n\n<p>In the <span class=\"rnthl rntliteral\">setup()<\/span>, initialize the Serial Monitor, the BME280 sensor, WiFi, and configure the NTP server.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup(){\n    Serial.begin(115200);\n\n    initBME();\n    initWiFi();\n    configTime(0, 0, ntpServer);<\/code><\/pre>\n\n\n\n<p>Configure the SSL Client.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>ssl_client.setInsecure();\n#if defined(ESP32)\n  ssl_client.setConnectionTimeout(1000);\n  ssl_client.setHandshakeTimeout(5);\n#elif defined(ESP8266)\n  ssl_client.setTimeout(1000); \/\/ Set connection timeout\n  ssl_client.setBufferSizes(4096, 1024); \/\/ Set buffer sizes\n#endif<\/code><\/pre>\n\n\n\n<p>The following line initializes the Firebase app with authentication and sets the <span class=\"rnthl rntliteral\">processData()<\/span> as the callback function for async results (this means that any results from the <span class=\"rnthl rntliteral\">initializeApp()<\/span> function will be handled on the <span class=\"rnthl rntliteral\">processData()<\/span> callback function).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>initializeApp(aClient, app, getAuth(user_auth), processData, \"\ud83d\udd10 authTask\");<\/code><\/pre>\n\n\n\n<p>Then, tell that you want to set the <span class=\"rnthl rntliteral\">Database<\/span> object defined earlier as a database for our Firebase <span class=\"rnthl rntliteral\">app<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>app.getApp&lt;RealtimeDatabase&gt;(Database);<\/code><\/pre>\n\n\n\n<p>Finally, set the database URL.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Database.url(DATABASE_URL);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">loop()<\/h4>\n\n\n\n<p>The Firebase library we&#8217;re using works asynchronously and with callback functions. This means that when an event happens, the corresponding assigned callback functions will run. To keep the Firebase app running, handling authentication and asynchronous tasks, we need to add <span class=\"rnthl rntliteral\">app.loop()<\/span> at the start of our <span class=\"rnthl rntliteral\">loop()<\/span> function.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void loop(){\n  app.loop();<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">app.ready()<\/span> command checks if Firebase authentication is complete and ready, so that we can proceed with other Firebase operations (like writing to the database).<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (app.ready()){<\/code><\/pre>\n\n\n\n<p>The following lines check if 10 seconds (<span class=\"rnthl rntliteral\">sendInterval<\/span>) have passed. We&#8217;ll use this to send data periodically every 10 seconds.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Periodic data sending every 10 seconds\nunsigned long currentTime = millis();\nif (currentTime - lastSendTime &gt;= sendInterval){\n   \/\/ Update the last send time\n  lastSendTime = currentTime;<\/code><\/pre>\n\n\n\n<p>After a successful authentication, we&#8217;ll get the user UID and save it in the <span class=\"rnthl rntliteral\">uid<\/span> variable. Then, we&#8217;ll update the database path to <span class=\"rnthl rntliteral\">UsersData\/&lt;USER_UID&gt;<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>uid = app.getUid().c_str();<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">databasePath<\/span> variable saves the database main path, which can now be updated with the user UID.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Update database path\ndatabasePath = \"\/UsersData\/\" + uid;<\/code><\/pre>\n\n\n\n<p>After this, we can get the current timestamp and update the path where the readings will be saved.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/Get current timestamp\ntimestamp = getTime();\nSerial.print (\"time: \");\nSerial.println (timestamp);\n\nparentPath= databasePath + \"\/\" + String(timestamp);<\/code><\/pre>\n\n\n\n<p>To better understand how we&#8217;ll organize our data, here&#8217;s a diagram.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"750\" height=\"463\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/12\/Firebase-BME280-datalogging-database.png?resize=750%2C463&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase BME280 datalogging database\" class=\"wp-image-108647\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/12\/Firebase-BME280-datalogging-database.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/12\/Firebase-BME280-datalogging-database.png?resize=300%2C185&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>It might seem redundant to save the timestamp twice (in the parent node and the child node), however, having all the data at the same level of the hierarchy will make things simpler in the future, if we want to build a web app to display the data.<\/p>\n\n\n\n<p>After getting the timestamp and updating all database nodes with the user UID and corresponding timestamp, we can get readings from the BME280 sensor.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ Get sensor readings\ntemperature = bme.readTemperature();\nhumidity = bme.readHumidity();\npressure = bme.readPressure()\/100.0;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Creating the JSON Objects<\/h4>\n\n\n\n<p>We&#8217;ll send all the readings and corresponding timestamps to the realtime database at the same time by creating a JSON object that contains the values of those variables. The <span class=\"rnthl rntliteral\">FirebaseClient<\/span> library has its own JSON methods. We&#8217;ll use them to send data in JSON format to the database. <\/p>\n\n\n\n<p>We use the <span class=\"rnthl rntliteral\">create()<\/span> method on the <span class=\"rnthl rntliteral\">writer<\/span> object to create a JSON object with a key (node) and corresponding value (sensor readings).<\/p>\n\n\n\n<p>For example, the following line creates a JSON object with <span class=\"rnthl rntliteral\">tempPath<\/span> as the key and <span class=\"rnthl rntliteral\">temperature<\/span> as the value. It stores the result in <span class=\"rnthl rntliteral\">obj1<\/span>. <\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>writer.create(obj1, tempPath, temperature);<\/code><\/pre>\n\n\n\n<p>An example of output would be in this case:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>obj1 = {\"UserData\/&lt;user_udi>\/timestamp\/temperature\": 23.5}<\/code><\/pre>\n\n\n\n<p>We proceed similarly for the other readings and timestamp.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>writer.create(obj2, humPath, humidity);\nwriter.create(obj3, presPath, pressure);\nwriter.create(obj4, timePath, timestamp);<\/code><\/pre>\n\n\n\n<p>Then, we use the <span class=\"rnthl rntliteral\">join()<\/span> method on the <span class=\"rnthl rntliteral\">writer<\/span> object to merge the four JSON objects (<span class=\"rnthl rntliteral\">obj1<\/span>, <span class=\"rnthl rntliteral\">obj2<\/span>, <span class=\"rnthl rntliteral\">obj3<\/span>, <span class=\"rnthl rntliteral\">obj4<\/span>) into a single JSON structure, stored in <span class=\"rnthl rntliteral\">jsonData<\/span>. The <span class=\"rnthl rntliteral\">4<\/span> indicates the number of objects to combine.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>writer.join(jsonData, 4, obj1, obj2, obj3, obj4);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Sending the JSON Object<\/h4>\n\n\n\n<p>Finally, we can send the JSON object to the database using the <span class=\"rnthl rntliteral\">set()<\/span> method on the database object. We already explained how this works <a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\" title=\"\">in this previous tutorial<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Database.set&lt;object_t>(aClient, parentPath, jsonData, processData, \"RTDB_Send_Data\");<\/code><\/pre>\n\n\n\n<p>This uses the SSL client <span class=\"rnthl rntliteral\">aClient<\/span> and sends the <span class=\"rnthl rntliteral\">jsonData<\/span> variable to the <span class=\"rnthl rntliteral\">parentPath<\/span> path in the database. The results of this operation will be handled on the <span class=\"rnthl rntliteral\">processData<\/span> function. The last parameter <span class=\"rnthl rntliteral\">RTDB_Send_Data<\/span> identifies this task.<\/p>\n\n\n\n<p>Finally, the&nbsp;<span class=\"rnthl rntliteral\">processData()<\/span>&nbsp;function logs the results of the asynchronous Firebase operations.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void processData(AsyncResult &amp;aResult){\n    if (!aResult.isResult())\n        return;\n\n    if (aResult.isEvent())\n        Firebase.printf(\"Event task: %s, msg: %s, code: %d\\n\", aResult.uid().c_str(), aResult.eventLog().message().c_str(), aResult.eventLog().code());\n\n    if (aResult.isDebug())\n        Firebase.printf(\"Debug task: %s, msg: %s\\n\", aResult.uid().c_str(), aResult.debug().c_str());\n\n    if (aResult.isError())\n        Firebase.printf(\"Error task: %s, msg: %s, code: %d\\n\", aResult.uid().c_str(), aResult.error().message().c_str(), aResult.error().code());\n\n    if (aResult.available())\n        Firebase.printf(\"task: %s, payload: %s\\n\", aResult.uid().c_str(), aResult.c_str());\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"demonstration\">Demonstration<\/h2>\n\n\n\n<p>Upload the previous code to your ESP32 board. Don&#8217;t forget to insert your network credentials, project API key, database URL, user email, and the corresponding password.<\/p>\n\n\n\n<p>After uploading the code, press the board RST button so that it starts running the code. It should authenticate to Firebase, get the user UID, and immediately send new readings to the database.<\/p>\n\n\n\n<p>Open the Serial Monitor at a baud rate of 115200 and check that everything is working as expected.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"933\" height=\"518\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-BME280-Firebase-Data-Logging-Serial-Monitor.jpg?resize=933%2C518&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Send JSON Firebase Serial Monitor\" class=\"wp-image-170053\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-BME280-Firebase-Data-Logging-Serial-Monitor.jpg?w=933&amp;quality=100&amp;strip=all&amp;ssl=1 933w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-BME280-Firebase-Data-Logging-Serial-Monitor.jpg?resize=300%2C167&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-BME280-Firebase-Data-Logging-Serial-Monitor.jpg?resize=768%2C426&amp;quality=100&amp;strip=all&amp;ssl=1 768w\" sizes=\"(max-width: 933px) 100vw, 933px\" \/><\/figure><\/div>\n\n\n<p>Aditionally, go to the Realtime Database on your Firebase project interface and check that new readings are saved. Notice that it saves the data under a node with the own user UID\u2014this is a way to restrict access to the database.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"702\" height=\"607\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-Datalogging-Timestamp.png?resize=702%2C607&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Datalogging\" class=\"wp-image-170055\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-Datalogging-Timestamp.png?w=702&amp;quality=100&amp;strip=all&amp;ssl=1 702w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-Datalogging-Timestamp.png?resize=300%2C259&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 702px) 100vw, 702px\" \/><\/figure><\/div>\n\n\n<p>Wait some time until you get some readings on the database. Expand the nodes to check the data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"wrapping-up\">Wrapping Up<\/h2>\n\n\n\n<p>In this tutorial, you learned how to log your sensor readings with timestamps to the Firebase Realtime Database using the ESP32. This was just a simple example for you to understand how it works.<\/p>\n\n\n\n<p>You can use other methods provided by the <span class=\"rnthl rntliteral\">FirebaseClient<\/span> library to log your data, and you can organize your database in different ways. We organized the database in a way that is convenient for another project that we&#8217;ll publish soon.<\/p>\n\n\n\n<p>In PART 2, we&#8217;ll create a Firebase Web App to display all saved data in a table and the latest readings on charts.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a style=\"font-size: inherit;\" href=\"https:\/\/randomnerdtutorials.com\/esp32-esp8266-firebase-gauges-charts\/\">ESP32\/ESP8266: Firebase Data Logging Web App (Gauges, Charts, and Table)<\/a><span style=\"font-size: inherit;\"> <\/span><\/li>\n<\/ul>\n\n\n\n<p>We hope you&#8217;ve found this tutorial useful.<\/p>\n\n\n\n<p>If you like Firebase projects, please take a look at our new eBook. We&#8217;re sure you&#8217;ll like it:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\">Firebase Web App with ESP32 and ESP8266<\/a><\/strong><\/li>\n<\/ul>\n\n\n\n<p>Learn more about the ESP32 with our resources:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\" title=\"https:\/\/randomnerdtutorials.com\/projects-esp32\/\">Free ESP32 Projects and Tutorials<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/learn-esp32-with-arduino-ide\/\">Learn ESP32 with Arduino IDE<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, you&#8217;ll learn how to log data with the ESP32 to the Firebase Realtime Database with timestamps (data logging) so that you have a record of your data &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP32 Data Logging to Firebase Realtime Database\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp32-data-logging-firebase-realtime-database\/#more-108623\" aria-label=\"Read more about ESP32 Data Logging to Firebase Realtime Database\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":170057,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[281,276,277,299,264],"tags":[],"class_list":["post-108623","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp32-project","category-esp32","category-esp32-arduino-ide","category-0-esp32","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2022\/01\/ESP32-ESP8266-BME280-Datalogging-Firebase.jpg?fit=1920%2C1080&quality=100&strip=all&ssl=1","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/108623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/comments?post=108623"}],"version-history":[{"count":23,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/108623\/revisions"}],"predecessor-version":[{"id":170059,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/108623\/revisions\/170059"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/170057"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=108623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=108623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=108623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}