0

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 - WiFiClientSecure). With my way Everything is also ok when i put topic direct value: "sometopic/abc/adbc" to input param. (Hardcoded)

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

But My program has to check some configs to get MQTT topics from EEPROM so it can't assign TOPIC when initialize variable at head of file.

So i created new pointer to initialize value for MQTT variables. Then the library can't connect to server.

#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() always falied with ret = -1 (Connection failed) Address output:

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

What could be the problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.