3

I have sent my DHT sensor data from my NodeMCU to Firebase with the following code.

void loop() {
  if(timeSinceLastRead > 2000) {
    float h = dht.readHumidity();
    float t = dht.readTemperature();
    float f = dht.readTemperature(true);
    if (isnan(h) || isnan(t) || isnan(f)) {
      Serial.println("Failed to read from DHT sensor!");
      timeSinceLastRead = 0;
      return;
    }

    float hif = dht.computeHeatIndex(f, h);
    float hic = dht.computeHeatIndex(t, h, false);

    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(t);
    Serial.print(" *C ");
    Serial.print(f);
    Serial.print(" *F\t");
    Serial.print("Heat index: ");
    Serial.print(hic);
    Serial.print(" *C ");
    Serial.print(hif);
    Serial.println(" *F");
    Firebase.setFloat("Temp",t);
    Firebase.setFloat("Humidity",h);
    Firebase.setFloat("HeatIndex",hic);

    timeSinceLastRead = 0;
  }
  delay(100);
  timeSinceLastRead += 100;
 }

It has successfully sent the data to Firebase but in the following structure.

Field-Root
|_ HeatIndex: <value>
|_ Humidity : <value>
|_ Temp     : <value>

But, I have two more user-defined ID parameters which I want to sent to Firebase and I need the following structure.

Field-Root
|_ ID1
   |_ ID2
      |_ HeatIndex: <value>
      |_ Humidity : <value>
      |_ Temp     : <value>

But, I am not getting the hierarchical structure which I need where instead I am getting the old structure. How do I get that?

Maria Irudaya Regilan J
  • 1,426
  • 1
  • 11
  • 22

2 Answers2

3

The first parameter of the setFloat method allows you to specify the path of the data.

From https://firebase-arduino.readthedocs.io/en/latest/#_CPPv2N15FirebaseArduino8setFloatERK6Stringf

void setFloat(const String &path, float value)

  Writes the float value to the node located at path equivalent to 
  the REST API’s PUT.

parameters
  path: The path inside of your db to the node you wish to update.
  value: Float value that you wish to write. 

So by you can use a path like:

Firebase.setFloat("ID-Floor1/ID-Bathroom/Temp", 1.1);
Firebase.setFloat("ID-Floor1/ID-Bathroom/Humidity", 2.2);
Firebase.setFloat("ID-Floor1/ID-Bathroom/HeatIndex", 3.3);

Which will appear in Firebase like:

Firebase screenshot

You can also minimize the string manipulation depending on when ID1 and ID2 are available.

If they are known in your setup then you could hardcode the paths as in the example above.

Otherwise you can form the path (preferable once) using:

String path = Id1;
path.concat("/");
path.concat(Id2);
path.concat("/");

String temperaturePath = path;
temperaturePath.concat("Temp");
String humidityPath = path;
humidityPath.concat("Temp");
String heatIndexPath = path;
heatIndexPath.concat("Temp");

Then in the loop function use:

Firebase.setFloat(temperaturePath, 1.1);
Firebase.setFloat(humidityPath, 2.2);
Firebase.setFloat(heatIndexPath, 3.3);
Ben T
  • 4,656
  • 3
  • 22
  • 22
1

To get the database structure you want, inside your setFloat function you will need to get a reference to ID2 before writing the t,h, or hic float values using code like below (this assumes Field-Root shown in your DB structure is the root of your Firebase database)

function setFloat(fieldName, fieldValue) {
    //.....
    firebase.database().ref( ID1 + '/' + ID2 + '/' + fieldName).set(fieldValue);
    //.....
}

This will give you the structure below

Field-Root
|_ ID1
   |_ ID2
      |_ HeatIndex: <value>
      |_ Humidity : <value>
      |_ Temp     : <value>