Open Nuddel69 opened 1 year ago
Hi,
I'm in a similar situation, when I try to connect an MQTT broker over internet (eg. test.mosquitto.org) or in my LAN (Raspberry Pi) and there isn't any internet or network connection (I haven't possibility to check it before) calling PubSubClient::connect(const char* )
the ESP32 hangs in that row
I believe this may help: https://github.com/knolleary/pubsubclient/issues/403#issuecomment-1432869562 I answered thinking this is my problem, but I found your issue.
Link does not work by clicking, copy and paste it.
The client seems to freeze when executing
mqttClient.connect()
. I find it strange considering how it manages a few complete transmissions before this happens. I'm using GSM - I make sure the arduino is connected before each transmission by reconnecting and checking whether there's a valid IP-address present. This doesn't seem to be a common issue so I figured I'd try my luck here. Here's the mqtt function:There's a reconnect function executed before each MQTT transmission looking like this:
Here's a screenshot of the output:
After looking through the source code I'm guessing this is the part where I'm getting stuck (in
boolean PubSubClient::connect(const char *id, const char *user, const char *pass, const char* willTopic, uint8_t willQos, boolean willRetain, const char* willMessage, boolean cleanSession)
), but I can't seem to figure a way to solve itEdit:
I tried rewriting the function to use a unique pointer instead to ensure a fresh client, but the problem persists First declaring the object:
std::unique_ptr<PubSubClient> mqttClient;
And defining the function:Edit 2:
After some further testing I seem to have located the part causing issues. I placed print statements before and after each function call in the
connect()
method, and the only part where it seems to struggle is here:write(MQTTCONNECT,this->buffer,length-MQTT_MAX_HEADER_SIZE);
Is it possible to add some sort of either timeout or complete reset of the buffer before executing this command?