{"id":106618,"date":"2025-04-21T04:12:00","date_gmt":"2025-04-21T04:12:00","guid":{"rendered":"https:\/\/randomnerdtutorials.com\/?p=106618"},"modified":"2025-04-22T15:52:06","modified_gmt":"2025-04-22T15:52:06","slug":"esp8266-nodemcu-firebase-realtime-database","status":"publish","type":"post","link":"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-firebase-realtime-database\/","title":{"rendered":"ESP8266 NodeMCU: Getting Started with Firebase (Realtime Database)"},"content":{"rendered":"\n<p>Get started quickly with Firebase using the ESP8266 NodeMCU board. Firebase is Google\u2019s mobile application development platform that includes many services to manage data from IOS, Android, or web applications. You\u2019ll create a Firebase project with a realtime database (RTDB), and you\u2019ll learn how to store and read values from the database with your ESP8266 board.<\/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\/2021\/09\/ESP8266-Getting-Started-Firebase-Realtime-Database.jpg?resize=1200%2C675&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 NodeMCU: Getting Started with Firebase (Realtime Database)\" class=\"wp-image-169204\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-Realtime-Database.jpg?w=1920&amp;quality=100&amp;strip=all&amp;ssl=1 1920w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-Realtime-Database.jpg?resize=300%2C169&amp;quality=100&amp;strip=all&amp;ssl=1 300w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-Realtime-Database.jpg?resize=1024%2C576&amp;quality=100&amp;strip=all&amp;ssl=1 1024w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-Realtime-Database.jpg?resize=768%2C432&amp;quality=100&amp;strip=all&amp;ssl=1 768w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-Realtime-Database.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 21 April 2025<\/em><\/p>\n\n\n\n<p>In a later tutorial, you\u2019ll learn how to create a Firebase web app that you can access from anywhere to monitor and control your ESP8266 using firebase\u2019s realtime database.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-firebase-web-app\/\" title=\"\">ESP8266 NodeMCU with Firebase \u2013 Creating a Web App<\/a><\/li>\n<\/ul>\n\n\n\n<p>We have a similar tutorial for the ESP32 board:&nbsp;<a href=\"https:\/\/randomnerdtutorials.com\/esp32-firebase-realtime-database\/\">Getting Started with Firebase (Realtime Database)<\/a><\/p>\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\"><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\" 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\u2019s 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.<\/p>\n\n\n\n<p>The following paragraph clearly explains the advantages of using Firebase:<\/p>\n\n\n\n<p>&#8220;<em>Firebase is a toolset to \u201cbuild, improve, and grow your app\u201d, and the tools it gives you cover a large portion of the services that developers would normally have to build themselves<\/em> <em>but don\u2019t really want to build because they\u2019d 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<\/em>. The services are hosted in the cloud<em> and scale with little to no effort on the part of the developer<\/em>.&#8221;<\/p>\n\n\n\n<p>This paragraph was taken from <a href=\"https:\/\/medium.com\/firebase-developers\/what-is-firebase-the-complete-story-abridged-bcc730c5f2c0\" target=\"_blank\" rel=\"noreferrer noopener\">this article<\/a>, and we recommend that you read it if you want to better understand what Firebase is and what it allows you to do.<\/p>\n\n\n\n<p>You can use the ESP8266 to connect and interact with your Firebase project, and you can create applications to control the ESP8266 via Firebase from anywhere in the world.<\/p>\n\n\n\n<p>In this tutorial, we\u2019ll create a Firebase project with a Realtime Database and use the ESP8266 to store and retrieve data. Once connected to the internet, the ESP8266 can access the database from anywhere in the world.<\/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=\"329\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-f.png?resize=750%2C329&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Firebase Project Introduction realtime database\" class=\"wp-image-169206\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-f.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-f.png?resize=300%2C132&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>You can have two ESP8266 boards in different networks, with one board storing data and the other board reading the most recent data, for example.<\/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=\"247\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Firebase-Getting-Started-RTDB.png?resize=750%2C247&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Getting Started Realltime Database Firebase\" class=\"wp-image-169207\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Firebase-Getting-Started-RTDB.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Firebase-Getting-Started-RTDB.png?resize=300%2C99&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>In a later tutorial, we&#8217;ll create a web app using Firebase that will control the ESP8266 to display sensor readings or control outputs from anywhere in the world.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Project Overview<\/h2>\n\n\n\n<p>In this tutorial, you&#8217;ll learn how to create a Firebase project with a realtime database and store and read data from the database using the ESP8266.<\/p>\n\n\n\n<p>To follow this project, first, you need to set up a Firebase project and create a realtime database for that project. Then, you&#8217;ll program the ESP8266 to store and read data from the database. This tutorial is divided into three sections.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a href=\"#create-firebase-project\">Create a Firebase Project<\/a><\/li>\n\n\n\n<li><a href=\"#esp8266-store-data-firebase\" title=\"\">ESP8266: Store data to the Firebase Realtime Database<\/a><\/li>\n\n\n\n<li><a href=\"#esp8266-read-data-firebase\" title=\"\">ESP8266: Read data from the Firebase Realtime Database<\/a><\/li>\n<\/ol>\n\n\n\n<p>Let&#8217;s get started!<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create-firebase-project\">Set Up a Firebase Account and Create a New Project<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1.Create a New Project<\/h3>\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<h3 class=\"wp-block-heading\" id=\"authentication-methods\">2. Set Authentication Methods<\/h3>\n\n\n\n<p>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 ESP8266). Knowing a user&#8217;s identity allows an app to securely save user data in the cloud &#8230;&#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\/04\/12-user-created-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase User Created\"><\/figure><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">3. Creating a Realtime Database<\/h3>\n\n\n\n<p>Next, you need to create a Realtime Database for your project. Follow the next steps to create the database.<\/p>\n\n\n\n<ol id=\"database-url\" class=\"wp-block-list\">\n<li>On the left sidebar click on <strong>Build <\/strong>&gt; <strong>Realtime Database<\/strong> and then, click on <strong>Create Database<\/strong>.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105743\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/13-create-database-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Project Create Database\"><\/figure><\/li>\n\n\n\n<li>Select your database location. It should be the closest to your location.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105744\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/14-database-location-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Set up location\"><\/figure><\/li>\n\n\n\n<li>Set up security rules for your database. For testing purposes, select <strong>Start in test mode<\/strong>. In later tutorials, you&#8217;ll learn how to secure your database using database rules.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105745\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/15-database-start-test-mode-firebase.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Realtime Database Start in Test Mode\"><\/figure><\/li>\n\n\n\n<li>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 ESP8266 code.<br><figure><img data-recalc-dims=\"1\" decoding=\"async\" class=\"wp-image-105746\" style=\"width: 750px;\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2025\/04\/16-firebase-database-url.png?w=1200&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"Firebase Real time database URL\"><\/figure><\/li>\n<\/ol>\n\n\n\n<p>The Realtime Database is all set. Now, you also need to get your project API key.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"project-api-key\">4. Get Project API Key<\/h3>\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<p>Now, you have everything ready to interface the ESP8266 with the database.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Program the ESP8266 to Interface with Firebase<\/h2>\n\n\n\n<p>Now that the Firebase Realtime Database is created, you&#8217;ll learn how to interface the ESP8266 with the database.<\/p>\n\n\n\n<p>To program the ESP8266, you can use <a href=\"https:\/\/randomnerdtutorials.com\/installing-esp8266-nodemcu-arduino-ide-2-0\/\" title=\"\">Arduino IDE<\/a>, <a href=\"https:\/\/randomnerdtutorials.com\/vs-code-platformio-ide-esp32-esp8266-arduino\/\" title=\"\">VS Code with the PlatformIO extension<\/a>, or other suitable software.<\/p>\n\n\n\n<p><strong>Note: <\/strong> For Firebase projects, we recommend using VS Code with the PlatformIO or pioarduino extension. If you plan to develop a web application to connect the ESP8266 with Firebase, VS Code offers all the tools you need. However, since we won\u2019t be building a web application in this tutorial, you can simply use the Arduino IDE, if you prefer.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Installing the FirebaseClient Library<\/h3>\n\n\n\n<p>There is a library with lots of examples to use Firebase with the ESP8266: the <a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\" target=\"_blank\" rel=\"noopener\" title=\"\">FirebaseClient library<\/a>. This library is compatible with the ESP32, ESP8266, and many other boards.<\/p>\n\n\n\n<p>In this tutorial, we&#8217;ll look at simple examples to store and read data from the database. The library provides many other examples that you can check <a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/a>. It also provides detailed documentation explaining how to use the library.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Installation &#8211; VS Code + PlatformIO<\/h4>\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>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>If you&#8217;re using Arduino IDE, follow the next steps to install the library.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Go to <strong>Sketch <\/strong>&gt; <strong>Include Library<\/strong> &gt; <strong>Manage Libraries<\/strong><\/li>\n\n\n\n<li>Search for <strong>FirebaseClient<\/strong> and install the <strong>FirebaseClient<\/strong> by Mobitz. We&#8217;re using version 2.0.3.<\/li>\n<\/ol>\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 ESP8266 board to interact with the database.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp8266-store-data-firebase\">ESP8266 Store Data to Firebase Database<\/h2>\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=\"401\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Store-Data-RTDB-Firebase.png?resize=750%2C401&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Firebase store data realtime database project example\" class=\"wp-image-169208\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Store-Data-RTDB-Firebase.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Store-Data-RTDB-Firebase.png?resize=300%2C160&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>Copy the following code to your Arduino IDE. This sketch inserts an int, a float number, and a string into the database every 10 seconds. This is a simple example that shows how to connect the ESP8266 to the database to store data.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-firebase-realtime-database\/\n*********\/\n\n#include &lt;Arduino.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;WiFiClientSecure.h&gt;\n#include &lt;FirebaseClient.h&gt;\n\n\/\/ Network and Firebase credentials\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n\n#define Web_API_KEY &quot;REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY&quot;\n#define DATABASE_URL &quot;REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL&quot;\n#define USER_EMAIL &quot;REPLACE_WITH_FIREBASE_PROJECT_EMAIL_USER&quot;\n#define USER_PASS &quot;REPLACE_WITH_FIREBASE_PROJECT_USER_PASS&quot;\n\n\/\/ User function\nvoid processData(AsyncResult &amp;aResult);\n\n\/\/ Authentication\nUserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);\n\n\/\/ Firebase components\nFirebaseApp app;\nWiFiClientSecure ssl_client;\nusing AsyncClient = AsyncClientClass;\nAsyncClient aClient(ssl_client);\nRealtimeDatabase Database;\n\n\/\/ Timer variables for sending data every 10 seconds\nunsigned long lastSendTime = 0;\nconst unsigned long sendInterval = 10000; \/\/ 10 seconds in milliseconds\n\n\/\/ Variables to send to the Database\nint intValue = 0;\nfloat floatValue = 0.01;\nString stringValue = &quot;&quot;;\n\nvoid setup(){\n  Serial.begin(115200);\n\n  \/\/ Connect to Wi-Fi\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n  Serial.print(&quot;Connecting to Wi-Fi&quot;);\n  while (WiFi.status() != WL_CONNECTED)    {\n    Serial.print(&quot;.&quot;);\n    delay(300);\n  }\n  Serial.println();\n\n  \/\/ Configure SSL client\n  ssl_client.setInsecure();\n  ssl_client.setTimeout(1000); \/\/ Set connection timeout\n  ssl_client.setBufferSizes(4096, 1024); \/\/ Set buffer sizes\n\n  \/\/ Initialize Firebase\n  initializeApp(aClient, app, getAuth(user_auth), processData, &quot;\ud83d\udd10 authTask&quot;);\n  app.getApp&lt;RealtimeDatabase&gt;(Database);\n  Database.url(DATABASE_URL);\n}\n\nvoid loop(){\n  \/\/ Maintain authentication and async tasks\n  app.loop();\n\n  \/\/ Check if authentication is ready\n  if (app.ready()){\n    \/\/ Periodic data sending every 10 seconds\n    unsigned long currentTime = millis();\n    if (currentTime - lastSendTime &gt;= sendInterval){\n      \/\/ Update the last send time\n      lastSendTime = currentTime;\n\n      \/\/ send a string\n      stringValue = &quot;value_&quot; + String(currentTime);\n      Database.set&lt;String&gt;(aClient, &quot;\/test\/string&quot;, stringValue, processData, &quot;RTDB_Send_String&quot;);\n\n      \/\/ send an int\n      Database.set&lt;int&gt;(aClient, &quot;\/test\/int&quot;, intValue, processData, &quot;RTDB_Send_Int&quot;);\n      intValue++; \/\/increment intValue in every loop\n\n      \/\/ send a string\n      floatValue = 0.01 + random (0,100);\n      Database.set&lt;float&gt;(aClient, &quot;\/test\/float&quot;, floatValue, processData, &quot;RTDB_Send_Float&quot;);\n    }\n  }\n}\n\nvoid processData(AsyncResult &amp;aResult){\n  if (!aResult.isResult())\n    return;\n\n  if (aResult.isEvent())\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());\n\n  if (aResult.isDebug())\n    Firebase.printf(&quot;Debug task: %s, msg: %s\\n&quot;, aResult.uid().c_str(), aResult.debug().c_str());\n\n  if (aResult.isError())\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());\n\n  if (aResult.available())\n    Firebase.printf(&quot;task: %s, payload: %s\\n&quot;, aResult.uid().c_str(), aResult.c_str());\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Firebase-ESP\/raw\/main\/ESP8266-Firebase-Send-RTDB\/ESP8266-Firebase-Send-RTDB.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You need to insert your network credentials, URL database, project API key, firebase user email and password.<\/p>\n\n\n\n<p>This sketch was based on <a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\/blob\/main\/examples\/BareMinimum\/RealtimeDatabase\/RealtimeDatabase.ino\" target=\"_blank\" rel=\"noopener\" title=\"\">this basic example provided by the library<\/a>. You can find more examples <a href=\"https:\/\/github.com\/mobizt\/FirebaseClient\/tree\/main\/examples\" target=\"_blank\" rel=\"noopener\" title=\"\">here<\/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=\"#demo-write\">demonstration section<\/a>.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Including Libraries<\/h4>\n\n\n\n<p>First, include the required libraries. The <span class=\"rnthl rntliteral\">ESP8266WiFi.h<\/span> library to connect the ESP8266 to the internet, the <span class=\"rnthl rntliteral\">WiFiClientSecure<\/span> to create a wi-fi client, and the <span class=\"rnthl rntliteral\">FirebaseClient.h<\/span> library to interface the ESP8266 with Firebase.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#include &lt;Arduino.h>\n#include &lt;ESP8266WiFi.h>\n#include &lt;WiFiClientSecure.h>\n#include &lt;FirebaseClient.h><\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Defining Credentials<\/h4>\n\n\n\n<p>Include your network credentials in the following lines.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define WIFI_SSID \"REPLACE_WITH_YOUR_SSID\"\n#define WIFI_PASSWORD \"REPLACE_WITH_YOUR_PASSWORD\"<\/code><\/pre>\n\n\n\n<p>Insert your Firebase project API key\u2014the one you&#8217;ve gotten in <a href=\"#project-api-key\">section 4.1<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define Web_API_KEY \"REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY\"<\/code><\/pre>\n\n\n\n<p>Insert your database URL\u2014<a href=\"#database-url\">see section 3.4<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define DATABASE_URL \"REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL\"<\/code><\/pre>\n\n\n\n<p>Set the Firebase email user and corresponding password. The ones you set up in <a href=\"#authentication-methods\" title=\"\">section 2<\/a>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>#define USER_EMAIL \"REPLACE_WITH_FIREBASE_PROJECT_EMAIL_USER\"\n#define USER_PASS \"REPLACE_WITH_FIREBASE_PROJECT_USER_PASS\"<\/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>, that 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 and save the data to be sent to the database.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>unsigned long lastSendTime = 0;\nconst unsigned long sendInterval = 10000; \/\/ 10 seconds in milliseconds\nint intValue = 0;\nfloat floatValue = 0.01;\nString stringValue = \"\";<\/code><\/pre>\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 and connect the board to your Wi-Fi network.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>void setup(){\n  Serial.begin(115200);\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n  Serial.print(\"Connecting to Wi-Fi\");\n  while (WiFi.status() != WL_CONNECTED)    {\n    Serial.print(\".\");\n    delay(300);\n  }\n  Serial.println();<\/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();\nssl_client.setTimeout(1000); \/\/ Set connection timeout\nssl_client.setBufferSizes(4096, 1024); \/\/ Set buffer sizes<\/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>unsigned long currentTime = millis();\nif (currentTime - lastSendTime &gt;= sendInterval){\n  lastSendTime = currentTime;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Send Data to the Database<\/h4>\n\n\n\n<p>Next in the <span class=\"rnthl rntliteral\">loop()<\/span>, we send our data. We&#8217;re sending three different variable types. Sending other types is the same. You just need to specify the type on the <span class=\"rnthl rntliteral\">set()<\/span> function as we&#8217;ll explain.<\/p>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">FirebaseClient<\/span> library supports different methods to send data to the Real Time Database. In this example, we&#8217;re using an <strong>asynchronous approach with a callback function<\/strong>.<\/p>\n\n\n\n<p>To send data to the database we use <span class=\"rnthl rntliteral\">Database.set()<\/span>. This is templated to support different data types. Here\u2019s the general syntax and the arguments:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Database.set&lt;T&gt;(AsyncClient &amp;client, const String &amp;path, T value, AsyncResultCallback callback, const String &amp;uid);<\/code><\/pre>\n\n\n\n<p>Let&#8217;s break down how it works: <\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">&lt;T&gt;<\/span> refers to the data type. In the code, it\u2019s used as <span class=\"rnthl rntliteral\">Database.set&lt;String&gt;<\/span>, <span class=\"rnthl rntliteral\">Database.set&lt;int&gt;<\/span> and <span class=\"rnthl rntliteral\">Database.set&lt;float&gt;<\/span>. You can use other data types.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">AsyncClient &amp;client<\/span>: this is the asynchronous client object (<span class=\"rnthl rntliteral\">aClient<\/span> in the code) that manages the network connection to Firebase.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">const String &amp;path<\/span>: specifies the path in the Firebase Realtime Database where the data will be written. The path is relative to the database root (defined by <span class=\"rnthl rntliteral\">DATABASE_URL<\/span>). For example: <span class=\"rnthl rntliteral\">&#8220;test\/string&#8221;<\/span>.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">T value<\/span>: the value to be written to the specified path.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">AsyncResultCallback callback<\/span>: a function pointer to the callback that handles the result of the asynchronous operation. In the code, this is the <span class=\"rnthl rntliteral\">processData()<\/span> function. It processes the <span class=\"rnthl rntliteral\">AsyncResult<\/span> object to log events, errors, debug messages, or successful payloads. The callback is called when the Firebase server responds or if an error occurs during the request.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">const String &amp;uid<\/span>: a unique identifier for the task, used to track the specific operation in the callback. This helps differentiate between multiple asynchronous tasks in the <span class=\"rnthl rntliteral\">processData()<\/span> function.<\/li>\n<\/ul>\n\n\n\n<p>For example, the following lines send a string to the database.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>stringValue = \"value_\" + String(currentTime);\nDatabase.set&lt;String&gt;(aClient, \"\/test\/string\", stringValue, processData, \"RTDB_Send_String\");<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Client: <span class=\"rnthl rntliteral\">aClient<\/span> (manages the HTTPS connection).<\/li>\n\n\n\n<li>Path: <span class=\"rnthl rntliteral\">&#8220;\/test\/string&#8221;<\/span>.<\/li>\n\n\n\n<li>Value: <span class=\"rnthl rntliteral\">stringValue<\/span> variable that contains a String.<\/li>\n\n\n\n<li>Callback: <span class=\"rnthl rntliteral\">processData<\/span> (handles the result).<\/li>\n\n\n\n<li>UID: <span class=\"rnthl rntliteral\">&#8220;RTDB_Send_String&#8221;<\/span> (identifies this task in logs).<\/li>\n<\/ul>\n\n\n\n<p>Sending an integer and a float is similar.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/ send an int\nDatabase.set&lt;int&gt;(aClient, \"\/test\/int\", intValue, processData, \"RTDB_Send_Int\");\nintValue++; \/\/increment intValue in every loop\n\n\/\/ send a string\nfloatValue = 0.01 + random (0,100);\nDatabase.set&lt;float&gt;(aClient, \"\/test\/float\", floatValue, processData, \"RTDB_Send_Float\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Process the Async Results<\/h4>\n\n\n\n<p>Finally, the <span class=\"rnthl rntliteral\">processData()<\/span> 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<h3 class=\"wp-block-heading\" id=\"demo-write\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to your ESP8266 board. Don&#8217;t forget to insert your network credentials, database URL path, the project API key, and the Firebase project user email and password.<\/p>\n\n\n\n<p>After uploading the code, open the Serial Monitor at a baud rate of 115200 and press the ESP8266 on-board reset button so it starts running the code.<\/p>\n\n\n\n<p>If everything works as expected, the values should be stored in the database, and you should get success messages.<\/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=\"669\" height=\"374\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Send-to-Firebase-Database-Serial-Monitor.png?resize=669%2C374&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Send Data to the Realtime Database Firebase - Serial Monitor Demonstration\" class=\"wp-image-169209\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Send-to-Firebase-Database-Serial-Monitor.png?w=669&amp;quality=100&amp;strip=all&amp;ssl=1 669w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Send-to-Firebase-Database-Serial-Monitor.png?resize=300%2C168&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 669px) 100vw, 669px\" \/><\/figure><\/div>\n\n\n<p>Go to your project&#8217;s Firebase Realtime database, and you&#8217;ll see the values saved on the different node paths. Every 10 seconds, it saves a new value. The database blinks when new values are saved.<\/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=\"760\" height=\"357\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/RealTimeDatabase-Firebase-Data-Sent-From-ESP.png?resize=760%2C357&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP32 Store value firebase database Success\" class=\"wp-image-169134\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/RealTimeDatabase-Firebase-Data-Sent-From-ESP.png?w=760&amp;quality=100&amp;strip=all&amp;ssl=1 760w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/RealTimeDatabase-Firebase-Data-Sent-From-ESP.png?resize=300%2C141&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 760px) 100vw, 760px\" \/><\/figure><\/div>\n\n\n<p>Congratulations! You&#8217;ve successfully stored data in Firebase&#8217;s Realtime Database using the ESP8266. In the next section, you&#8217;ll learn to read values from the different database node paths.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-css-opacity is-style-wide\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"esp8266-read-data-firebase\">ESP8266 Read From Firebase Database<\/h2>\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=\"403\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Get-Data-Firebase-RTDB.png?resize=750%2C403&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Firebase get data realtime database project example\" class=\"wp-image-169210\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Get-Data-Firebase-RTDB.png?w=750&amp;quality=100&amp;strip=all&amp;ssl=1 750w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Get-Data-Firebase-RTDB.png?resize=300%2C161&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 750px) 100vw, 750px\" \/><\/figure><\/div>\n\n\n<p>In this section, you&#8217;ll learn how to read data from the database. We&#8217;ll read the data stored in the previous section. Remember that we saved an int value in the <span class=\"rnthl rntliteral\">test\/int<\/span> path, a float value in the <span class=\"rnthl rntliteral\">test\/float<\/span> and a String value in the <span class=\"rnthl rntliteral\">test\/string<\/span> path.<\/p>\n\n\n\n<p>There are different ways to get values from the database. We&#8217;ll show you how to do that synchronously and asynchronously.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">ESP8266 &#8211; Read From Firebase RTDB (Async Mode)<\/h3>\n\n\n\n<p>The following example reads the values stored in the database using an asynchronous method. Upload the following code to your board. You can use the same ESP8266 board or another board to get the data posted by the previous ESP8266.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-firebase-realtime-database\/\n*********\/\n\n#include &lt;Arduino.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;WiFiClientSecure.h&gt;\n#include &lt;FirebaseClient.h&gt;\n\n\/\/ Network and Firebase credentials\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n\n#define Web_API_KEY &quot;REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY&quot;\n#define DATABASE_URL &quot;REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL&quot;\n#define USER_EMAIL &quot;REPLACE_WITH_FIREBASE_PROJECT_EMAIL_USER&quot;\n#define USER_PASS &quot;REPLACE_WITH_FIREBASE_PROJECT_USER_PASS&quot;\n\n\/\/ User functions\nvoid processData(AsyncResult &amp;aResult);\n\n\/\/ Authentication\nUserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);\n\n\/\/ Firebase components\nFirebaseApp app;\nWiFiClientSecure ssl_client;\nusing AsyncClient = AsyncClientClass;\nAsyncClient aClient(ssl_client);\nRealtimeDatabase Database;\n\n\/\/ Timer variables for reading data every 10 seconds\nunsigned long lastSendTime = 0;\nconst unsigned long sendInterval = 10000; \/\/ 10 seconds in milliseconds\n\n\/\/ Variables to save values from the database\nint intValue;\nfloat floatValue;\nString stringValue;\n\nvoid setup() {\n  Serial.begin(115200);\n\n  \/\/ Connect to Wi-Fi\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n  Serial.print(&quot;Connecting to Wi-Fi&quot;);\n  while (WiFi.status() != WL_CONNECTED) {\n    Serial.print(&quot;.&quot;);\n    delay(300);\n  }\n\n  \/\/ Configure SSL client\n  ssl_client.setInsecure(); \/\/ For testing only\n  ssl_client.setTimeout(1000); \/\/ Set connection timeout\n  ssl_client.setBufferSizes(4096, 1024); \/\/ Set buffer sizes\n\n  \/\/ Initialize Firebase\n  initializeApp(aClient, app, getAuth(user_auth), processData, &quot;\ud83d\udd10 authTask&quot;);\n  app.getApp&lt;RealtimeDatabase&gt;(Database);\n  Database.url(DATABASE_URL);\n}\n\nvoid loop(){\n  \/\/ Maintain authentication and async tasks\n  app.loop();\n\n  \/\/ Check if authentication is ready\n  if (app.ready()){\n    \/\/ Periodic data sending every 10 seconds\n    unsigned long currentTime = millis();\n    if (currentTime - lastSendTime &gt;= sendInterval){\n      \/\/ Update the last send time\n      lastSendTime = currentTime;\n\n      \/\/ GET VALUES FROM DATABASE (using the callback async method method)\n      \/\/ you can then get the values on the processData function as soon as the results are available\n      Database.get(aClient, &quot;\/test\/int&quot;, processData, false, &quot;RTDB_GetInt&quot;);\n      Database.get(aClient, &quot;\/test\/float&quot;, processData, false, &quot;RTDB_GetFloat&quot;);\n      Database.get(aClient, &quot;\/test\/string&quot;, processData, false, &quot;RTDB_GetString&quot;);\n          \n      Serial.println(&quot;Requested data from \/test\/int, \/test\/float, and \/test\/string&quot;);\n    }\n  }\n}\n\nvoid processData(AsyncResult &amp;aResult){\n  if (!aResult.isResult())\n    return;\n\n  if (aResult.isEvent())\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());\n\n  if (aResult.isDebug())\n    Firebase.printf(&quot;Debug task: %s, msg: %s\\n&quot;, aResult.uid().c_str(), aResult.debug().c_str());\n\n  if (aResult.isError())\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());\n\n  \/\/ here you get the values from the database and save them in variables if you need to use them later\n  if (aResult.available()) {\n    \/\/ Log the task and payload\n    Firebase.printf(&quot;task: %s, payload: %s\\n&quot;, aResult.uid().c_str(), aResult.c_str());\n\n    \/\/ Extract the payload as a String\n    String payload = aResult.c_str();\n\n    \/\/\/ Handle int from \/test\/int\n    if (aResult.uid() == &quot;RTDB_GetInt&quot;){\n      \/\/ Extract the value as an int\n      intValue = payload.toInt();\n      Firebase.printf(&quot;Stored intValue: %d\\n&quot;, intValue);\n    }\n    \/\/ Handle float from \/test\/float\n    else if (aResult.uid() == &quot;RTDB_GetFloat&quot;){\n      \/\/ Extract the value as a float\n      floatValue = payload.toFloat();\n      Firebase.printf(&quot;Stored floatValue: %.2f\\n&quot;, floatValue);\n    }\n    \n    \/\/ Handle String from \/test\/string\n    else if (aResult.uid() == &quot;RTDB_GetString&quot;){\n      \/\/ Extract the value as a String\n      stringValue = payload;\n      Firebase.printf(&quot;Stored stringValue: %s\\n&quot;, stringValue.c_str());\n    }\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Firebase-ESP\/raw\/main\/ESP8266-Firebase-Read-RTDB-Async\/ESP8266-Firebase-Read-RTDB-Async.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<p>You need to insert your network credentials, URL database, project API key, firebase user email and password.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the Code Works<\/h3>\n\n\n\n<p>The first sections of the code are quite similar to the previous example. It connects and authenticates to Firebase and sets up the database connection. Then, it gets the data from the database in async mode. The results of the operation (the values of the variables) are then handled in the callback function.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Getting Data from the Database<\/h4>\n\n\n\n<p>To get data, we need to know the exact path where it is located and its type (for later processing). We can get it using the <span class=\"rnthl rntliteral\">get()<\/span> method on the <span class=\"rnthl rntliteral\">Database<\/span> object as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Database.get(AsyncClient &amp;client, const String &amp;path, AsyncResultCallback callback, bool queue, const String &amp;uid);<\/code><\/pre>\n\n\n\n<p>Here&#8217;s how it works:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">AsyncClient &amp;client<\/span>: this is the asynchronous client object (<span class=\"rnthl rntliteral\">aClient<\/span> in the code) that manages the network connection to Firebase.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">const String &amp;path<\/span>: specifies the path in the Firebase Realtime Database where we&#8217;ll get the data. The path is relative to the database root (defined by <span class=\"rnthl rntliteral\">DATABASE_URL<\/span>). For example: <span class=\"rnthl rntliteral\">&#8220;test\/string&#8221;<\/span>.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">AsyncResultCallback callback<\/span>: a function pointer to the callback that handles the result of the asynchronous operation. In the code, this is the <span class=\"rnthl rntliteral\">processData()<\/span> function. In this example, it is where we&#8217;ll get the actual data from the database and save it in variables.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">bool queue<\/span>: a boolean flag indicating whether the operation should be queued if the client is busy with another task. Set <span class=\"rnthl rntliteral\">false<\/span> for immediate execution, or <span class=\"rnthl rntliteral\">true<\/span> to queue the request. In more complex examples, it is a good idea to set it to <span class=\"rnthl rntliteral\">true<\/span>.<\/li>\n<\/ul>\n\n\n\n<ul class=\"wp-block-list\">\n<li><span class=\"rnthl rntliteral\">const String &amp;uid<\/span>: a unique identifier for the task, used to track the specific operation in the callback. This helps differentiate between multiple asynchronous tasks in the <span class=\"rnthl rntliteral\">processData()<\/span> function.<\/li>\n<\/ul>\n\n\n\n<p>As you can see, the <span class=\"rnthl rntliteral\">get()<\/span> function is quite similar to the <span class=\"rnthl rntliteral\">set()<\/span> function we&#8217;ve seen previously.<\/p>\n\n\n\n<p>For example, in the following line, we set the operation to read from the database node <span class=\"rnthl rntliteral\">\/test\/int<\/span>. This operation will be handled on the task with the identifier <span class=\"rnthl rntliteral\">RTDB_GetInt<\/span>.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Database.get(aClient, \"\/test\/int\", processData, false, \"RTDB_GetInt\");<\/code><\/pre>\n\n\n\n<p>We proceed similarly to get values from the other node paths.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>Database.get(aClient, \"\/test\/float\", processData, false, \"RTDB_GetFloat\");\nDatabase.get(aClient, \"\/test\/string\", processData, false, \"RTDB_GetString\");<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Saving the Data Into Variables<\/h4>\n\n\n\n<p>The result of the <span class=\"rnthl rntliteral\">get()<\/span> operation is then handled in the <span class=\"rnthl rntliteral\">processData()<\/span> function.<\/p>\n\n\n\n<p>The following statement <span class=\"rnthl rntliteral\">if (aResult.available())<\/span> verifies if the <span class=\"rnthl rntliteral\">AsyncResult<\/span> contains a successful result with data available for processing. If <span class=\"rnthl rntliteral\">available()<\/span> returns <span class=\"rnthl rntliteral\">true<\/span>, it means we have data to process\u2014we successfully got data from the database.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>if (aResult.available())  {\n  \/\/ Log the task and payload\n  Firebase.printf(\"task: %s, payload: %s\\n\", aResult.uid().c_str(), aResult.c_str());<\/code><\/pre>\n\n\n\n<p>We can get the data as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>String payload = aResult.c_str();<\/code><\/pre>\n\n\n\n<p>Then, with <span class=\"rnthl rntliteral\">aResult.uid()<\/span> we can check which operation occurred based on the task name and then save the data in the appropriate variable type.<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>\/\/\/ Handle int from \/test\/int\nif (aResult.uid() == \"RTDB_GetInt\"){\n  \/\/ Extract the value as an int\n  intValue = payload.toInt();\n  Firebase.printf(\"Stored intValue: %d\\n\", intValue);\n}\n\/\/ Handle float from \/test\/float\nelse if (aResult.uid() == \"RTDB_GetFloat\"){\n  \/\/ Extract the value as a float\n  floatValue = payload.toFloat();\n  Firebase.printf(\"Stored floatValue: %.2f\\n\", floatValue);\n}\n        \n\/\/ Handle String from \/test\/string\nelse if (aResult.uid() == \"RTDB_GetString\"){\n  \/\/ Extract the value as a String\n  stringValue = payload;\n  Firebase.printf(\"Stored stringValue: %s\\n\", stringValue.c_str());\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Demonstration<\/h3>\n\n\n\n<p>Upload the code to your board. Then, open the Serial Monitor at a baud rate of 115200. After a few seconds, it will print the values saved in the database.<\/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=\"738\" height=\"375\" src=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Get-Data-from-Firebase-Realtime-Database-Serial-Monitor.png?resize=738%2C375&#038;quality=100&#038;strip=all&#038;ssl=1\" alt=\"ESP8266 Get Data from Firebase Realtime Database Serial Monitor Result\" class=\"wp-image-169211\" srcset=\"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Get-Data-from-Firebase-Realtime-Database-Serial-Monitor.png?w=738&amp;quality=100&amp;strip=all&amp;ssl=1 738w, https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Get-Data-from-Firebase-Realtime-Database-Serial-Monitor.png?resize=300%2C152&amp;quality=100&amp;strip=all&amp;ssl=1 300w\" sizes=\"(max-width: 738px) 100vw, 738px\" \/><\/figure><\/div>\n\n\n<h3 class=\"wp-block-heading\">ESP8266 &#8211; Read From Firebase RTDB (No Async Mode)<\/h3>\n\n\n\n<p>For simpler applications where you don\u2019t need to queue tasks and blocking code is not critical while waiting for a database operation, you can use the <span class=\"rnthl rntliteral\">get()<\/span> function in a simplified format. This allows you to directly retrieve a value from the Firebase Realtime Database and immediately store it in a variable.<\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<pre class=\"wp-block-code language-c\"><code>int intValue =  Database.get&lt;int&gt;(aClient, \"\/test\/int\");<\/code><\/pre>\n\n\n\n<p>The <span class=\"rnthl rntliteral\">Database.get(aClient, &#8220;\/test\/int&#8221;)<\/span> synchronously fetches an integer from the Firebase RTDB path <span class=\"rnthl rntliteral\">\/test\/int<\/span> using <span class=\"rnthl rntliteral\">aClient<\/span>, assigns it to <span class=\"rnthl rntliteral\">intValue<\/span>, and blocks execution until complete. This is ideal for simple applications where you don&#8217;t need multiple tasks, but this may delay other operations.<\/p>\n\n\n\n<p>Here&#8217;s an example code using this method. It works exactly like the previous example. But for more complex applications, you should opt for the asynchronous method.<\/p>\n\n\n<pre style=\"max-height: 40em; margin-bottom: 20px;\"><code class=\"language-c\">\/*********\n  Rui Santos &amp; Sara Santos - Random Nerd Tutorials\n  Complete instructions at https:\/\/RandomNerdTutorials.com\/esp8266-nodemcu-firebase-realtime-database\/\n*********\/\n\n#include &lt;Arduino.h&gt;\n#include &lt;ESP8266WiFi.h&gt;\n#include &lt;WiFiClientSecure.h&gt;\n#include &lt;FirebaseClient.h&gt;\n\n\/\/ Network and Firebase credentials\n#define WIFI_SSID &quot;REPLACE_WITH_YOUR_SSID&quot;\n#define WIFI_PASSWORD &quot;REPLACE_WITH_YOUR_PASSWORD&quot;\n\n#define Web_API_KEY &quot;REPLACE_WITH_YOUR_FIREBASE_PROJECT_API_KEY&quot;\n#define DATABASE_URL &quot;REPLACE_WITH_YOUR_FIREBASE_DATABASE_URL&quot;\n#define USER_EMAIL &quot;REPLACE_WITH_FIREBASE_PROJECT_EMAIL_USER&quot;\n#define USER_PASS &quot;REPLACE_WITH_FIREBASE_PROJECT_USER_PASS&quot;\n\n\/\/ User function\nvoid processData(AsyncResult &amp;aResult);\n\n\/\/ Authentication\nUserAuth user_auth(Web_API_KEY, USER_EMAIL, USER_PASS);\n\n\/\/ Firebase components\nFirebaseApp app;\nWiFiClientSecure ssl_client;\nusing AsyncClient = AsyncClientClass;\nAsyncClient aClient(ssl_client);\nRealtimeDatabase Database;\n\n\/\/ Timer variables for sending data every 10 seconds\nunsigned long lastSendTime = 0;\nconst unsigned long sendInterval = 10000; \/\/ 10 seconds in milliseconds\n\n\/\/ Variables to save values from the database\nint intValue;\nfloat floatValue;\nString stringValue;\n\nvoid setup(){\n  Serial.begin(115200);\n\n  \/\/ Connect to Wi-Fi\n  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);\n  Serial.print(&quot;Connecting to Wi-Fi&quot;);\n  while (WiFi.status() != WL_CONNECTED)    {\n    Serial.print(&quot;.&quot;);\n    delay(300);\n  }\n\n  \/\/ Configure SSL client\n  ssl_client.setInsecure();\n  ssl_client.setTimeout(1000); \/\/ Set connection timeout\n  ssl_client.setBufferSizes(4096, 1024); \/\/ Set buffer sizes\n\n  \/\/ Initialize Firebase\n  initializeApp(aClient, app, getAuth(user_auth), processData, &quot;\ud83d\udd10 authTask&quot;);\n  app.getApp&lt;RealtimeDatabase&gt;(Database);\n  Database.url(DATABASE_URL);\n}\n\nvoid loop(){\n  \/\/ Maintain authentication and async tasks\n  app.loop();\n\n  \/\/ Check if authentication is ready\n  if (app.ready()){\n    \/\/ Periodic data sending every 10 seconds\n    unsigned long currentTime = millis();\n    if (currentTime - lastSendTime &gt;= sendInterval){\n      \/\/ Update the last send time\n      lastSendTime = currentTime;\n\n      \/\/ GET VALUES FROM DATABASE (and save the data in a variable)\n      int intValue =  Database.get&lt;int&gt;(aClient, &quot;\/test\/int&quot;);\n      check_and_print_value (intValue);\n\n      float floatValue = Database.get&lt;float&gt;(aClient, &quot;\/test\/float&quot;);\n      check_and_print_value(floatValue);\n\n      String stringValue = Database.get&lt;String&gt;(aClient, &quot;\/test\/string&quot;);\n      check_and_print_value(stringValue);\n\n      Serial.println(&quot;Requested data from \/test\/int, \/test\/float, and \/test\/string&quot;);\n    }\n  }\n}\n\ntemplate &lt;typename T&gt;\nvoid check_and_print_value(T value){\n  \/\/ To make sure that we actually get the result or error.\n  if (aClient.lastError().code() == 0){\n    Serial.print(&quot;Success, Value: &quot;);\n    Serial.println(value);\n  }\n  else {\n    Firebase.printf(&quot;Error, msg: %s, code: %d\\n&quot;, aClient.lastError().message().c_str(), aClient.lastError().code());\n  }\n}\n\nvoid processData(AsyncResult &amp;aResult){\n  if (!aResult.isResult())\n    return;\n\n  if (aResult.isEvent())\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());\n\n  if (aResult.isDebug())\n    Firebase.printf(&quot;Debug task: %s, msg: %s\\n&quot;, aResult.uid().c_str(), aResult.debug().c_str());\n\n  if (aResult.isError())\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());\n\n  if (aResult.available()) {\n    \/\/ Log the task and payload\n    Firebase.printf(&quot;task: %s, payload: %s\\n&quot;, aResult.uid().c_str(), aResult.c_str());\n  }\n}\n<\/code><\/pre>\n\t<p style=\"text-align:center\"><a class=\"rntwhite\" href=\"https:\/\/github.com\/RuiSantosdotme\/Firebase-ESP\/raw\/main\/ESP8266-Firebase-Read-RTDB\/ESP8266-Firebase-Read-RTDB.ino\" target=\"_blank\">View raw code<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Wrapping Up<\/h2>\n\n\n\n<p>Congratulations! In this tutorial, you\u2019ve created a Firebase project with a Realtime Database and learned how to store and read data from the database using the ESP8266 NodeMCU board.<\/p>\n\n\n\n<p>To keep things simple, we\u2019ve stored sample values in the database. The idea is to save useful data like sensor readings or GPIO states.<\/p>\n\n\n\n<p>Then, you can access the database with another ESP8266 to get the data or create a Firebase web app to use that data to display sensor readings or control the ESP8266 GPIOs from anywhere in the world. You can follow the next tutorial to learn more about creating web apps with Firebase:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-firebase-web-app\/\">ESP8266 NodeMCU with Firebase \u2013 Creating a Web App<\/a><\/li>\n<\/ul>\n\n\n\n<p>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:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/firebase-esp32-esp8266-ebook\/\"><strong>Firebase Web App with ESP32 and ESP8266<\/strong><\/a><\/li>\n<\/ul>\n\n\n\n<p>If you want to learn more about the ESP8266, check our courses:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/randomnerdtutorials.com\/home-automation-using-esp8266\/\">Home Automation Using ESP8266<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/build-web-servers-esp32-esp8266-ebook\/\">Build Web Servers with ESP32 and ESP8266 eBook (3rd Edition)<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/randomnerdtutorials.com\/projects-esp8266\/\">More ESP8266 Projects and Tutorials\u2026<\/a><\/li>\n<\/ul>\n\n\n\n<p>Thanks for reading.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Get started quickly with Firebase using the ESP8266 NodeMCU board. Firebase is Google\u2019s mobile application development platform that includes many services to manage data from IOS, Android, or web applications. &#8230; <\/p>\n<p class=\"read-more-container\"><a title=\"ESP8266 NodeMCU: Getting Started with Firebase (Realtime Database)\" class=\"read-more button\" href=\"https:\/\/randomnerdtutorials.com\/esp8266-nodemcu-firebase-realtime-database\/#more-106618\" aria-label=\"Read more about ESP8266 NodeMCU: Getting Started with Firebase (Realtime Database)\">CONTINUE READING \u00bb<\/a><\/p>\n","protected":false},"author":5,"featured_media":169204,"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":[214,265,246,240,300,264],"tags":[],"class_list":["post-106618","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-esp8266","category-esp8266-project","category-esp8266-arduino-ide","category-esp8266-projects","category-0-esp8266","category-project"],"aioseo_notices":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/randomnerdtutorials.com\/wp-content\/uploads\/2021\/09\/ESP8266-Getting-Started-Firebase-Realtime-Database.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\/106618","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=106618"}],"version-history":[{"count":10,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/106618\/revisions"}],"predecessor-version":[{"id":169213,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/posts\/106618\/revisions\/169213"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media\/169204"}],"wp:attachment":[{"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/media?parent=106618"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/categories?post=106618"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randomnerdtutorials.com\/wp-json\/wp\/v2\/tags?post=106618"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}