Namaste @aggarg and Community Members,
I’m using the FreeRTOS V202002.00 downloaded from AWS portal. The MQTT enabled demo was downloaded sometime in the mid of May 2020. Later, by the end of July 2020, I downloaded the OTA enabled demo, again FreeRTOS V202002.00 based. I don’t remember the exact dates when I downloaded the images.
My requirement is to run both MQTT and OTA Update threads in parallel. The OTA Update thread will perform a firmware update whenever initiated from the AWS Cloud. The MQTT thread will periodically publish the application readings to the AWS Cloud and, receive all the messages associated with the topics subscribed for.
My first attempt is to test the existing MQTT Demo and OTA Demo codes, as is. For this, I’ve created two threads for running the two demos.
The code flow is:
-
app_main() → DEMO_RUNNER_RunDemos() → runDemoTask() → Complete the initialisation and establish Wi-Fi connectivity.
-
Instead of calling the RunMqttDemo(), I am calling my RunApplicationFunction().
-
This function creates a thread (pvMqttAndOtaTask), which in-turn creates two more threads (pvOtaUpdateTask and pvMqttTask).
The logic is shared below w/o going into the actual code details.
void pvMqttAndOtaTask( void * pvParameters )
{
/* OTA Update Demo Thread */
status = (int) Iot_CreateDetachedThread( pvOtaUpdateTask,
&otaUpdateDemoContext,
IOT_THREAD_DEFAULT_PRIORITY,
IOT_THREAD_DEFAULT_STACK_SIZE );
// Delay the MQTT thread creation for allowing the OTA thread to complete
// establishing the MQTT connection, which will then be used by the MQTT thread.
ESP_LOGI(TAG, "INFO : Delay before pvMqttTask() thread creation.");
vTaskDelay( pdMS_TO_TICKS(15000) );
while(1)
{
/* Mqtt Demo Thread */
status = (int) Iot_CreateDetachedThread( pvMqttTask,
&mqttDemoContext,
IOT_THREAD_DEFAULT_PRIORITY,
IOT_THREAD_DEFAULT_STACK_SIZE );
// Run the MQTT demo code every one minute. The MQTT demo completes in about 15 seconds.
vTaskDelay( pdMS_TO_TICKS(60000) );
}
}
Additional points to be noted are:
-
The OTA Demo thread is created first and then, after a 15 second delay, the MQTT Demo thread is created.
-
OTA Demo code flow is pvOtaUpdateTask thread → vStartOTAUpdateDemoTask() → vRunOTAUpdateDemo()
-
OTA Update Demo thread is created only once and the OTA Update Demo runs continuously. However, the MQTT Demo thread is created every minute for the MQTT Demo to run periodically.
-
The same MQTT connection is used as suggested, by creating a new
IotMqttConnection_t sharedMqttConnection
shared by both OTA Update Demo and MQTT Demo. -
Both the codes are run as is, w/o any major modifications within the demo codes. The only modifications are
- In the
vRunOTAUpdateDemo()
, thesharedMqttConnection = xConnection.xMqttConnection;
is added. - In the
RunMqttDemo()
, the_establishMqttConnection()
is commented and instead, themqttConnection = sharedMqttConnection;
statement is added.
- In the
Observation
- Both OTA Update Demo and MQTT Demo run in parallel (only once) using the same MQTT connection.
- The firmware crashes and restarts when the MQTT Demo runs the second time after completing the 1 minute delay.
- The error is traced back to the Line No. 606 in the iot_mqtt_api.c file → serializeSubscription = mqttConnection->pSerializer->serialize.subscribe; Below is the code block.
#if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1
if( mqttConnection->pSerializer != NULL )
{
if( mqttConnection->pSerializer->serialize.subscribe != NULL )
{
serializeSubscription = mqttConnection->pSerializer->serialize.subscribe;
}
else
{
EMPTY_ELSE_MARKER;
}
}
else
{
EMPTY_ELSE_MARKER;
}
#endif /* if IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1 */
- The partial console error log is provided below.
Guru Meditation Error: Core 0 panic'ed (LoadProhibited). Exception was unhandled.
Core 0 register dump:
PC : 0x40148a58 PS : 0x00060e30 A0 : 0x80148be0 A1 : 0x3ffe7e50
0x40148a58: _subscriptionCommon at D:\gitHub\device\firmware\aws\proj04\FreeRTOS\build/../libraries/c_sdk/standard/mqtt/src/iot_mqtt_api.c:606
Some Analysis
-
It looks like the
mqttConnection->pSerializer->serialize.subscribe
parameter is not getting populated from the MQTT connection created via OTA Update. -
The below code block is present within the _establishMqttConnection(), which does not get executed because the function is commented as mentioned above.
#if ( IOT_MQTT_ENABLE_SERIALIZER_OVERRIDES == 1 ) && defined( IOT_DEMO_MQTT_SERIALIZER )
networkInfo.pMqttSerializer = IOT_DEMO_MQTT_SERIALIZER;
#endif
Kindly assist in resolving this issue.
Thanks | Regards,
Dipen