【问题标题】:How to upload data to Firebase using Nodemcu?无法使用 Nodemcu 将数据上传到 Firebase
【发布时间】:2019-11-26 01:32:50
【问题描述】:

我的传感器正在正确收集数据,但没有将数据推送到 Firebase。正如预期的那样 Firebase.failed 返回 true 但 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());
  }

}

【问题讨论】:

  • 告诉我们您的草图基于此示例github.com/FirebaseExtended/firebase-arduino/tree/master/… 会有所帮助。我正在使用最新的 FirebaseArduino 库和最新的 ArduinoJson 5.x(不是 6.x!),并且演示运行良好。也许您应该回到模板并首先验证您为四个变量添加了正确的值?
  • DHT 标签是关于分布式哈希表,而不是温度传感器。
  • 您能否尝试运行随 Arduino firebase 库提供的基本示例并在此处发布结果。您必须在 DB 上启用读/写才能允许从 nodeMCU 写入数据。

标签: firebase firebase-realtime-database arduino arduino-ide nodemcu


【解决方案1】:

几天前,我在使用大约一年前的草图时遇到了同样的问题。 Firebase.failed() 返回 true,但 error 为空。

据我了解,使用带有密码的实时数据库的旧方法已弃用(如其所述,在项目设置中的数据库密码页面上),并且有 @ 987654321@ 用 OAuth 2.0 代替它。

然而,截至发帖时,据我所知,没有 Arduino 库直接支持这种 Firebase 身份验证,所以我想找到另一种方法来使其工作。

我设法找到了一个适合我的解决方法,而且我认为更适合这类 IOT 应用程序,因为它在传感器方面需要的资源更少。

我的解决方案是使用带有 http 触发器Firebase Cloud Functions。 基本上,您可以定义 JS 函数,而不是使用 Firebase 的 Arduino 库,这些函数可以存储或从数据库中检索您的数据,将它们部署到 Firebase 并通过简单的 https 和 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,然后通过设置过程选择云函数(我也选择了托管,因为我有一个前端来查看收集的数据。)

现在,我有一个使用这种解决方案的小传感器,每分钟上传一次测量值,它可以正常工作。

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    esp8266 上的 Firebase 问题

    访问这个网站

    https://www.grc.com/fingerprints.htm

    并通过键入搜索您的最新指纹

    test.firebaseio.com

    取指纹

    查找此库并将其替换为您的 in :

    "Arduino/libraries/firebase-arduino-master/src/FirebaseHttpClient.h"
    

    【讨论】:

      猜你喜欢
      • 2019-05-06
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 2016-03-19
      • 1970-01-01
      • 2018-06-14
      • 1970-01-01
      相关资源
      最近更新 更多