Google iot MQTT - ESP32 首次连接,仅在 30m 后重新连接

2024-01-11

我正在使用 ESP32 与谷歌物联网云合作,我发送假值只是为了使用 MQTT 数据 PUB/SUB 进行测试,显然我成功发布了值,有时,我无法重新连接到谷歌物联网。 我不知道为什么它不断检查 wifi...publishing 并且不检查 JWT 密钥。

我注意到,如果我连接到谷歌物联网,然后从我的电脑上拔下esp32(断开电源),然后再次插回并尝试连接,我将输入“检查wifi”大约30m,直到我可以连接回谷歌物联网。如何解决这个问题?

我相信有一些东西可以解决这个问题:

// Time (seconds) to expire token += 20 minutes for drift
const int jwt_exp_secs = 3600; // Maximum 24H (3600*24)

当我设法获得向服务器发送信息的良好响应时,我得到了这个+:

entry 0x400806b8
Setup.....
Starting wifi
Connecting to WiFi
Connected
Waiting on time sync...
checking wifi...
connecting...Refreshing JWT
connected

Library connected!
incoming: /devices/esp32-device/config - 
incoming: /devices/esp32-device/config - 
Publishing value
Publishing value
Publishing value
Publishing value
Publishing value

有时我会得到大约 30m 的错误响应,使用相同的代码,它似乎正在发送恒定的数据,这不应该是恒定的。(不应该发生):

ho 0 tail 12 room 4
load:0x40080400,len:6352
entry 0x400806b8
Setup.....
Starting wifi
Connecting to WiFi
Connected
Waiting on time sync...
checking wifi...Publishing value
checking wifi...checking wifi...checking wifi...Publishing value
(Just keeps repeating)

这是与 MQTT 代码的主要连接,尝试修复问题,但没有成功:

// This file contains static methods for API requests using Wifi / MQTT
#ifndef __ESP32_MQTT_H__
#define __ESP32_MQTT_H__
#include <Client.h>
#include <WiFi.h>
#include <WiFiClientSecure.h>
#include <MQTT.h>
#include <CloudIoTCore.h> 
#include <CloudIoTCoreMqtt.h>
#include "ciotc_config.h" // Update this file with your configuration

void messageReceived(String &topic, String &payload) {
Serial.println("incoming: " + topic + " - " + payload);
}


// Initialize WiFi and MQTT for this board
Client *netClient;
CloudIoTCoreDevice *device;
CloudIoTCoreMqtt *mqtt;
MQTTClient *mqttClient;
unsigned long iat = 0;
String jwt;

String getDefaultSensor() {
return  "Wifi: " + String(WiFi.RSSI()) + "db";
}

String getJwt() {
Serial.println("Entered JWT");
delay(5000);
iat = time(nullptr);
Serial.println("Refreshing JWT");
jwt = device->createJWT(iat, jwt_exp_secs);
return jwt;
}

void setupWifi() { 
Serial.println("Starting wifi");

Serial.print("WIFI status = ");
Serial.println(WiFi.getMode());
WiFi.disconnect(true);
delay(3000);
WiFi.mode(WIFI_STA);
delay(3000);
Serial.print("WIFI status = ");
Serial.println(WiFi.getMode());

WiFi.begin(ssid, password);
Serial.println("Connecting to WiFi");
while (WiFi.status() != WL_CONNECTED) {
  delay(100);
 }
Serial.println("Connected");
delay(5000);

configTime(0, 0, ntp_primary, ntp_secondary);
Serial.println("Waiting on time sync...");
while (time(nullptr) < 1510644967) {
  delay(10);
}
}


void connectWifi() {
Serial.print("checking wifi...");
while (WiFi.status() != WL_CONNECTED) {
 Serial.print(".");
}
delay(5000);
}


bool publishTelemetry(String data) {
return mqtt->publishTelemetry(data);
}

 bool publishTelemetry(const char* data, int length) {
 return mqtt->publishTelemetry(data, length);
 }

 bool publishTelemetry(String subfolder, String data) {
 return mqtt->publishTelemetry(subfolder, data);
 } 

 bool publishTelemetry(String subfolder, const char* data, int length) {
 return mqtt->publishTelemetry(subfolder, data, length);
 }

 void connect() {
 connectWifi();
  mqtt->mqttConnect();

  delay(5000);
 }

 void setupCloudIoT() {
 device = new CloudIoTCoreDevice(
  project_id, location, registry_id, device_id,
  private_key_str);

 setupWifi();
 netClient = new WiFiClientSecure();
 mqttClient = new MQTTClient(512);
 mqttClient->setOptions(180, true, 1000); // keepAlive, cleanSession, timeout
 mqtt = new CloudIoTCoreMqtt(mqttClient, netClient, device);
 mqtt->setUseLts(true);
 mqtt->startMQTT();

 delay(5000);
 }
 #endif //__ESP32_MQTT_H__

这是main.cpp:

#include <Arduino.h>
#include <WiFiClientSecure.h>
#include "esp32-mqtt.h"
#include <ArduinoJson.h>
#define led 14

char buffer[100];
float counter = 0;
float counter1 = 0;

void setup() {
Serial.begin(115200);

Serial.println("Setup.....");
pinMode(led, OUTPUT);

setupCloudIoT();
}

unsigned long lastMillis = 0;

void loop() {
mqtt->loop();
delay(10);  // <- fixes some issues with WiFi stability

if (!mqttClient->connected()) {
connect();
}

counter++;
counter1++;

if (millis() - lastMillis > 1000) {
Serial.println("Publishing value");
lastMillis = millis();
float temp = counter;
float hum = counter1;
StaticJsonDocument<100> doc;
doc["temp"] = temp;
doc["humidity"] = hum;
serializeJson(doc, buffer);
publishTelemetry(buffer);
}
}

有人知道是否有其他模块没有同样的问题?


恕我直言,您面临的问题不是您的代码或您使用的 MQTT-lib 的问题。 ESP32核心包似乎有问题(1.04仍然有)。请参阅 github 中的问题集合以及一些建议的解决方案,这些解决方案最终延迟了问题,但没有解决问题。
MQTT 问题2018年至今问题汇总 https://github.com/knolleary/pubsubclient/issues/624
这些都是悬而未决的问题关于 WiFi 重新/连接问题 https://github.com/espressif/arduino-esp32/issues/1212
A 引发错误的测试用例 https://github.com/espressif/arduino-esp32/issues/1921#issuecomment-471927946从其中一篇文章链接。
我在具有 MQTT 连接的 esp8266 上使用相同的程序来识别 ESP32 特定问题,并且它运行了几个月。
ESP32 的人们有一个坏习惯,就是将问题保留一个月而不加评论,希望陈旧的机器人能够将其关闭。所以我只是引用了未解决的问题,如果您搜索已解决的问题,您会发现更多)由过时的机器人而不是解决方案关闭!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google iot MQTT - ESP32 首次连接,仅在 30m 后重新连接 的相关文章

随机推荐

  • Python/Pandas 计算 Ichimoku 图表组件

    我有 Pandas DataFrame 对象 其中包含日期 开盘价 收盘价 每日最低股票数据和最高股票数据 我想计算的组成部分Ichimoku https www investopedia com terms i ichimoku clou
  • 如何在 Ubuntu 18.04 上安装或升级到 sqlite 3.33.0?

    我目前正在运行带有 SQLite3 的 Ubuntu 18 04 SQLite 3 的版本为 3 22 0 我需要将其升级到版本 3 33 0 以利用可用的新功能 如果我使用 apt get 删除并重新安装 SQLite3 它只会重新安装
  • 如何在 woocommerce 中获取产品的特色图片

    请告诉我哪里出错了 产品特色图片未显示 args array post type gt product posts per page gt 80 product cat gt profiler orderby gt rand loop ne
  • 如何在 Enterprise Architect 中自动生成 HTML 输出

    Enterprise Architect 有一种方法可以生成 HTML RTF 等格式的文档 您可以发布它 但您必须使用其 GUI 手动执行此操作 当您的 eap 文件位于 CVS Subversion 服务器中时 拥有一个每天检查最新版本
  • 将固定大小的 ImageView 在父 ConstraintLayout 中水平居中

    我有一个相当简单的布局 AConstraintLayout占满整个屏幕 一大片CardView在顶部有一个ImageView那是一半CardView和上面的一半 实际上不是一半 但你明白了 然而 有两个问题 ImageView粘在父级的左侧
  • dplyr 连接 A 列或 B 列

    如何使用 dplyrs 连接函数在 R 中通过 OR 语句连接 2 个表 例如 使用以下密钥将 df1 加入到 df2 上 df1 ColumnA df2 ColumnA 或 df1 ColumnA df2 ColumnB library
  • Boost::GIL 如何将图像以 JPEG 或 PNG 格式保存到 char* 中?

    所以我在保存到文件时看到了很多示例 但我想知道是否可以保存到 char 或字符串而不是文件中 也就是说将其保存在内存中 boost 本身似乎没有任何东西可以促进这一点 所有 I O 似乎都基于提供文件名 不过好像还有延伸here http
  • 在可编辑模式下使用单独的源目录安装包

    情况 这是示例包的结构 tree Foo Foo setup py src bar py init py 包的名称应为foo但是包的源文件放置在src folder 文件的内容是 设置 py from setuptools import s
  • 隐藏 ASP.NET MVC5 中某些角色的链接

    所以这听起来可能是一个愚蠢的问题 但如何仅为管理员用户显示链接 假设普通用户看到以下链接 主页 关于 联系我们 管理员用户会看到以下链接 主页 关于 联系 管理员 我尝试限制控制器并链接菜单上的控制器 但它仍然向所有人显示链接 只是不允许除
  • std::shared_mutex 和 std::shared_lock 是读者更喜欢还是作者更喜欢?

    在读写锁的实现中 我们可以利用std shared mutex with std shared lock and std lock guard or std unique lock Question gt 这个新专题是作者还是读者更喜欢 根
  • android中是否需要关闭参数/参数InputStream?

    所有流和 bufferedReader 都需要关闭我的问题是如果 流和 bufferedReader 位于方法参数 参数内 也需要关闭吗 正常代码示例 InputStream i entity getContent i close 问 如果
  • 如何将代码从 O(n^2) 优化为 nlog(n)

    给定一个数字数组 以产生最大值的方式排列它们 例如 如果给定数字为 54 546 548 60 则排列 6054854654 给出最大值 如果给定的数字是 1 34 3 98 9 76 45 4 则排列 998764543431 给出最大值
  • 从数据库执行代码

    我有一个 PHP 代码存储在数据库中 我需要在检索时执行它 但我的代码是HTML和PHP的混合体 主要用在echo 看起来像我的代码的示例 echo Some Text var something more text anotherVar
  • WPF 应用程序不再显示主窗口

    我的应用程序构建并运行良好 我在任务栏中看到一个图标 显示该窗口存在 但它从未显示 我从 app xaml 调用 StartupUri MainWindow Xaml 而 mainwindow 仅包含一些函数和 InitializeComp
  • 在 Xcode 10 中找不到 -lstdc++.6 的库

    我无法在 Xcode Beta 和 GM 版本中构建基于 Cordova 的 iOS 应用程序 因为在此项目中使用 lstdc 6 为什么因为苹果已经弃用了 lstdc 6这个库在较新的 Xcode 版本中 所以我已经从Link Binar
  • 在 Activity 之间传递 ArrayList

    我已经实现了一个 Parcelable 类 public class Evento implements Parcelable private int private String private String imagen more at
  • 有没有办法通过一次操作从 TFS 中的各个文件夹中检出多个文件

    有没有办法通过一次操作从 TFS 中的各个文件夹中检出多个文件 我已经修改了多个目录下的多个文件 但我希望它们只需单击一次即可签出 也可以单击一次签入 Thanks 是的 使用 TFS 命令行客户端 tf exe 如果您有可用的 tf ex
  • window.pageYOffset 始终为 0,并且溢出-x: 隐藏

    我正在创建一个网页 其中包含一些仅需要在特定时间滑入的屏幕外内容 为了实现这一目标 我正在设置overflow x hidden on html body 这样用户就无法向左或向右滚动来获取内容 但是 在应用程序中的某个时刻 我还需要用户向
  • 在 iPhone 中重新定位后,框架/窗口尺寸“不正确”

    在我的 iPhone OS 应用程序中 我想要 需要 观察设备方向的变化 以便重新排列屏幕的某些部分 我使用的方法是使用CGRect frame UIScreen mainScreen applicationFrame获取屏幕尺寸 并从那里
  • Google iot MQTT - ESP32 首次连接,仅在 30m 后重新连接

    我正在使用 ESP32 与谷歌物联网云合作 我发送假值只是为了使用 MQTT 数据 PUB SUB 进行测试 显然我成功发布了值 有时 我无法重新连接到谷歌物联网 我不知道为什么它不断检查 wifi publishing 并且不检查 JWT