从 NodeMCU 发送到 Firebase 时如何构建我的传感器数据?
How to structure my sensor data when sending to Firebase from NodeMCU?
我已使用以下代码将我的 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 函数中,您需要在使用如下代码写入 t、h 或 hic 浮点值之前获得对 ID2 的引用(这假设 Field-Root 显示在您的数据库结构是你的 Firebase 数据库的根)
function setFloat(fieldName, fieldValue) {
//.....
firebase.database().ref( ID1 + '/' + ID2 + '/' + fieldName).set(fieldValue);
//.....
}
这将为您提供以下结构
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");
然后在loop
函数中使用:
Firebase.setFloat(temperaturePath, 1.1);
Firebase.setFloat(humidityPath, 2.2);
Firebase.setFloat(heatIndexPath, 3.3);
我已使用以下代码将我的 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 函数中,您需要在使用如下代码写入 t、h 或 hic 浮点值之前获得对 ID2 的引用(这假设 Field-Root 显示在您的数据库结构是你的 Firebase 数据库的根)
function setFloat(fieldName, fieldValue) {
//.....
firebase.database().ref( ID1 + '/' + ID2 + '/' + fieldName).set(fieldValue);
//.....
}
这将为您提供以下结构
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");
然后在loop
函数中使用:
Firebase.setFloat(temperaturePath, 1.1);
Firebase.setFloat(humidityPath, 2.2);
Firebase.setFloat(heatIndexPath, 3.3);