gpt4 book ai didi

ssl - Adafruit MQTT 库错误连接失败

转载 作者:太空宇宙 更新时间:2023-11-03 14:25:27 24 4
gpt4 key购买 nike

Adafruit_MQTT 库很棒,但我遇到了问题。我在 Arduino 中将它与 NodeMCU (ESP8266) 一起使用。示例代码一切正常。 (TLS 连接 - WiFiClientSecure)。用我的方式当我将 topic direct value: "sometopic/abc/adbc" 输入参数时,一切都还可以。 (硬编码)

//Connect successfuly when i use:
GLB_MQTTClient = temMQTT;
Adafruit_MQTT_Publish *temPub = new Adafruit_MQTT_Publish(GLB_MQTTClient, "pubTopic", MQTT_QOS_1);
Adafruit_MQTT_Subscribe *temSub = new Adafruit_MQTT_Subscribe(GLB_MQTTClient, "sometopic/abc/adbc", MQTT_QOS_1);

但是我的程序必须检查一些配置才能从 EEPROM 获取 MQTT 主题,因此它无法在文件头初始化变量时分配 TOPIC。

所以我创建了新指针来初始化 MQTT 变量的值。然后图书馆无法连接到服务器。

#include <Arduino.h>
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Ticker.h>
#include <common.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

//Do something for wifi connection...

WiFiClientSecure GLB_secureClient;
//Init with default value, cuz the library does not have default constructor
Adafruit_MQTT_Client *GLB_MQTTClient = new Adafruit_MQTT_Client(NULL, NULL, NULL, NULL, NULL, NULL);
Adafruit_MQTT_Publish *GLB_MQTT_Publish = new Adafruit_MQTT_Publish(NULL, NULL, MQTT_QOS_1);
Adafruit_MQTT_Subscribe *GLB_MQTT_Subcribe = new Adafruit_MQTT_Subscribe(NULL,NULL,MQTT_QOS_1);

void initNormalConnection()
{
printf("\n\nAddress of GLB_secureClient: %p\n", &GLB_secureClient);
printf("Address of GLB_MQTTClient: %p\n", GLB_MQTTClient);
printf("Address of GLB_MQTT_Publish: %p\n", GLB_MQTT_Publish);
printf("Address of GLB_MQTT_Subcribe: %p\n", GLB_MQTT_Subcribe);


GLB_secureClient = WiFiClientSecure();
GLB_secureClient.setFingerprint(FINGER_PRINT);

Adafruit_MQTT_Client *temMQTT = new Adafruit_MQTT_Client(&GLB_secureClient, BROKER_HOST, BROKER_PORT);

GLB_MQTTClient = temMQTT;
Adafruit_MQTT_Publish *temPub = new Adafruit_MQTT_Publish(GLB_MQTTClient, pubTopic, MQTT_QOS_1);
Adafruit_MQTT_Subscribe *temSub = new Adafruit_MQTT_Subscribe(GLB_MQTTClient, subTopic, MQTT_QOS_1);

GLB_MQTT_Publish = temPub;
GLB_MQTT_Subcribe = temSub;
GLB_MQTT_Subcribe->setCallback(MQTTCtrCallback);
GLB_MQTTClient->subscribe(GLB_MQTT_Subcribe);

printf("\n\nAddress of GLB_secureClient: %p\n", &GLB_secureClient);
printf("Address of GLB_MQTTClient: %p\n", GLB_MQTTClient);
printf("Address of GLB_MQTT_Publish: %p\n", GLB_MQTT_Publish);
printf("Address of GLB_MQTT_Subcribe: %p\n", GLB_MQTT_Subcribe);

PRINTLN("\ninitNormalConnection");
}

void MQTTConnect()
{
int8_t ret;
// Stop if already connected.
PRINTLN("MQTTConnect");
if (GLB_MQTTClient->connected())
{
PRINTLN("connected");
return;
}
PRINTLN("not connected");

if (stateSmartConfig || WiFi.status() != WL_CONNECTED)
{
return;
}
PRINTLN("Connecting to MQTT... ");
uint8_t retries = 3;
ret = GLB_MQTTClient->connect();
PRINT("Connect MQTT result: ");
PRINTLN(ret);
while (ret != 0)
{ // connect will return 0 for connected
ERR_PRINTLN(GLB_MQTTClient->connectErrorString(ret));
DEBUG_PRINTLN("Retrying MQTT connection in 10 seconds...");
GLB_MQTTClient->disconnect();
delay(10000);
retries--;
if (retries == 0)
{
// basically die and wait for WDT to reset me
return;
}
}
Serial.println("MQTT Connected!");
}


void setup(){
//Do something and check config
initNormalConnection();
}

void loop()
{

//do somethings

MQTTConnect();
GLB_MQTTClient->processPackets(10000);
if (!GLB_MQTTClient->ping())
{
GLB_MQTTClient->disconnect();
}
}

ret = GLB_MQTTClient->connect() 总是失败 ret = -1(连接失败)地址输出:

Address of GLB_secureClient: 0x3ffef170 
Address of GLB_MQTTClient: 0x3fff167c
Address of GLB_MQTT_Publish: 0x3fff17e4
Address of GLB_MQTT_Subcribe:0x3fff17f4

Address of GLB_secureClient: 0x3ffef170
Address of GLB_MQTTClient: 0x3fff27ac
Address of GLB_MQTT_Publish: 0x3fff220c
Address of GLB_MQTT_Subcribe: 0x3fff2914

可能是什么问题

最佳答案

您误解了如何使用堆。您在堆上实例化 GLB_MQTTClient、GLB_MQTT_Publish、GLB_MQTT_Subcribe 两次,然后出现小的内存泄漏,您丢失了指向在声明部分中实例化的类的指针。仅声明一个指针,然后在 initNormalConnection() 中实例化一个对象。

代码摘录:

WiFiClientSecure GLB_secureClient;
//Init with default value, cuz the library does not have default constructor
Adafruit_MQTT_Client *GLB_MQTTClient;
Adafruit_MQTT_Publish *GLB_MQTT_Publish;
Adafruit_MQTT_Subscribe *GLB_MQTT_Subcribe;

void initNormalConnection()
{
printf("\n\nAddress of GLB_secureClient: %p\n", &GLB_secureClient);
printf("Address of GLB_MQTTClient: %p\n", GLB_MQTTClient);
printf("Address of GLB_MQTT_Publish: %p\n", GLB_MQTT_Publish);
printf("Address of GLB_MQTT_Subcribe: %p\n", GLB_MQTT_Subcribe);

GLB_secureClient = WiFiClientSecure();
GLB_secureClient.setFingerprint(FINGER_PRINT);

GLB_MQTTClient = new Adafruit_MQTT_Client(&GLB_secureClient, BROKER_HOST, BROKER_PORT);
GLB_MQTT_Publish = new Adafruit_MQTT_Publish(GLB_MQTTClient, pubTopic, MQTT_QOS_1);
GLB_MQTT_Subcribe = new Adafruit_MQTT_Subscribe(GLB_MQTTClient, subTopic, MQTT_QOS_1);

...`

关于ssl - Adafruit MQTT 库错误连接失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57642422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com