Ridmi Natasha
5 min readNov 8, 2023

Send Data to Firebase Realtime Database using ESP8266

In this tutorial, you’ll learn how to send data to the Firebase Realtime Database using ESP8266.

What will you learn?

  1. How to install ESP8266 into Arduino IDE?
  2. How to create a new database in Firebase?
  3. How to send data to Firebase Real-time database?

Things you need

  1. ESP8266
  2. Arduino IDE

Installing ESP8266 into Arduino IDE

  • In your Arduino IDE, go to Tools Board Board Manager, and the following Boards Manager window will open.
Boards Manager Window
  • In the search bar, search for ESP8266 and click install. This will install the latest ESP8266 version.

Once installed successfully, the ESP8266 board will be shown in the Boards Manager.

  • Next, go to Tools Board Board Manager and select ESP8266 BoardsNodeMCU 1.0 (ESP-12E Module).
  • Finally, go to File Preferences, and on the Additional Boards Manager URLs field paste the following link:

http://arduino.esp8266.com/stable/package_esp8266com_index.json

Now, every time you work with ESP8266, make sure that the proper board type and port are selected from the Boards Manager and Port.

Creating a Firebase Project

  • Log on to Google Firebase using your email.

Firebase | Google’s Mobile and Web App Development Platform

  • Click on Get started or Go to console.
  • Next, click on Add Project and give a name for your project.
  • Give a unique name to your project, or you can give a name suggested by Firebase.
  • Click on Continue
  • Here, if you need to use Google Analytics in your project, you can enable the given option or Continue without it.
  • Next, select the option Default Account for Firebase; this will create a Firebase project in your default Google account.
  • Then click Create Project. After a few seconds, it will create the project. Select Continue.

Now, you’ll land on your project’s overview page. This page shows all the functions that Google Firebase offers. Firebase can be used for mobile and web app development.

  • From the left menu, select Build Authentication → Get started
  • Select Sign-in methodAnonymous → Enable → Save
  • Next, from the left menu, select Realtime DatabaseCreate Database
  • Select the location of the database from the drop-down menu.
  • Select test mode and Enable.
  • Once the database is created, go to Rules, edit the code as above, and click Publish.
  • Now to get the Firebase Host URL and Firebase Auth Key,
  • Go to Data and copy your URL as above.
  • Next, go to Project OverviewProject SettingsGeneral.
  • Copy the Web API key.

Arduino Program

  • In your Arduino IDE, open a new sketch.

To work with ESP8266 and Firebase, we need to install the Firebase Realtime Database Library for ESP8266.

  • Download the Firebase-ESP8266-master file from the below link.
  • https://drive.google.com/file/d/1NGZnYZ5v09taplu5moB9nPsnehgEoqt0/view?usp=sharing
  • To insert the zip library file into Arduino, go to Sketch Include LibraryAdd.zip Library and select the downloaded zip file.
  • Next, let’s write a simple program to send the data to the Firebase Realtime database.
  • The below code will blink the Built-in LED of the ESP8266 board every 5 seconds, and at the same time, it will display a message on the serial monitor and send data to the real-time database.

#include<Wire.h>
#include "FirebaseESP8266.h"
#include<ESP8266WiFi.h>

const int ledPin = 13;
char FIREBASE_AUTH [] = " "; // Your Firebase Web API Key
char FIREBASE_HOST [] = " "; // Your Firebase Host URL
char WIFI_SSID [] = " "; // Your WIFI SSID
char WIFI_PASSWORD [] = " "; // Your WIFI Password

FirebaseData firebaseData;

void setup(){
Serial.begin(115200);
pinMode(ledPin,OUTPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
}

void loop(){
digitalWrite(ledPin, HIGH);
Serial.println("LED is ON");
Firebase.setString(firebaseData, "/data", "ON"); //This will create a path as 'data' and save the value on Firebase
delay(5000);

digitalWrite(ledPin, LOW);
Serial.println("LED is OFF");
Firebase.setString(firebaseData, "/data", "OFF");
delay(5000);
}
  • Make sure to update your code with the following: Firebase Auth Key, Firebase Host URL, Your WIFI SSID, Your WIFI Password
  • Use the previously copied Web API key and Firebase URL as the FIREBASE_AUTH and FIREBASE_HOST in your code.
  • Next Select the proper board and port and upload your code into ESP8266.

Now, once you open your serial monitor, it will display as ‘LED is ON’ when the Built-in LED is ON and as ‘LED is OFF’ when the Built-in LED is OFF.

And in your Realtime database, it will display as ‘ON’ and ‘OFF.

In the next blog, I’ll show how to receive data from the Firebase real-time database using ESP8266.