MQTT subscription failed

I used MQTT Explorer to connect and subscribe to the broker and succeeded, but when I switched to code, it returned false or 0.

Following is the main code (for 256dpi/MQTT@^2.5.2):

void test()
{
    WiFiClient wifiClient;
    MQTTClient mqttClient;
    mqttClient.begin(BEMFA_HOST, BEMFA_PORT, wifiClient);
    while (!mqttClient.connect(BEMFA_CLIENT_ID))
    {
        Serial.print(".");
        delay(1000);
    }

    Serial.println("\nconnected!");

    auto r = mqttClient.subscribe("C04E30BA4408002");
    Serial.printf("Subscription result: %d", r);

    while (1)
        ;
}

It printed connected, but mqttClient.subscribe returned false. I tested both 256dpi/MQTT@^2.5.2 and arduino-libraries/ArduinoMqttClient@^0.1.8 and get the same result.
I really want to know why false or 0. I need a clue on what's going on.
Please help!

I modified my code to the following:

void test()
{
    WiFiClient wifiClient;
    MQTTClient mqttClient;
    mqttClient.begin(BEMFA_HOST, BEMFA_PORT, wifiClient);
    while (!mqttClient.connect(BEMFA_CLIENT_ID))
    {
        Serial.print(".");
        delay(1000);
    }

    Serial.println("\nconnected!");

    auto r = mqttClient.subscribe("test");
    Serial.printf("Subscription result: %d", r);
    Serial.printf("last error", mqttClient.lastError());

    while (1)
        ;
}

The error code is -8. From this code:

I can see it means "LWMQTT_REMAINING_LENGTH_MISMATCH", but what is this???

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.