Real-time Data Storage with Firebase and Node-RED

In this tutorial, we will show you how to use Firebase with Node-RED to store data in the realtime database (RTDB). We’ll show you examples to SET, PUSH, UPDATE, and DELETE data.

Real-time Data Storage with Firebase and Node-RED

Firebase is a popular backend service that provides a Realtime Database (RTDB) to easily store and retrieve data in real-time. Node-RED is a visual programming tool that allows you to connect different web services and devices together and is widely used for IoT and Home Automation projects.

Want to learn how to use Node-RED to build a Home Automation System and connect IoT devices? Check our Smart Home eBook.

Table of Contents

  1. Installing Node-RED
  2. Setting Up the Firebase Project
  3. Installing Firebase Nodes
  4. Creating Node-RED Flows—Save Data in the Firebase Realtime Database

1) Installing Node-RED

Before proceeding you need to have Node-RED installed. You can install Node-RED locally on your computer or on a Raspberry Pi, or on a cloud server. Check one of the following tutorials to install Node-RED:

For a quick Node-RED getting started guide, take a look at the following tutorials:

2) Setting Up the Firebase Project

Firebase logo

Firebase is Google’s mobile application development platform with a set of tools to build, improve and grow your app. We’ll use the following Firebase products in this project:

  • Authentication: login/logout and user identity. This is needed so that we can access the Firebase project using Node-RED.
  • Realtime database: real-time, cloud-hosted, NoSQL database; data is stored in a JSON structure. This is the database where we’ll store the data.

How does the Realtime Database Work?

All the data stored in the Firebase Realtime Database is stored as JSON objects. So, you can think of the database as a cloud-based JSON tree. When you add data to the JSON tree, it becomes a node with an associated key in the existing JSON structure.The following picture shows an example of the RTDB JSON content of a project we’ve built some time ago.

Firebase Realtime Database is stored as JSON objects

Follow the next steps to set up your Firebase project, including the authentication, and realtime database.

Want to create your own web app using Firebase and the ESP32 and ESP8266? Check our eBook: Firebase Web App with the ESP32 and ESP8266.

Create Firebase Project

1) Go to Firebase and sign in using a Google Account.

2) Click Get Started and then Add project to create a new project.

3) Give a name to your project. For example, Node-RED Firebase.

Create Firebase Project Node-RED

4) Disable the option Enable Google Analytics for this project as it is not needed and click Create project.

Set Up Firebase Project for ESP32 and ESP8266 Step 2

5) It will take a few seconds to set up your project. Then, click Continue when it’s ready.

6) You’ll be redirected to your Project console page.

Set Authentication Methods

To interact with your Firebase project, you need to set an authentication method first. There are several different authentication methods. We’ll use email and password.

1) On the left sidebar, click on Build > Authentication and then on Get started.

Firebase Project Authentication

2) Select the Option Email/Password.

Selecting Firebase Authentication with Email/Password

3) Enable that authentication method and click Save.

Enable Email/password authentication Firebase

4) Authentication with email and password should now be enabled.

Firebase Authentication with Email/Password enabled

5) Now, you need to add a user. On the Authentication tab, select the Users tab at the top. Then, click on Add User.

Create Firebase Project Authentication add user

6) Add an email address for the authorized user. It can be your google account email or any other email. You can also create an email for this specific project. Add a password that will allow you to sign in to your app and access the database. Don’t forget to save the password in a safe place because you’ll need it later. When you’re done, click Add user.

Create Firebase Project authentication email and password

7) A new user was successfully created and added to the Users table.

Firebase Users Table

Notice that Firebase creates a unique UID for each registered user. The user UID allows us to identify the user and keep track of the user to provide or deny access to the project or the database. There’s also a column that registers the date of the last sign-in. At the moment, it is empty because we haven’t signed in with that user yet.

3) Get the Project API Key

To interface with your Firebase project using Node-RED, you need to get your project API key. Follow the next steps to get your project API key.

1) On the left sidebar, click on Project Settings.

Get the Project API Key

2) Copy the Web API Key to a safe place because you’ll need it later.

Firebase Project API key

4) Set up the Realtime Database

Now, let’s create a realtime database and set up database rules for our project.

1) On the left sidebar, click on BuildRealtime Database and then click on Create Database.

Create Real Time database firebase

2) Select your database location. It should be the closest to your location.

Set up realtime database firebase ESP32 ESP8266 Select Location

3)Now, we need to set up database security rules. The rules defined who can interact with the database and how (read and/or write). Start in test mode. We’ll change the database rules in just a moment.

Set up realtime database firebase ESP32 ESP8266 Set Security Rules

4) Your database is now created. You need to copy and save the database URL—highlighted in the following image—because you’ll need it later in your Node-RED flow.

Firebase Realtime Database URL

5) Set up Database Security Rules

Now, let’s set up the database rules. On the Realtime Database tab, select the Rules tab at the top. Then, click on Edit rules, and add the following rules.

{
  "rules": {
    ".read": "auth != null",
     ".write": "auth != null"
   }
}

These database rules determine that:

  • Only authenticated users can read and write to the database.

3) Installing Firebase Nodes

To interact with Firebase using Node-RED we’ll use these Firebase nodes. These nodes don’t come installed by default, so we’ll need to install them ourselves.

In Node-RED, go to Menu > Manage Palette.

Manage Palette Node-RED

Select the Install tab and search for @gogovega/node-red-contrib-firebase-realtime-database, then click install and finally Close.

Install Firebase Nodes Node-RED

The Firebase nodes will show up on the Palette at the left sidebar.

Firebase Nodes Node-RED Palette

There are three different nodes:

  • Firebase in: listens to database changes in a specified path;
  • Firebase get: fetches data from a specified path; you can use queries to filter data;
  • Firebase out: to set, push, update or remove data at a specified path in the database—this is the node that we’ll be using in this tutorial. This is the node you should use if you want to save data in the database.

4) Creating Node-RED Flows—Save Data in the Firebase Realtime Database

There are different ways in which you can write to the Firebase Realtime Database. You can:

  • set new data on a certain path—this will overwrite existing data on that path;
  • push new data—this will append data as a new child with a unique key to a certain path without overwriting previous data;
  • update data—this will update the values of a specific key;

We’ll cover these three different scenarios and we’ll also cover how to delete data using the Firebase out node.

Set New Data (Overwrite)

Add an Inject node and a Firebase out node to the flow and wire them together.

Node-RED Flow Set New Data Firebase

Double-click on the Firebase out node. Click on the pencil icon to add your Project and database configuration details.

Node-RED add new database config

Give a name to your Database in Node-RED, it can be whatever you want. Then, for the authentication method select Email. Finally, insert your project API key, your database URL, the user you created previously, and the corresponding password. When you’re done, click Update.

node-red add api key url email password Firebase

Then, you can set the database path where you want to save your data. The path where you’ll save your data will depend on your project requirements. In this simple example, we’ll just send the current timestamp set in the inject node. We’ll save it to the following path RDdata/timestamp.

Edit Firebase out node path setting

When you’re finished, click Done. Then, deploy your project.

Node-RED deploy button

If you’ve set up your Firebase project details correctly, a green sign saying connected will show up under the Firebase node.

Node-RED firebase demonstration example

Testing the Flow

Now, you can test the flow. Open your Firebase Realtime Database on your Firebase project (it is under Build > Realtime Database).

On your Node-RED flow, click on the red square next to the inject node. This will send a message payload (msg.payload) to the next node containing the current timestamp (in unix format).

Testing Node-RED firebase demonstration example

At the same time, you should see new data showing up on your realtime database.

Node-RED firebase demonstration example output node debugging

As you can see, it saved the timestamp under the path you defined previously. timestamp is the key and the actual timestamp is the key value. timestamp is a child of NRData.

If you click on the timestamp node again, the previous timestamp value will be overwritten with the new timestamp.

Node-RED firebase demonstration example output node debugging timestamp

This feature can be useful in scenarios that you need to save configuration settings, the current state of an output, threshold values, and more. If you want, for example, to save the last sensor readings, you should use push instead of set—see the next example.


Push New Data (Append)

In this section, we’ll see out to push new data to the database. The Node-RED Firebase out node can append data as a new child with a unique key to a certain path without overwriting previous data. The best way to understand how it works is to actually test it.

Create a flow similar to the previous one with an inject node and a Firebase out node.

Node-RED firebase demonstration example output node push new data

Double-click on the Firebase node. For the Database field, select the Database node settings you created for the previous node. On the Method select PUSH, and for the Path, use the same as the previous example: NRData/timestamp. Make sure you select string on that little grey rectangle next to the input field.

Node-RED firebase demonstration example output node edit settings

When you’re finished, click Done.

Next, deploy your application.

Node-RED deploy button

Testing the Flow

Click on the inject node several times to test the flow.

Node-RED firebase demonstration example output node testing the flow

Go to your realtime database to see what happens.

Here’s how your database will look like:

Node-RED Firebase timestamp demonstration

As you can see, every time you click on the inject node, it will push new data to the NRData/timestamp path. It will save all timestamps under that path with a unique key generated by Firebase.


Update Data

You also have the option to update the value of a specific key. To exemplify this, choose one of the keys of the previous example. I’ll choose the following -NLlW-dGssCjJEcMEIUB.

We’ll update the value of that specific key with a different value.

As with previous examples, add an inject node and a Firebase out node to the flow.

Update Data Node-RED Firebase

Double-click on the Firebase node. For the Database field, select the Database node settings you created for the previous examples. On the Method select UPDATE, and for the Path, use the same of previous examples NRData/timestamp.

Edit Firebase out node Node-RED timestamp

Then, double-click on the inject node to edit its properties. You need to send a JSON object with the key:value pair you want to replace. In my case, the key is -NLlW-dGssCjJEcMEIUB and the new value will be “Hello” (instead of a timestamp so that it is more noticeable where the change occurred in the database).

Select JSON on the msg.payload field and insert the following but replace it with your key value:

{"-NLlW-dGssCjJEcMEIUB": "Hello"}
Edit Firebase out node Node-RED timestamp JSON key value

After these changes, your flow will look like this.

Edit Firebase out node Node-RED timestamp inject demonstration

Deploy your flow.

Node-RED deploy button

Testing the Flow

Click on the inject node to trigger the flow.

Go back to your database. Check that it has inserted a new key:value (“Hello”) pair under the path you specified.

Edit Firebase out node Node-RED timestamp inject demonstration testing the flow

Remove Data

You can also delete data from the database using the Firebase out node. You just need to specify which data path you want to delete.

As with previous examples, add an inject node and a Firebase out node to the flow. The inject node is just needed to trigger the Firebase node. We won’t actually pass any data on that node.

Remove Data Node-RED Firebase flow

Double-click on the Firebase out node. For the Database field, select the Database node settings you created for the previous examples. On the Method select REMOVE, and for the Path, use NRData/timestamp to remove all data from the database.

Remove Data Node-RED Firebase flow edit out node

Your flow will look as follows.

Remove Data Node-RED Firebase flow testing demonstration

Finally, deploy your flow.

Node-RED deploy button

Testing the Flow

After deploying the flow, click on the inject node to trigger the Firebase out node.

Check your Realtime Database. It will be empty.

Remove Data Node-RED Firebase flow testing timestamp

Wrapping Up

In this tutorial, you learned how to save data in the Firebase Realtime Database (RTDB) using Node-RED. We provided you simple examples that you can modify to use in your own projects. This can be useful to save configuration settings, threshold values, output values, sensor readings and more.

You can add ESP32, ESP8266 boards or other IoT devices to this setup. The ESP boards can send data to Node-RED via MQTT or HTTP requests. Then, Node-RED can process that data and send it to Firebase and display it in different formats.

The ESP32/ESP8266 boards can interact with Firebase directly, but using Node-RED in-between can be advantageous if you want to build complex conditions based on data. This way all the logic would be done on Node-RED and the ESP boards would be clients that connect to Node-RED. This is a good choice if you already have your boards set up as MQTT clients and you don’t want to change their code.

If you want to learn more about Firebase with the ESP32/ESP8266 boards and how to build your own web application, check our eBook:

To learn more about using Node-RED to build an IoT Home Automation System with the Raspberry Pi, ESP32, and ESP8266, you’ll certainly like our eBook:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

14 thoughts on “Real-time Data Storage with Firebase and Node-RED”

  1. Hola buen día, escribo en español ya que ingles se poco y nada, pero quiero hacer una pregunta que siempre me genero ruido y es como subir un proyecto de node-red (como un dashboard) a un servidor como puede ser netlify, para tener un servidor en la nube y no tenerlo en forma local, no se si me explico, eso se puede hacer o no? ya que se me han ocurrido varias ideas para hacer con node-red pero siempre en forma local tuve que hacerlo correr y no sobre un servidor. Se podrá hacer esto o estoy errado.

    Reply
  2. Hi.
    Regarding stepper motor control, can it be controlled from anywhere like the ESP32 and ESP8266 GPIO Control from Anywhere in the World that the admin has made

    Reply
  3. Hi,

    I have Node-Red running.(for a couple of years) and about 20 tabs with flows (average 1/3 of the available space used).
    Node-Red runs on a Rpi3b. A different Rpi3b runs Home-Assistant and Mosquitto. Both Rpi altered to run on SSD. I have dozens and dozens of sensors and smart stuff. Most Tasmota, but also Ewelink,Tuya, Zigbee, RF and a few other systems.
    I am not so good with yaml, so almost all automations are handled by Node-Red. However, HA has much better and easier data handling, like temperature history per sensor.
    I would like some of that from my RF equipment.
    I tried installing the Gogovega module but it won’t install. I had v12 Nodejs. then I tried v18; nope, and now v16. Do not tell me it only runs on v14…
    I tend to avoid add-ons that are so much depending on installed version. It might work at the moment, but do one update, and everything fails.

    I wonder how well a light would work if the Pir RF signal should be found in a Firebase array somewhere.
    Any suggestions that might help me?

    Reply
      • Hi.
        What do you mean by “Pir RF signal should be found in a Firebase array somewhere”. Can you reformulate your question?
        You can send and retrieve data to Firebase using those Node-RED nodes. Then, you can handle the retrieved data in the way that’s most convenient for your application.
        Regards,
        Sara

        Reply
  4. https://drive.google.com/file/d/1a5-peYjDGO4OjcesZRa1J5qeindcc78P/view?usp=drivesdk
    I have this couple of choice functions that contain a lot(but not all) RF codes. Checking the codes takes more time than you think, then some other routines (and HAss) make the light turn on or off, depending on amount of natural light in the room and time of day. (global variables) All that has to happen before I bump into some furniture because the light did not go on in time.

    Reply
  5. The question here is then : what do you think is the fastest and also low cpu way to compare incoming RF codes (8 chars) to several lists of known codes.

    Reply
  6. Hi there,
    Seems to be great article. Thanks for that.
    Unfortunately, When I configure the Firebase RT database in Node-Red it doesn’t accept my API key. I’m 100% sure its the right key and using the right database URL.

    Are there more people have the same problem. Error is: input box is red lined and hovering gives: Invalid inpuit pattern.

    My API key is:
    AIzaSyDvbSD_A-XXXXXXXXXXXXXXXX__3XE
    (I removed some chars on X)

    Any ideas how to solve this?

    Secondly, I have a Node-Red running on virtual machine in Oracle cloud.
    Its free to use, passwd protected.
    Regards,

    Reply
    • Hi.
      I’m not sure.
      double-check that you’re using the right URL and API key and that you’re pasting them on the right fields.
      Regards,
      Sara

      Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.