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)
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.
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();
}
}