为什么 Wemos D1 R1 数据中的响应代码 400 到 Firebase 实时数据库?

问题描述 投票:0回答:1
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>
#include <DFRobot_DHT11.h>

const char* ssid = "WCS_LAB 2.4G";
const char* password = "secret";
const char* firebaseProject = "arduino-secret-secret-default-rtdb";
const char* databasePath = "/"; 

int ledPin = D3; // Connect LED to this pin

WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");

DFRobot_DHT11 DHT;
#define DHT11_PIN D2

// sendDataToFirebase function definition
void sendDataToFirebase(float temperature, float humidity) {
  // Rest of the Firebase transmission code...
  Serial.println("Firebase transmission started");
  timeClient.update();
  String kstTimestamp = convertToKST(timeClient.getEpochTime());
  String data = "{ \"temperature\": " + String(temperature, 2) + ", \"humidity\": " + String(humidity, 2) +
                 ", \"timestamp_kst\": \"" + kstTimestamp + "\" }";

  Serial.println("Data: " + data);

  WiFiClient client;
  HTTPClient http;

  String firebaseURL = "https://" + String(firebaseProject) + ".firebaseio.com" + databasePath;
  http.begin(client, firebaseURL);

  http.addHeader("Content-Type", "application/json");

  // Debug information
  Serial.println("HTTP request started");
  Serial.println("URL: " + firebaseURL);
  Serial.println("Data: " + data);

  int httpResponseCode = http.POST(data);

  if (httpResponseCode > 0) {
    Serial.print("Data sent to Firebase successfully, response code: ");
    Serial.println(httpResponseCode);
  } else {
    Serial.print("Failed to send data to Firebase, response code: ");
    Serial.println(httpResponseCode);

    String response = http.getString();
    Serial.println("Server response: " + response);

    Serial.print("Error: ");
    Serial.println(http.errorToString(httpResponseCode).c_str());
  }

  Serial.println("HTTP request ended");
  http.end();
}

// convertToKST function definition
String convertToKST(time_t utcTime) {
  struct tm *timeinfo;
  timeinfo = gmtime(&utcTime);

  // Convert to KST by adding 9 hours to UTC
  timeinfo->tm_hour += 9;

  char kstBuffer[50];
  strftime(kstBuffer, sizeof(kstBuffer), "%Y-%m-%d %H:%M:%S", timeinfo);

  return String(kstBuffer);
}

void setup() {
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  WiFi.begin(ssid, password);
  
  Serial.println("Setup started");

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("WiFi connected");
  timeClient.begin();

  while (!timeClient.update()) {
    timeClient.forceUpdate();
  }

  Serial.println("Time synchronization completed");
}

void loop() {
  Serial.println("Loop started");
  DHT.read(DHT11_PIN);
  float temperature = DHT.temperature;
  float humidity = DHT.humidity;

  Serial.println("Temperature: " + String(temperature, 2) + ", Humidity: " + String(humidity, 2));

  if (!isnan(temperature) && !isnan(humidity)) {
    Serial.println("Attempting to send data to Firebase");
    sendDataToFirebase(temperature, humidity);
  }

  delay(5000); // Execute every 5 seconds`
}

这是我的串行监视器输出:

10:05:18.777 -> HTTP request started
10:05:18.777 -> URL: secret
10:05:18.841 -> Data: { "temperature": 19.00, "humidity": 20.00, "timestamp_kst": "2023-12-04 10:05:17" }
10:05:19.085 -> Data sent to Firebase successfully, response code: 400
10:05:19.126 -> HTTP request ended
firebase firebase-realtime-database arduino
1个回答
0
投票

我发现的第一件事是您的 URL 不以

.json
结尾,这是与 Firebase 的 REST API 通信所必需的:

String firebaseURL = "https://" + String(firebaseProject) + ".firebaseio.com" + databasePath + ".json";
© www.soinside.com 2019 - 2024. All rights reserved.