ESP8266 MQTT Glorifying Example and Getting Keepalive Timeouts

Hi Everyone,

I recently took Losant’s provided ESP8266 MQTT example found in their Github here → Link to Repo

After adding code to manage load switching and reporting state and a few other attributes, I am constantly running into the issue of hitting the “Keepalive Timeout” error. When using PlatformIO, I ensured the maximum packet size was set to 256 or higher (#define MQTT_MAX_PACKET_SIZE 512) within the “LosantDevice.h” file. As you can see, I am at 512 as of right now.

As the error continued to persist, I added “device.loop()” to occur more frequently within the functions that house the load switching itself which is roughly every second or so. I will say, that did help the error for about 4 hours of a test run, however, still having this issue continue.

Any help?


Hello @Charles_Laning,

Welcome to the Losant Forums, we’re happy you’re here!

Keepalive timeouts generally mean that something between your device and the Losant Platform terminated the connection.

There are a few posts here on the forums regarding the Keepalive timeout errors. Although some of them address the MQTT max packet size adjustment, some of them point to router issues.

Would you be able to send along a screenshot of your Application Log?

Thank you,

Hi Heath,

I did my absolute best to find every single post on the forums regarding this issue but had no luck.

Attached is my device/application log:

Hey @Charles_Laning,

Thanks for sending that along!

MQTT comes with a built-in Keep-Alive, where the client (i.e. your 8266 device) every so often sends a PINGREQ, and the server will send a PINGRESP in response. If the server does not see a PINGREQ in a given period, it assumes the client (your device) is dead for some reason and will mark the device as disconnected with a keepalive timeout.

Losant’s default Keepalive is 60 seconds. So, some reasons why you would get a Keepalive timeout could be your device losing power or losing its connection to the internet. Your router could also be a culprit, where it is blocking traffic in some cases.

Thank you,

Hi Heath,

I see, that does make sense. So MQTT must follow a TCP type protocol, where the client and the host are constantly in touch (with some form of wait time in between)?

Is there any suggestion you have in regards to how to find out what the specific issue maybe? Then a way of how to solve it?

I am ultimately looking to communicate for a minimum of 100 hours/test I run.