# How to get MQTT disconnected callbacks in an application based on the Shadow demo (202011)

**URL:** <https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341>\
**Category:** AWS\
**Created:** [December 13, 2020, 6:35pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341 "2020-12-13T18:35:02Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 13, 2020, 6:35pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/1 "2020-12-13T18:35:02Z")

</div>

Hello,  
I’m running a shadow connection application that is based on the 202011 Shadow demo. In this implementation the MQTT connection is establish by calling code MQTT functions - MQTT\_Init & MQTT\_Connect.  
One of the MQTT\_Init parameters is a user event callback of type MQTTEventCallback\_t that notify the user about incoming packet events but not MQTT events like disconnect.  
So how can I know that I lost the MQTT connection?  
Thanks,  
Eyal

---

<div class="post-metadata">

**Author:** ![abrinao](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/abrinao/32/735_2.png) [@abrinao](https://forums.freertos.org/u/abrinao)\
**Post date:** [December 14, 2020, 6:44pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/2 "2020-12-14T18:44:12Z")

</div>

You can check if `MQTTRecvFailed` is returned by `MQTT_ProcessLoop`. If the MQTT connection is no longer active, this status is returned.

---

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 14, 2020, 8:34pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/3 "2020-12-14T20:34:27Z")

</div>

Thank you for your response.  
I’m a bit confused about the role of the MQTT\_ProcessLoop.  
I would expect the MQTT to run forever when I’m working with a shadow, that is, waiting for delta message from the broker in the callback and send updates on a need to basses but in the Shadow demo I don’t see such a infinite loop calling MQTT\_ProcessLoop.  
In such a case of an infinite loop I can understand the MQTT status check for disconnection with the return value but this the case in the shadow demo.  
Can you please elaborate on the usage of the MQTT\_ProcessLoop?  
Thanks

---

<div class="post-metadata">

**Author:** ![abrinao](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/abrinao/32/735_2.png) [@abrinao](https://forums.freertos.org/u/abrinao)\
**Post date:** [December 15, 2020, 8:24pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/4 "2020-12-15T20:24:47Z")

</div>

The Shadow demo is using `prvWaitForDeleteResponse` to call `MQTT_ProcessLoop`. It will run for a duration of `MQTT_PROCESS_LOOP_TIMEOUT_MS`. This is done by passing a timer function called `prvGetTimeMs` in `mqtt_demo_helpers.c`. Note that the user event callback is called by `MQTT_ProcessLoop` whenever an incoming MQTT packet is received from AWS IoT Core.

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 15, 2020, 10:08pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/5 "2020-12-15T22:08:19Z")

</div>

This may also be helpful: [https://www.freertos.org/mqtt/mqtt-agent-demo.html](https://www.freertos.org/mqtt/mqtt-agent-demo.html)

---

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 17, 2020, 5:28pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/7 "2020-12-17T17:28:39Z")

</div>

Thank you for this info, I can’t see `prvWaitForDeleteResponse` in 202011 but in 202012 I can see it.

---

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 20, 2020, 9:32pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/8 "2020-12-20T21:32:43Z")

</div>

In the MQTT agent demo I see a use of MQTTAgent\_CommandLoop that is part of FreeRTOS-Plus-MQTT.  
This library is not part of the 202012 AFR, what is the relationship between this MQTTAgent\_CommandLoop and the MQTT\_ProcessLoop?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 20, 2020, 10:15pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/9 "2020-12-20T22:15:21Z")

</div>

The MQTT agent sits on top of coreMQTT. MQTTAgent\_CommandLoop() is part of the MQTT agent, whereas MQTT\_ProcessLoop() is part of coreMQTT. Note the agent is not complete yet - but when using the agent you call MQTTAgent\_CommandLoop() to have a separate thread manage MQTT for your application so the application does not have to call MQTT\_ProcessLoop() at all. The MQTTAgent\_COmmandLoop() only returns if there is an error that probably requires the creation of a new connection.

---

<div class="post-metadata">

**Author:** ![EyalG](https://avatars.discourse-cdn.com/v4/letter/e/b782af/32.png) [@EyalG](https://forums.freertos.org/u/EyalG)\
**Post date:** [December 21, 2020, 3:38pm UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/10 "2020-12-21T15:38:02Z")

</div>

Thanks for that explanation.  
So if I’m standing now in a point where I’m porting my existing product FW that is based on 202002 to 202012 and I don’t want to restructure the code again in a month or so then should I use the FreeRTOS-Plus-MQTT as it is in the MQTT agent demo and upgrade it when it will be ready or should I use the core MQTT directly (that is using MQTT\_ProcessLoop)?

---

<div class="post-metadata">

**Author:** ![SRA](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@SRA](https://forums.freertos.org/u/SRA)\
**Post date:** [December 23, 2020, 12:47am UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/11 "2020-12-23T00:47:22Z")

</div>

@rtel

Other than the readme notes about code standards, and that this new Agent is still green, is there a technical reason this wouldn’t work with `iot_ble_mqtt_transport.c`?

The file `mqtt_demo_ble_transport.c` right now seems like it would/could/should be mostly replaced with this agent with the exception of the channel and callback inits. Seems like it would make sense to commonize software esp for demos. Any idea on that?

There is a file at `amazon-freertos/libraries/c_sdk/standard/mqtt/src/iot_mqtt_agent.c` updated recently for CoreMQTT. Similar function names and responsibilities. Can I assume this is obsolete and only for backcompat?

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [December 23, 2020, 12:57am UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/12 "2020-12-23T00:57:39Z")

</div>

I’m afraid I don’t know enough about the BLE transport implementation to know for sure, but suspect it will work.

---

<div class="post-metadata">

**Author:** ![SRA](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@SRA](https://forums.freertos.org/u/SRA)\
**Post date:** [December 23, 2020, 1:06am UTC](https://forums.freertos.org/t/how-to-get-mqtt-disconnected-callbacks-in-an-application-based-on-the-shadow-demo-202011/11341/13 "2020-12-23T01:06:46Z")

</div>

That is the idea behind the “transports” right? Provide CoreMQTT a send and receive and away it goes?

The only annoyance I have is MQTT get serialized, then the MqttOverBle transport will de-serialize, then re-serialize into CBOR. A waste, but this is what the official demos do now so maybe there was no better option.

Any idea if between the two Agents one will be left standing?
