如何从NodeMCU发送到火力地堡时,组织我的传感器数据?

问题描述 投票:2回答:2

我已将我的DHT传感器数据从我NodeMCU用下面的代码,以火力地堡。

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;
 }

它成功地将数据发送到火力地堡,但在以下结构。

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

但是,我有我想要发送到火力地堡两个用户自定义的ID参数,我需要以下结构。

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

但是,我没有得到的分层结构,我需要在那里,而不是我越来越老的结构。我如何获得的?

firebase firebase-realtime-database iot sensor nodemcu
2个回答
2
投票

setFloat方法的第一个参数允许您指定数据的路径。

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. 

所以,当你可以使用路径,如:

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);

将出现在火力地堡,如:

Firebase screenshot

您也可以根据当ID1和ID2可在减少字符串操作。

如果他们在你的设置已知的,那么你可以硬编码的路径,如上面的例子。

否则,您可以形成路径(最好一次)使用:

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");

然后在loop功能使用:

Firebase.setFloat(temperaturePath, 1.1);
Firebase.setFloat(humidityPath, 2.2);
Firebase.setFloat(heatIndexPath, 3.3);

1
投票

为了得到你想要的数据库结构,你的setFloat函数里面,你会需要编写T,H,或者HIC浮动使用代码类似下面的值之前到ID2的引用(假定现场的根在你的数据库结构所示是根你的火力地堡的数据库)

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

这会给你下面的结构

Field-Root
|_ ID1
   |_ ID2
      |_ HeatIndex: <value>
      |_ Humidity : <value>
      |_ Temp     : <value>
© www.soinside.com 2019 - 2024. All rights reserved.