如何使用 Nodemcu 上传数据到 Firebase?
How to upload data to Firebase using Nodemcu?
我的传感器正在收集但未将数据推送到 Firebase。正如预期的那样 Firebase.failed returns 正确但 Firebase.error 为空。
我尝试更改 FirebaseHttpClient.h 文件中的指纹。我还尝试使用和不使用“/”更改 Firebase HOST。
#include "DHT.h"
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#define FIREBASE_HOST "your-project.firebaseio.com"
#define FIREBASE_AUTH "69DtX********************"
#define WIFI_SSID "LAPTOP" // Change the name of your WIFI
#define WIFI_PASSWORD "********" // Change the password of your WIFI
#define DHTPIN 14 // Data Pin of DHT 11 , for NodeMCU D5 GPIO no. is 14
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
dht.begin();
Serial.println ("");
Serial.println ("WiFi Connected!");
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // Reading temperature as Celsius (the default)
String firehumid = String(h) + String("%");
String firetemp = String(t) + String("C");
Serial.println("Humidity = ");
Serial.println(h);
Serial.println("Temperature = ");
Serial.println(t);
Firebase.setString("/Humidity",firehumid);
Firebase.setString("/Temperature",firetemp);
delay(4000);
if(Firebase.failed())
{
Serial.println("error!");
Serial.println(Firebase.error());
}
}
我 运行 几天前遇到了同样的问题,我的草图在大约一年前有效。 Firebase.failed() 返回 true,但 error 为空。
据我了解,使用带有秘密的实时数据库的旧方法已被弃用(正如它所说,在项目设置的数据库秘密页面上),取而代之的是带有 OAuth 2.0 的 Firebase Admin SDK。
但是,据我所知,没有Arduino库直接支持firebase的这种身份验证,所以我想找到其他方法让它发挥作用。
我设法找到了一种适合我的解决方法,我认为它更适合这类物联网应用程序,因为它在传感器方面需要的资源更少。
我的解决方案是使用 Firebase Cloud Functions 和 http 触发器 。
基本上不用为 Firebase 使用 Arduino 库,你可以定义 JS 函数,它可以存储或从数据库中检索你的数据,将它们部署到 Firebase 并通过简单的 https[=49= 在你的 Arduino sketch 中调用它们] 和 http 调用。
所以我的 arduino 草图的相关部分如下所示:
#include <ESP8266HTTPClient.h>
HTTPClient http;
// you will see this url, after you deployed your functions to firebase
#define FIREBASE_CLOUDFUNCTION_URL "http://<your-function-url>.cloudfunctions.net/add"
...
// Make sure you are connected to the internet before you call this function
void uploadMeasurements(float lux, float ctemp, float hum) {
String url = String(FIREBASE_CLOUDFUNCTION_URL) + "?lux=" + String(lux,2)
+ "&temp=" + String(ctemp,2) + "&hum=" + String(hum,2);
http.begin(url);
int httpCode = http.GET();
// you don't have to handle the response, if you dont need it.
String payload = http.getString();
Serial.println(payload);
http.end();
}
我将测量值放入查询参数中,但您也可以将它们发送到请求正文中。
我存储数据的云函数如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.add = functions.https.onRequest((request, response)=>
{
console.log('Called with queries: ', request.query, ' and body: ', request.body);
var ref = admin.database().ref("any/endpoint/child");
var childRef = ref.push();
let weatherData = {
lux: request.query.lux,
temp: request.query.temp,
hum: request.query.hum,
// this automatically fills this value, with the current time
timestamp : admin.database.ServerValue.TIMESTAMP
};
childRef.set(
weatherData
);
response.status(200).send("OK!");
});
您可以初始化一个存储您的云函数并可以使用 Firebase CLI 部署到 Firebase 的项目,在您的项目文件夹中调用 firebase init,然后选择cloudfunctions 通过设置过程(我也选择了托管,因为我有一个前端来查看收集的数据。)
现在,我有一个使用此解决方案的小传感器,每分钟上传一次测量值,并且没有任何问题。
希望对您有所帮助。
esp8266 上的 Firebase 问题
访问此站点
https://www.grc.com/fingerprints.htm
然后输入
搜索您最新的指纹
test.firebaseio.com
采集指纹
查找此库并在此处替换它:
"Arduino/libraries/firebase-arduino-master/src/FirebaseHttpClient.h"
我的传感器正在收集但未将数据推送到 Firebase。正如预期的那样 Firebase.failed returns 正确但 Firebase.error 为空。
我尝试更改 FirebaseHttpClient.h 文件中的指纹。我还尝试使用和不使用“/”更改 Firebase HOST。
#include "DHT.h"
#include <FirebaseArduino.h>
#include <ESP8266WiFi.h>
#define FIREBASE_HOST "your-project.firebaseio.com"
#define FIREBASE_AUTH "69DtX********************"
#define WIFI_SSID "LAPTOP" // Change the name of your WIFI
#define WIFI_PASSWORD "********" // Change the password of your WIFI
#define DHTPIN 14 // Data Pin of DHT 11 , for NodeMCU D5 GPIO no. is 14
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
WiFi.begin (WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
dht.begin();
Serial.println ("");
Serial.println ("WiFi Connected!");
Firebase.begin(FIREBASE_HOST,FIREBASE_AUTH);
}
void loop() {
float h = dht.readHumidity();
float t = dht.readTemperature(); // Reading temperature as Celsius (the default)
String firehumid = String(h) + String("%");
String firetemp = String(t) + String("C");
Serial.println("Humidity = ");
Serial.println(h);
Serial.println("Temperature = ");
Serial.println(t);
Firebase.setString("/Humidity",firehumid);
Firebase.setString("/Temperature",firetemp);
delay(4000);
if(Firebase.failed())
{
Serial.println("error!");
Serial.println(Firebase.error());
}
}
我 运行 几天前遇到了同样的问题,我的草图在大约一年前有效。 Firebase.failed() 返回 true,但 error 为空。
据我了解,使用带有秘密的实时数据库的旧方法已被弃用(正如它所说,在项目设置的数据库秘密页面上),取而代之的是带有 OAuth 2.0 的 Firebase Admin SDK。
但是,据我所知,没有Arduino库直接支持firebase的这种身份验证,所以我想找到其他方法让它发挥作用。
我设法找到了一种适合我的解决方法,我认为它更适合这类物联网应用程序,因为它在传感器方面需要的资源更少。
我的解决方案是使用 Firebase Cloud Functions 和 http 触发器 。 基本上不用为 Firebase 使用 Arduino 库,你可以定义 JS 函数,它可以存储或从数据库中检索你的数据,将它们部署到 Firebase 并通过简单的 https[=49= 在你的 Arduino sketch 中调用它们] 和 http 调用。
所以我的 arduino 草图的相关部分如下所示:
#include <ESP8266HTTPClient.h>
HTTPClient http;
// you will see this url, after you deployed your functions to firebase
#define FIREBASE_CLOUDFUNCTION_URL "http://<your-function-url>.cloudfunctions.net/add"
...
// Make sure you are connected to the internet before you call this function
void uploadMeasurements(float lux, float ctemp, float hum) {
String url = String(FIREBASE_CLOUDFUNCTION_URL) + "?lux=" + String(lux,2)
+ "&temp=" + String(ctemp,2) + "&hum=" + String(hum,2);
http.begin(url);
int httpCode = http.GET();
// you don't have to handle the response, if you dont need it.
String payload = http.getString();
Serial.println(payload);
http.end();
}
我将测量值放入查询参数中,但您也可以将它们发送到请求正文中。
我存储数据的云函数如下所示:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
exports.add = functions.https.onRequest((request, response)=>
{
console.log('Called with queries: ', request.query, ' and body: ', request.body);
var ref = admin.database().ref("any/endpoint/child");
var childRef = ref.push();
let weatherData = {
lux: request.query.lux,
temp: request.query.temp,
hum: request.query.hum,
// this automatically fills this value, with the current time
timestamp : admin.database.ServerValue.TIMESTAMP
};
childRef.set(
weatherData
);
response.status(200).send("OK!");
});
您可以初始化一个存储您的云函数并可以使用 Firebase CLI 部署到 Firebase 的项目,在您的项目文件夹中调用 firebase init,然后选择cloudfunctions 通过设置过程(我也选择了托管,因为我有一个前端来查看收集的数据。)
现在,我有一个使用此解决方案的小传感器,每分钟上传一次测量值,并且没有任何问题。
希望对您有所帮助。
esp8266 上的 Firebase 问题
访问此站点
https://www.grc.com/fingerprints.htm
然后输入
搜索您最新的指纹test.firebaseio.com
采集指纹
查找此库并在此处替换它:
"Arduino/libraries/firebase-arduino-master/src/FirebaseHttpClient.h"