You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ESP32连接Firestore出现response payload read timed out错误求助

ESP32连接Firestore失败,持续出现Token请求超时错误(code: -6)

我使用ESP32搭配DS18B20温度传感器,尝试将温度数据上传至Firestore数据库,但ESP32无法正常连接Firestore。代码可正常编译并上传,但串口监视器持续输出Token请求超时的错误信息。

串口输出错误信息

Connecting to WiFi....

Connected with IP: 192.168.

firebase client v4.4.10 Token info: type = id token (GITKit token),
status = on request Token info: type = id token (GITKit token), status
= error Token error: code: -6, message: response payload read timed out
        Token info: type = id token (GITKit token), status = on
request Token info: type = id token (GITKit token), status = error
Token error: code: -6, message: response payload read timed out       
Token info: type = id token (GITKit token), status = on request Token
info: type = id token (GITKit token), status = error Token error:
code: -6, message: response payload read timed out        Token info:
type = id token (GITKit token), status = on request Token info: type =
id token (GITKit token), status = error Token error: code: -6,
message: response payload read timed out

使用的代码

#include <Arduino.h>
#include <OneWire.h>
#include <Wire.h>
#include <DallasTemperature.h>

#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include <addons/TokenHelper.h>

#define WIFI_SSID "TPlink"
#define WIFI_PASSWORD "laugh__ta1e"

#define API_KEY "the firestore API key"
#define FIRESTORE_PROJECT_ID "firestore project ID"
#define USER_EMAIL "email"
#define USER_PASSWORD "password"

int oneWireBus = 4;
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);

FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;

void setup()
{
  Serial.begin(115200);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  Serial.print("Connecting to WiFi");

  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print(".");
    delay(300);
  }

  Serial.println();
  Serial.print("Connected with IP: ");
  Serial.println(WiFi.localIP());
  Serial.println();

  Serial.printf("firebase client v%s\n\n", FIREBASE_CLIENT_VERSION);

  config.api_key = API_KEY;

  auth.user.email = USER_EMAIL;
  auth.user.password = USER_PASSWORD;

  config.token_status_callback = tokenStatusCallback;
  Firebase.reconnectNetwork(true);

  Firebase.reconnectWiFi(true);

  Firebase.begin(&config, &auth);
  delay(1500);
  Serial.println("Connected successfully");

  // Start the DS18B20 sensor
  sensors.begin();
  pinMode(btn, INPUT_PULLUP);
}

void loop()
{
  String path = "/Sensor/temp";
  FirebaseJson content;
  float tempC = sensors.getTempCByIndex(0);

  content.set("TempCollection/Device1/Temperature", String(tempC));
  Serial.print("update/add temp to data... ");

  if (Firebase.Firestore.patchDocument(&fbdo, FIRESTORE_PROJECT_ID, "", path.c_str(), content.raw(), "Temperature"))
  {
    Serial.printf("ok\n%s\n\n", fbdo.payload().c_str());
  }
  else
  {
    Serial.println("its in here !!!");
    Serial.println(fbdo.errorReason());
  }
}

已尝试的操作

  • 修改Firestore安全规则
  • 调整Firebase登录方式
  • 创建数据库时遇到CORS错误,禁用React扩展并取消控制台显示CORS错误后完成创建

怀疑点

问题可能出在#include <addons/TokenHelper.h>和FirebaseConfig config; config.token_status_callback = tokenStatusCallback;相关代码部分。


问题排查与解决方案

1. 补全Token回调函数

代码中仅设置了config.token_status_callback = tokenStatusCallback;,但未实现tokenStatusCallback函数,这是TokenHelper.h要求必须定义的回调逻辑,缺失会导致Token处理异常。添加以下代码:

void tokenStatusCallback(TokenInfo info) {
  Serial.printf("Token info: type = %s, status = %s\n", info.type.c_str(), info.status.c_str());
  if (info.status == "error") {
    Serial.printf("Token error: code: %d, message: %s\n", info.code, info.message.c_str());
  }
}

2. 修正Firestore数据结构与请求格式

你的patchDocument调用和FirebaseJson的结构不匹配,Firestore要求特定的字段格式:

void loop()
{
  String path = "Sensor/temp"; // 集合Sensor下的文档temp
  FirebaseJson content;
  sensors.requestTemperatures(); // 必须先请求温度,否则getTempCByIndex返回旧值或错误
  float tempC = sensors.getTempCByIndex(0);

  content.set("fields/Temperature/doubleValue", tempC); // Firestore标准字段格式
  Serial.print("update/add temp to data... ");

  if (Firebase.Firestore.patchDocument(&fbdo, FIRESTORE_PROJECT_ID, "", path.c_str(), content.raw(), ""))
  {
    Serial.printf("ok\n%s\n", fbdo.payload().c_str());
  }
  else
  {
    Serial.println("its in here !!!");
    Serial.println(fbdo.errorReason());
  }
  delay(5000); // 添加延迟,避免频繁请求触发限流
}

3. 优化网络超时配置

错误code:-6为请求超时,可增加Firebase的超时阈值,在setup中添加:

config.timeout.serverResponse = 30000; // 服务器响应超时设为30秒
config.timeout.tokenRefresh = 30000; // Token刷新超时设为30秒

4. 验证Firebase凭据与权限

  • 确认API_KEY、FIRESTORE_PROJECT_ID、USER_EMAIL、USER_PASSWORD完全正确,无多余空格
  • 在Firebase控制台的Authentication->Sign-in method中,确保启用了邮箱/密码登录方式
  • 临时设置Firestore测试安全规则(上线后需修改为严格规则):
rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}

5. 修复代码其他问题

setup中使用了pinMode(btn, INPUT_PULLUP);但未定义btn变量,需添加#define btn 引脚号或删除该行(若未使用该引脚)。

内容的提问来源于stack exchange,提问作者Mohamed Amine Oussouas

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 03:11:02