我已使用以下代码将我的 DHT 传感器数据从我的 NodeMCU 发送到 Firebase。
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;
}
它已成功将数据发送到 Firebase,但采用以下结构。
Field-Root
|_ HeatIndex: <value>
|_ Humidity : <value>
|_ Temp : <value>
但是,我还有两个用户定义的 ID 参数要发送到 Firebase,我需要以下结构。
Field-Root
|_ ID1
|_ ID2
|_ HeatIndex: <value>
|_ Humidity : <value>
|_ Temp : <value>
但是,我没有得到我需要的层次结构,而是得到了旧结构。我如何获得它?
最佳答案
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 中会像这样出现:
您还可以根据 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");
然后在循环
函数中使用:
Firebase.setFloat(temperaturePath, 1.1);
Firebase.setFloat(humidityPath, 2.2);
Firebase.setFloat(heatIndexPath, 3.3);
关于firebase - 从 NodeMCU 发送到 Firebase 时如何构建我的传感器数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54491119/