0

Adafruit_MQTT error Connection failed
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Adafruit_MQTT error Connection failed

by phanphuoc93pp on Sat Aug 24, 2019 9:16 am

Adafruit_MQTT library is awesome but i have got a problem with it.
I use it in Arduino with NodeMCU (ESP8266).
Everything is okey with example code. (TLS connection).
Everything is also ok when i put topic direct value: "sometopic/abc/adbc" to input param.
But My program has to check some configs to get MQTT topic from EEPROM so it can't assign TOPIC when initialize variable at head of file.
So i created new pointer for new value.

I got Connection failed when i use
This is my basic code:

Code: Select all | TOGGLE FULL SIZE

thank for reading.
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()
{


  String pub = String(GLB_DEVICE_ID) + "/pub";
  String sub = String(GLB_DEVICE_ID) + "/sub";

  pubTopic = pub.c_str();
  subTopic = sub.c_str();


  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);

/**

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, "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(){
    initNormalConnection();
 }

void loop()
{

//do somethings

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


Variable's addresses was changed.
The output of the address:
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


phanphuoc93pp
 
Posts: 1
Joined: Sat Aug 24, 2019 8:59 am

Please be positive and constructive with your questions and comments.