MQTT reconnect fails

fmuller-pi
Posts: 22
Joined: Wed Jan 23, 2019 8:41 am

MQTT reconnect fails

Postby fmuller-pi » Sun Feb 10, 2019 10:55 am

Hi,

I am using the ESP8266_RTOS_SDK_2.x.x version and I connect to my cloud (Google Cloud) using MQTT secured protocol. The flow of functions I call is chronological as follow:
1) NetworkInitSSL()
2) MQTTClientInit()
3) NetworkConnectSSL()
4) MQTTConnectWithResults()
5) MQTTSubscribe()
6) MQTTPublish()
Everything works just fine, and I can see that my messages are published to the cloud with no issue.

My problem happens if, for example, I publish a message that doesn't make it through (could happen for various reasons). Then MQTTPublish() returns an error code and the function closes the session by calling

Code: Select all

MQTTCloseSession(c)
. This is where my problems begin because I want to be able to restart a connection in the event this happens. But so far I have been unable to do this successfully. I tried to reconnect by calling

Code: Select all

MQTTDisconnect(&client)
after MQTTPublish() returns the error, and going through all the steps described above when first connecting but it doesn't work. It actually works once, but then doesn't work a second time. It fails at NetworkConnectSSL() and says it couldn't connect...

Any thoughts on what I could be doing wrong here? I would really appreciate some help.

Thanks!

fmuller-pi
Posts: 22
Joined: Wed Jan 23, 2019 8:41 am

Re: MQTT reconnect fails

Postby fmuller-pi » Sun Feb 17, 2019 5:41 am

Self-answer here.

The issue came from the fact that I was not doing network.disconnect(&network) after an issue occurred and before I was trying to reconnect to the MQTT client. In the ESP_SDK v2.x.x, they don't show this, but in the version v3.1 they show it properly.

Who is online

Users browsing this forum: No registered users and 6 guests