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.
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
- Installing Node-RED
- Setting Up the Firebase Project
- Installing Firebase Nodes
- 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:
- Install Node-RED on Raspberry Pi (works on RPI OS 64-bit)
- Access Node-RED Dashboard from Anywhere using Digital Ocean
For a quick Node-RED getting started guide, take a look at the following tutorials:
- Getting Started with Node-RED on Raspberry Pi
- Getting Started with Node-RED Dashboard on Raspberry Pi
2) Setting Up the Firebase Project
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.
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.
4) Disable the option Enable Google Analytics for this project as it is not needed and click Create project.
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.
2) Select the Option Email/Password.
3) Enable that authentication method and click Save.
4) Authentication with email and password should now be 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.
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.
7) A new user was successfully created and added to the 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.
2) Copy the Web API Key to a safe place because you’ll need it later.
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 Build > Realtime Database and then click on Create Database.
2) Select your database location. It should be the closest to your 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.
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.
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.
Select the Install tab and search for @gogovega/node-red-contrib-firebase-realtime-database, then click install and finally Close.
The Firebase nodes will show up on the Palette at the left sidebar.
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.
Double-click on the Firebase out node. Click on the pencil icon to add your Project and database configuration details.
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.
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.
When you’re finished, click Done. Then, deploy your project.
If you’ve set up your Firebase project details correctly, a green sign saying connected will show up under the Firebase node.
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).
At the same time, you should see new data showing up on your realtime database.
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.
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.
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.
When you’re finished, click Done.
Next, deploy your application.
Testing the Flow
Click on the inject node several times to test the flow.
Go to your realtime database to see what happens.
Here’s how your database will look like:
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.
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.
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"}
After these changes, your flow will look like this.
Deploy your flow.
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.
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.
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.
Your flow will look as follows.
Finally, deploy your flow.
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.
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.
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.
Hi.
Yes.
You can run node-RED on the cloud.
We have this tutorial explaining how to set it up on Digital Ocean: https://randomnerdtutorials.com/access-node-red-dashboard-anywhere-digital-ocean/
But you can use any other server.
Regards,
Sara
Gracias Sara por responder voy a buscar ese vÃdeo y encontrar una alternativa gratis, porque la economÃa de mi paÃs está complicada y cada dólar cuesta.
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
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?
Oh, And I ordered an iHost from Itead.cc 🙂
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
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.
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.
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,
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
i am suffering from this error in node red any solutions
“Component auth has not been registered yet”
At what stage of the tutorial do you get that error?
Regards,
Sara
I have the same problem. It occurs on step 4) set new data. After I clicked on “deploy”, it gave the error: “Component auth has not been registered yet”.