ESP8266 Firebase: Connect ESP8266 to Google Firebase real time database

This tutorial covers how to connect ESP8266 to Firebase. We will explore how to use Firebase real time database with ESP8266. In other words, we want to synchronize the data on the Firebase database with ESP8266 to control the device in real time. To understand better how to connect ESP8266 to Firebase and how we can use this interaction, we will build a real example: an IoT controlled RGB LEDs (such a LED strip). This is an interesting project because through it, it is possible to explore how to use Firebase in IoT.
Briefly, Google Firebase cloud is a platform that provides several services such as Authentication, real time database, and so on. To build this IoT project we will use a real time database features. The result is shown in the video below:

https://youtu.be/I5GoRxoeSUc

 To develop this Firebase IoT project, we have to follow these two steps:

  • Connecting the ESP8266 to Google Firebase real-time database
  • Configure the Firebase real-time database

Connecting ESP8266 to Firebase real-time database

In this first step, it is necessary to connect ESP8266 to Firebase database so that this device receives the updates from the database as soon as we modify the values. To achieve it, first, we have to connect the ESP8266 to the Wifi:

#include <ESP8266WiFi.h>
void connectWifi() {
  // Let us connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(".......");
  Serial.println("WiFi Connected....IP Address:");
  Serial.println(WiFi.localIP());
}Code language: PHP (php)

where connectWifi() is called in the setup() as we will see later and the ssid and password are the WiFi ssid and the WiFi password.

Importing Firebase library

To connect the ESP8266 to Google Firebase, we will use a Firebase library that simplifies the project. Go to Sketch->Include Library->Manage Libraries and look for Firebase library:

ESP8266 (or ESP32) Firebase library to connect to Google Firebase real-time library

Select the library according to the device you are using and you’re ready! The code below shows how to connect the ESP8266 to Google Firebase:

void setup() {
  Serial.begin(9600);
  connectWifi();
  ....
  
  Firebase.begin("firebase_url", "firebase_API_key");
}
Code language: JavaScript (javascript)

At line 6, the code sets up the connection between ESP8266 and Firebase. Two parameters are necessary:

  • firebase_url
  • firebase_API_Key

You will see later how to get it from Firebase console after we have configured the Firebase project.

Receiving data from Firebase database

Finally, it is necessary to receive data from the Firebase realtime database:

void loop() {
  if (Firebase.getInt(firebaseData, "/red")) {
   if  (firebaseData.dataType() == "int") {
      int val = firebaseData.intData();
      if (val != redValue) {
        redValue = val;
        setLedColor();
      }
    }
  }
  if (Firebase.getInt(firebaseData, "/green")) {
   if  (firebaseData.dataType() == "int") {
      int val = firebaseData.intData();
      if (val != greenValue) {
        greenValue = val;
        setLedColor();
      }
    }
  }
  if (Firebase.getInt(firebaseData, "/blue")) {
   if  (firebaseData.dataType() == "int") {
      int val = firebaseData.intData();
      if (val != blueValue) {
        blueValue = val;
        setLedColor();
      }
    }
  }
}Code language: JavaScript (javascript)

A few things to note. First, to control RGB LEDs it is necessary to use three components (red, green, blue). Next thing to notice: the code above gets the reference to the data stored in Firebase realtime database using:

Firebase.getInt(firebaseData, "/red")Code language: JavaScript (javascript)

then, it is necessary to verify that the value is an integer:

if  (firebaseData.dataType() == "int") {
  ....
}Code language: JavaScript (javascript)

and finally, the code retrieves the value:

int val = firebaseData.intData();

Arduino code to connect to Firebase real time database

The final code is shown below:

#include "FirebaseESP8266.h"
#include <ESP8266WiFi.h>
#include <Adafruit_NeoPixel.h>
#define PIN D1
#define NUM_LEDS 8
const char* ssid = "your_ssid";
const char* password = "your_wifi_passowrd";
FirebaseData firebaseData;
Adafruit_NeoPixel leds(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
// Current color values
int redValue = 0;
int greenValue = 0;
int blueValue = 0;
void setup() {
  Serial.begin(9600);
  connectWifi();
  leds.begin();
  
  Firebase.begin("https://xxxx.firebaseio.com/", "wDsHB30jVN554CA********");
}
void loop() {
  if (Firebase.getInt(firebaseData, "/red")) {
    if  (firebaseData.dataType() == "int") {
      int val = firebaseData.intData();
      if (val != redValue) {
        redValue = val;
         setLedColor();
      }
    }
  }
  if (Firebase.getInt(firebaseData, "/green")) {
    if  (firebaseData.dataType() == "int") {
      int val = firebaseData.intData();
      if (val != greenValue) {
        greenValue = val;
        setLedColor();
      }
    }
  }
  if (Firebase.getInt(firebaseData, "/blue")) {
    if  (firebaseData.dataType() == "int") {
      int val = firebaseData.intData();
      if (val != blueValue) {
        blueValue = val;
        setLedColor();
      }
    }
  }
}
void connectWifi() {
  // Let us connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(".......");
  Serial.println("WiFi Connected....IP Address:");
  Serial.println(WiFi.localIP());
}
void setLedColor() {
  for (int i=0; i < NUM_LEDS; i++) 
    leds.setPixelColor(i, leds.Color(redValue, greenValue, blueValue));
   leds.show();
}Code language: C++ (cpp)

If you are interested you can discover how to integrate ESP8266 and Alexa so that you can control devices using voice commands.

Configuring the Firebase real time database

In this second step in building this ESP8266 Firebase project, we will configure the Firebase realtime database. If you don’t have an account, before starting it is necessary to create one for free. Then go to the console and start adding a new project:

Firebase real-time database wih ESP8266

and then add a new project as shown below:

How to use Firebase in IoT

If everything goes well, then the new Firebase project is created:

Then it is time to create the Firebase database. This database will contain the three components of color used to control the RGB LEDs connected to the ESP8266. Once we will connect the ESP8266 to Google Firebase, every value changes in the database will be reflecting on the ESP8266 side.

Firebase real-time database

It is important you set the database in test mode:

Allowing ESP8266 to connect to Firebase

Then when all these steps are complete, select the realtime database and start adding fields as shown below:

Firebase database fields to control IoT RGB LEDs

Now the last two steps. First, in the Rules, you have to set all the values to true and then it is necessary to retrieve the values to use in the ESP8266 code shown above.

The URL is shown in the picture above https://xxxx.firebaseio.com and the API Key is in Project settings -> Services account->Database secrets.

Realtime database secrets in admin console

That’s all. You can run and test the project. The URL shown above is the URL we will use to connect the ESP8266 to Google Firebase. If you want to know more about how to build an ESP8266 Web Server instead of using Firebase, you can read my tutorial about “ESP8266 Web Server: send commands and serve HTML Page

Schematics: Connecting ESP8266 to RGB LEDs

The circuit diagram to connect RGB LEDs to ESP8266 is shown below:

ESP8266 with LEDs controlled by Firebase realtima database

In this project, we will use Neopixels LEDs, but we can use other kinds of LEDs. The same steps can be applied even if you change the LEDs you use. ESP8266 Firebase database connection to connect to Firebase database

Summary

This tutorial has shown how to connect ESP8266 to Google Firebase’s real-time database to control RGB LEDs. This project demonstrated how to use Firebase in IoT. Another interesting option to control an ESP8266 is using a Telegram Bot. As you have seen, it is very easy and with a few lines of code, you can control remotely RGB LEDs.

    1. IoT solutions in Hyderabad December 30, 2019
    2. hendy agustino s February 14, 2020
    3. Saikat Bhattacharya May 23, 2020
    4. Daniel Fernandes November 1, 2021

    Add Your Comment