Introduction: How to Fetch Data From a Firebase Database on NodeMCU
For this instructable, we'll be fetching data from a database in Google Firebase and fetch it using a NodeMCU for further parsing.
PROJECT REQUIREMENTS:
1) NodeMCU or ESP8266 Controller
2) G-Mail account for creating a Firebase database.
3) Download the Firebase Arduino IDE Library and install it on the Arduino IDE.
Step 1:
Step 2: Create a New Database on Firebase
Simply head over to the Firebase console and click on Add Project.
Once that is done, head over to the Database tab and add a Realtime Database.
Step 3: Add Host Name/Database Secret Key to Arduino Sketch
Copy the Host Name from the top of the database and Database Secret Key from Setting>Project Setting>SERVICE ACCOUNTS>DATABASE Secrets.
Use these details while initialising the Firebase in Setup code. For example:
Firebase.begin("doit-data.firebaseio.com", "lGkRasLexBtaXu9FjKwLdhWhSFjLK7JSxJWhkdJo");
Step 4: Connect Your NodeMCU to a WiFi
Add the following line to your Arduino Sketch to connect your NodeMCU to a router:
WiFi.begin("SSID", "p@ssword");
Replace SSID with the SSID of your router and p@ssword with the router password.
Step 5: Complete the Arduino Sketch.
The Firebase/Arduino library provides a variety of functions to simplify access to the Firebase Database:
> FirebaseObject object = Firebase.get("/");
After connecting to the Firebase using the begin command, the above command helps you to retrieve the entire database, which can then further be parsed using additional Firebase Objects.
> classFirebaseObject
Represents value stored in firebase, may be a singular value (leaf node) or a tree structure.
> int getInt(const String &path)
This function can help you get an integer value stored at the path mentioned.
> String getString(const String &path)
getString gets the string stored under a given key (mentioned in the path).
Step 6: Upload the Arduino Sketch to the NodeMCU
Make sure the Board has been selected properly and the correct port is being used.
Refer to the example sketch for further implementation details.
Step 7: Create a Progressive Web App for Further Control
To extend functionality in the IoT realm, you can create a Progressive Web App as well which can extend functionality to Android/iOS smartphones. Surprisingly, making a PWA requires minimal knowledge of Android Development and is entirely web-based. Thus, we can manipulate databases using a NodeMCU as well as the PWA.
9 Comments
4 years ago
I come from Vietnam, I have a question.
If I want to check if there is any data from arduino that exists on firebase, is there any syntax to help it?
Specifically, if I want to check if A1565D7 data exists, what should I do?
Please help me
Reply 3 years ago
Have u found solution?
Reply 2 years ago
I have the same question, has any of you guys solved it yet?
3 years ago
Basically, if i want to receive the data of that main node for example appliance/relay so is it possible to do so? or i have to specify the child as well?
4 years ago
Sorry my English, I'm from Argentina and I speak Spanish. I need to solve this problem with esp8266 and Firebase
path
-LVV1zLX0dCrKAtr3FcK
125815:
"from = 5060141652 & to = 125815 & data = action1"
-LVV20vEwTq3EC7nyLKX
125815:
"from = 5060141652 & to = 125815 & data = action2"
-LVV2G5958tyYWU2z7e59
13b01a:
"from = 4030188961 & to = 13b01a & data = action1"
-LVV2GKtyYWUZHz7ekTD
125815:
"from = 5060141652 & to = 125815 & data = action3"
The program must select one of these nodes (example -LVV20vEwTq3EC7nyLKX, locating the text 125815 and retrieve the data "action2".
Once read, I must delete (remove) only that node.
As there are 3 nodes with the same source data (125815), I must perform three readings of the database to process and remove each node in particular.
I can not understand the concept of firebase object, how to read it, process it and remove it from the database.
Please I need help. Thank you
Reply 4 years ago
Did you find the code for the above problem if you do then please share
4 years ago
Can anyone tell me that how to access the value of child nodes(eg. Appliance/Relay1/Pin) .
please share the syntax for this.
thank you
Reply 4 years ago
Firebase.getInt("Appliance/Relay1/Ack");
Question 5 years ago on Step 6
can you please provide the code?