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.

Share

    Recommendations

    • Epilog X Contest

      Epilog X Contest
    • Trash to Treasure

      Trash to Treasure
    • Build a Tool Contest

      Build a Tool Contest

    5 Discussions

    0
    None
    LongN62

    4 weeks 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

    Screenshot 2019-02-17 15.02.10.png
    0
    None
    KaranjeetS1

    2 months 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

    json.jpg
    1 reply
    0
    None
    tecnomicro

    2 months 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

    0
    None
    RX3Killa

    Question 10 months ago on Step 6

    can you please provide the code?