Proper way to restart MQTT and Wifi?

bkgoodman
Posts: 45
Joined: Fri Feb 17, 2017 12:41 pm

Proper way to restart MQTT and Wifi?

Postby bkgoodman » Fri Sep 25, 2020 10:20 pm

As any IoT device would warrant - I am connecting via Wifi and MQTT, and want to make sure if things go wonky (i.e. Wifi issues, etc) - my device can reconnect. I've had testing issues - and am now simulating by just turning off and turning on my Wifi access point.

I am largely using MQTTS_EXAMPLE code - and get an MQTT event "7" (??), and then an MQTT_EVENT_ERROR when my Wifi access point goes away, and I appear to lose connectivity to the access point.

From there I'm trying to do a combination of a lot of things to re-establish connectivity - namley, stopping MQTT then Wifi, then restarting them (in reverse order). So far - all combinations I've tried have had bad luck.

ANY IDEA how this should be done??

In my latest incarnation - I am first calling esp_mqtt_client_destroy(client_global); - but that appears to hang and not return. (Though when it does - once Wifi AP comes back, Wifi connection WILL come back - as if the two are on different threads).

so is there some secret sauce here I don't know? For example, if I get an MQTT_EVENT_ERROR - can I just "restart" MQTT service without touching Wifi, etc?

Any help would be appreciated!

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Proper way to restart MQTT and Wifi?

Postby PeterR » Fri Sep 25, 2020 11:11 pm

I would be brutal with all clients services on IP address grant & loss and then see what you have left to debug.
My experience is that the services are quite forgiving but there is no evidence/statement that that should be so.

So in my stack (as a paranoid/no brainer) I esp_mqtt_client_destroy() on loss of IP and esp_mqtt_client_init() on gain.
This may not be optimal but it is a 'I don't wanna debug' approach. I don't buy into the MQTT API though & the documentation is far from clear! Took me a while to even figure the URI format (and how that determines the transport).

Suggest you start with beefing up your Ethernet & IP event handlers logging - do you get an IP address event following disconnect? When in the cycle do you get Eth connect or IP grant? Then try a console key start/stop to short circuit restart tests.

Note: My other post https://www.esp32.com/viewtopic.php?f=2&t=17510 & that I seem to have MQTT web socket issues. Just to say I'm no expert!
& I also believe that IDF CAN should be fixed.

PeterR
Posts: 621
Joined: Mon Jun 04, 2018 2:47 pm

Re: Proper way to restart MQTT and Wifi?

Postby PeterR » Fri Sep 25, 2020 11:23 pm

Note that your call to destroy fails to return.
Suggests that your issues might be driver/ifs based then.

I have not seen this issue on Ethernet but I am still developing MQTT so I am not yet on edge cases. With IDF 4.1+ it should be easy to switch to Ethernet (say on an EVB) and then compare using cable disconnect. Its quite possible that Wifi behaves different.

Should also correct; I don't destroy. I esp_mqtt_client_stop() on IP loss & destroy on shutdown. Not test all these scenarios yet though!
& I also believe that IDF CAN should be fixed.

Who is online

Users browsing this forum: No registered users and 159 guests