I’ve got a Photon that keeps losing Wifi connectivity. It wouldn’t be a problem, since it looks like it reconnects fine, but I’m also using an MQTT library to push events to an MQTT broker. When the Photon drops off-line, my connection to the MQTT broker is broken and I’m having trouble re-establishing it. At least…that’s what I think is going on. I tried to add UDP/Papertrail logging, but that’s not working all that well either. Alas…
I’m still not 100% sure what is going on, but looking in the MQTT code, I see that it creates a single TCPClient object and uses (and re-uses) that for connections. Is that safe? So, if I am connected, then the connection fails (and breaks/disconnects the underlying TCP socket), is it safe to re-use that TCPClient object to create a new connection? Or, is it better to create an entirely new TCPClient object? I’m kind of grasping at straws as to what could be preventing the reconnection.
[SOLVED] - It turns out that reusing the TCPClient
object was not the underlying cause of my problems. As far as I can tell, that’s fine to do. In my case, I had to add a significant delay between reconnection attempts. At first I was delaying for 50ms between attempts. I switched this to 15000ms, based on a few posts I read talking about hard-wired timeouts for the unerlying sockets on the Photon. Once I made this change, everything seemed to work a lot better. You may be able to get away with shorter delays, perhaps as short as 5000ms, but for my case a 15s reconnection delay every few hours when it loses its connection is not a problem.
Also, specific to the MQTT library, I found an issue with the MQTT host server IP address. I was passing that in as a byte array, and that array was scoped to the setup()
function. I was doing it in a way that meant it could successfully connect once, but then the array fell out of scope and was deleted from the stack. So, the next time the MQTT library tried to reconnect, instead of a byte array for the IP address, I assume it had a null pointer. The lesson is: Make sure the variables you pass to the MQTT constructor do not get deleted or collected due to scoping.