DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Everything You Need to Know About Building MVP With React and Firebase
  • 5 Most Preferred React Native Databases
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines

Trending

  • Building a Zero-Cost Approval Workflow With AWS Lambda Durable Functions
  • Introduction to Tactical DDD With Java: Steps to Build Semantic Code
  • How to Write for DZone Publications: Trend Reports and Refcards
  • Compliance Automated Standard Solution (COMPASS), Part 11: Compliance as Code, the OSCAL MCP Server Way
  1. DZone
  2. Data Engineering
  3. Databases
  4. ESP8266 (ESP32) With Firebase Real-Time Database: IoT-Controlled RGB LEDs

ESP8266 (ESP32) With Firebase Real-Time Database: IoT-Controlled RGB LEDs

Learn more about building an IoT-controlled RGB LED project.

By 
Francesco Azzola user avatar
Francesco Azzola
·
Jul. 18, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
41.8K Views

Join the DZone community and get the full member experience.

Join For Free

This tutorial covers how to build an IoT-controlled RGB LEDs (such a LEDs strip) using ESP32 or ESP8266 and Firebase. This is an interesting project because through it, it is possible to explore how to use IoT. Some years ago, it was almost impossible to build IoT-controlled RGB LEDs, now, as you will see, it is very easy. In this IoT project, we will use the power of Google Firebase to control RGB LEDs using ESP32 or ESP8266. 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. The result is shown in the video below:

To control LEDs using Firebase real-time database, we have to follow these two steps:

  • Configure the ESP32/ESP8266 to connect to Google Firebase real-time database
  • Configure the Firebase real-time database

Configuring ESP32 (or ESP8266) to Connect to Google Firebase Real-Time Database

In this first step, it is necessary to connect ESP32 to Firebase database so that this device receives the data from the database as soon as we modify them. To achieve it, first we have to connect the ESP8266 to the Wi-Fi:

#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());
}


This happens where connectWifi() is called in the setup() as we will see later, and the ssid and password are the Wi-Fi ssid and the related password.

Circuit Diagram

The circuit diagram to connect RGB LEDs to ESP8266 (or ESP32) is shown below:

This IoT-controlled LED project uses Neopixels LEDs, but we can use other kinds of LEDs. The same steps can be applied even if you change the LEDs you use.

Programming the ESP8266 to Connect to Firebase

This is the core of the project. To connect the ESP32 to Google Firebase real-time database, we will use a Firebase library that simplifies the project. Go to Sketch->Include Library->Manage Libraries and look for Firebase library:

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

#include "FirebaseESP8266.h"

FirebaseData firebaseData;

void setup() {
  Serial.begin(9600);
  connectWifi();
  Firebase.begin("firebase_url", "your_firebase_API_key");
}


At line 8, the code sets up the connection between ESP8266 and Firebase. Two parameters are necessary: the firebase_url and the firebase_API_key. You will see later how to get it from Firebase console after we have configured the Firebase project. Finally, it is necessary to receive data from Firebase real-time 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();
      }
    }
  }
}


A few things to note. First, to control an IoT RGB LEDs, it is necessary to use three components (red, green, and blue). The second thing to notice: the code above gets the reference to the data stored in Firebase real-time database using:

Firebase.getInt(firebaseData, "/red")


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

if  (firebaseData.dataType() == "int") {
  ....
}


And finally, the code retrieves the value:

int val = firebaseData.intData();


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();
}


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 steps in building an IoT controlled RGB LEDs, we will configure the Firebase real-time database. If you don't have an account, before starting, it is necessary to create one for free. Then, go to the Firebase console and start adding a new project:

And then add a new project, as shown below:

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

Then, it is time to create the Firebase database:

It is important you set the database in test mode:

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

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.

That's all. You can run and test the project.

This tutorial has shown how to build an IoT-controlled RGB LEDs using Firebase real-time database and ESP32 or ESP8266. As you have seen, it is very easy, and with a few lines of code, you can control remotely RGB LEDs.

Firebase Database LEd

Published at DZone with permission of Francesco Azzola. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Everything You Need to Know About Building MVP With React and Firebase
  • 5 Most Preferred React Native Databases
  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • When Snowflake Lies to You: Understanding False Failures in dbt Pipelines

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook