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.