mqtt_client read error or end of stream (wrong topic len in TCP)

Leander
Posts: 28
Joined: Thu Sep 26, 2019 8:50 pm

mqtt_client read error or end of stream (wrong topic len in TCP)

Postby Leander » Mon Nov 04, 2019 7:23 pm

I'm using the ESP IDF to make mqtt connections to the broker. Most of the time, this works fine but sometimes I'm getting an error on the ESP32: mqtt_client read error or end of stream
Then the client disconnects the broker and after a timeout it reconnects. The same data is send many times and the error doesn't happen every time.

But because of this error the MQTT communication isn't reliable.

I've captured a Wireshark on the broker and found that sometimes the ESP is sending a wrong topic length in the MQTT communcation. The broker then closes the connection.

In the following image we can see that we (172.16.22.31) Publish a message (lldevice/event/MAC) and then receive data from the broker (172.17.31.21) on a subscribed topic (lldevice/event/MAC/confirm). This works fine until we get the malvformed Packet (selected in the screenshot)

Image
http://vickyenleander.be/Downloads/MQTT ... cation.jpg

Looking at the malformed packet, we see that the advertised topic lenth is set to 31498 while in reality its only 27 chars long. This causes the broker to disconnect the client.

Image
http://vickyenleander.be/Downloads/MQTT ... length.jpg

It seems like this doesn't have anything to do with the way data is send as the error occurs after sending the data. Just to be sure, i'm explaining the way I send the MQTT data. In my code I'm using a message buffer and a seperate task that sends the buffered messages:

Code: Select all

void ProcessMQTTSendData()
{
    mqttmessage *buf_msg = mqttclient.send_queue.GetHead();
    
    if(buf_msg != 0)
    {
        for(uint8_t i = 0;i<devicesettings.mqttservers.size();i++){
            if(devicesettings.mqttservers.at(i).connected){
                ESP_LOGD(TAG, "Sending mqtt data from queue to: %s with topic: %s",devicesettings.mqttservers.at(i).cfg.uri,buf_msg->topic.c_str());
                const char *temptopic = buf_msg->topic.c_str();
                printf("topic length: %i",strlen(temptopic));
                esp_mqtt_client_publish(devicesettings.mqttservers.at(i).client,temptopic,buf_msg->message.c_str(),0,0, 0);

                vTaskDelay(10 / portTICK_PERIOD_MS);
            }
        }
        buf_msg = mqttclient.send_queue.GetNext();
    }
}
Could this be an issue in the MQTT Library ?

ESP_cermak
Posts: 69
Joined: Thu Nov 01, 2018 8:32 am

Re: mqtt_client read error or end of stream (wrong topic len in TCP)

Postby ESP_cermak » Tue Nov 05, 2019 6:49 am

Hi Leander,

which version of IDF and esp-mqtt are you using?
There were indeed some issues like malformed packets in some cases but with older version of the library. Simple check is to update mqtt submodule to the latest master and see if the issues still present.

Leander
Posts: 28
Joined: Thu Sep 26, 2019 8:50 pm

Re: mqtt_client read error or end of stream (wrong topic len in TCP)

Postby Leander » Tue Nov 05, 2019 10:34 am

I'm working on the MQTT library v3.3-rc.
I did a download of the MQTT section on the IDF main release and overwritten the folder.

I did a clean and recompiled the whole project.

The issue seems to be solved now, thank you for the respons.

Who is online

Users browsing this forum: No registered users and 68 guests