espressif / esp-aws-iot

AWS IoT SDK for ESP32 based chipsets
Apache License 2.0
257 stars 154 forks source link

How to keep MQTT connection always alive to receive subscribe topics messages? (CA-225) #124

Open twlawrenceko opened 2 years ago

twlawrenceko commented 2 years ago

Hello,

With the example provided in thing_shadow, it will disconnect after the full demo routine is complete. However, even if we remove the line returnStatus = DisconnectMqttSession(); it will still disconnect after keep alive timeout.

This is the log from AWS IoT:

{
  "clientId": "esp32",
  "timestamp": xx,
  "eventType": "disconnected",
  "clientInitiatedDisconnect": false,
  "sessionIdentifier": "xx",
  "principalIdentifier": "xx",
  "disconnectReason": "MQTT_KEEP_ALIVE_TIMEOUT",
  "versionNumber": 4
}

How can we keep ESP32 continue to receive messages of the topics that have subscribed in the do while loop?

Thank you

SolidStateLEDLighting commented 1 year ago

I believe you're not passing through MQTT_ProcessLoop on a regular basis. The MQTT_ProcessLoop function will ping the server and keep the connection alive on its own. MQTT_ProcessLoop is also responsible for allowing the event handlers to fire when a message arrives.