Adafruit MQTT Subscribe using MAC Address (ESP8266)

Hello,

I’m working with the ESP8266, and have successfully setup connecting the module to a MQTT broker. One of the things I require is a library which supports QoS 1 for publishing, and from what I’ve been able to find so far, it seems Adafruit MQTT Client is one of the only options. The problem I’m having is that I cannot subscribe when creating a topic which isn’t hard-coded.

I made small additions to the library to allow me to create an Adafruit_MQTT_Subscribe variable uninitialized, then set it during setup.

If I explicitly enter the topic as seen below, I’m able to see that the debug publishes I send are being received.

const char *_topic = "/req";
  Serial.println("_topic:"+String(_topic));
  
  mqttReq = Adafruit_MQTT_Subscribe(&mqtt, _topic, MQTT_QOS_1);
  if (mqtt.subscribe(&mqttReq))
    Serial.println("Subscribed.");
  else 
    Serial.println("Could not subscribe.");

If I try to pass the topic like below, I do not see any publishes being received, although I do get “Subscribed”."

String topicString = "/req";
  int len = topicString.length() + 1;
  char topicChar[len];
  topicString.toCharArray(topicChar, len);
  
  //const char *_topic = "/req";
  const char *_topic = topicChar;
  Serial.println("_topic:"+String(_topic));
  
  mqttReq = Adafruit_MQTT_Subscribe(&mqtt, _topic, MQTT_QOS_1);
  if (mqtt.subscribe(&mqttReq))
    Serial.println("Subscribed.");
  else 
    Serial.println("Could not subscribe.");

I need to be able to subscribe using the module’s MAC address, though for simplicity I’ve been trying to get “/req” to work.

Does anyone know what I might be doing wrong?
I’ve tried doing some searches regarding Adafruit MQTT and subscribing using MAC addresses, but I haven’t been able to find anything which discusses that.
If anyone knows of another option for a MQTT library which supports publishing QoS 1, please let me know.

Full code:

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

/************************* WiFi Access Point *********************************/

#define WLAN_SSID       "..."
#define WLAN_PASS       "..."

/************************* Adafruit.io Setup *********************************/

#define MQTT_SERVER      "..."
#define MQTT_SERVERPORT  12798                  
#define MQTT_USERNAME    "..."
#define MQTT_PASSWORD    "..."

/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, MQTT_SERVER, MQTT_SERVERPORT, MQTT_USERNAME, MQTT_PASSWORD);

/****************************** Feeds ***************************************/

// Setup a feed for subscribing to requests.
Adafruit_MQTT_Subscribe mqttReq;

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() 
{
  Serial.begin(38400);
  delay(10);

  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); 
  Serial.println(WiFi.localIP());
  //////////////////////////////////

  String topicString = "/req";
  int len = topicString.length() + 1;
  char topicChar[len];
  topicString.toCharArray(topicChar, len);
  
  //const char *_topic = "/req";
  const char *_topic = topicChar;
  Serial.println("_topic:"+String(_topic));
  
  mqttReq = Adafruit_MQTT_Subscribe(&mqtt, _topic, MQTT_QOS_1);
  if (mqtt.subscribe(&mqttReq))
    Serial.println("Subscribed.");
  else 
    Serial.println("Could not subscribe.");
}

uint32_t x=0;
void loop() 
{
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect(); 
 
  // this is our 'wait for incoming subscription packets' busy subloop
  // try to spend your time here
  Adafruit_MQTT_Subscribe *subscription;
  while ((subscription = mqtt.readSubscription(100))) 
  {
    if (subscription == &mqttReq)
    {
      Serial.print(F("Got request: "));
      Serial.println((char *)mqttReq.lastread);
    }
  }
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() 
{
  int8_t ret;
  if (mqtt.connected()) // Stop if already connected.
    return;

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) 
  { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0)    // basically die and wait for WDT to reset me
         while (1);
  }
  Serial.println("MQTT Connected!");
}