egol wrote on January 24, 2020:
Hello and thank you for getting back to me.
I'll try to describe my implementation:
- My code base is 201906
- My target is a custom board based on the STM IOT kit (B-L475E-IOT01A)
- I started from the shadow demo and change the main task to fit my application needs.
- The main task have a message queue that receive messages from other tasks in the application and act accordingly.
- The main task, initialize communication to the shadow before it starts the loop that handle the messages
Now to you question of how I'm trying to handle the disconnect event (both WiFi and or MQTT):
- I defined a disconnect handler when I called IotMqtt_Connect - networkInfo.disconnectCallback = disconnectCallbackInfo;
- The disconnectCallback start a timer:
void _disconnectCallback(void* pArgument,IotMqttCallbackParam_t* const pParam)
{
(void)pArgument;
IotLogError("MQTT Disconnect callback service.");
xTimerStart(DisconnectRetry_Timer,0);
}
and when the timer expired its callback send a message to the main task about the disconnect event:
void DisconnectRetry_TimerCallback( TimerHandle_t xTimer )
{
MainLogicQueueData_t msgToMainLogic;
configASSERT( xTimer );
msgToMainLogic.eMsgType = eIntTasksMsg_MQTT_Disconnected;
sendMsgToMainLogic(&msgToMainLogic);
}
The main task, when getting this message is doing this:
AwsIotShadow_Init(0);
AwsIotNetworkManager_DisableNetwork(AWSIOT_NETWORK_TYPE_WIFI);
if( AwsIotNetworkManager_EnableNetwork( configENABLED_NETWORKS ) != configENABLED_NETWORKS )
{
IotLogError( "Failed to intialize all the networks configured for the device." );
xTimerStart(DisconnectRetry_Timer,0);
continue;
}
status = _establishMqttConnection(pIdentifier,
pNetworkServerInfo,
pNetworkCredentialInfo,
pNetworkInterface,
&global_wb_gw_mqttConnection);
status = _setShadowCallbacks(&deltaSemaphore,
global_wb_gw_mqttConnection,
pIdentifier,
thingNameLength);
_clearShadowDocument(global_wb_gw_mqttConnection, pIdentifier, thingNameLength);
And then the shadow should be recreated and the system should continue to work normally.
What I see is this:
- I disconnected the Ethernet connection to the AP to simulate a break
- After around 30 sec (this is the MQTT defined keep alive timeout) I get a message:
120 44968 [iot_thread] [ERROR][MQTT][44968] (MQTT connection 0x10006510) Failed to receive PINGRESP within 5000 ms.
121 44978 [iot_thread] [INFO ][MQTT][44978] (MQTT connection 0x10006510) Network connection closed.
122 44987 [iot_thread] [ERROR][WB_GW][44987] MQTT Disconnect callback service.
I then immediately reconnect the Ethernet and wait for my DisconnectRetry_Timer to expired (also 30 sec), when it does the message is sending to the main task and I see this in the log:
135 74995 [iot_thread] [INFO ][WB_GW][74995] Main logic receive message - eIntTasksMsg_MQTT_Disconnected
136 75004 [iot_thread] [INFO ][Shadow][75004] Shadow library successfully initialized.
137 76179 [iot_thread] [INFO ][WB_GW][76179] Shadow Thing Name is WB_bulk_test_RSA1 (length 17).
138 77980 [iot_thread] [INFO ][MQTT][77980] Establishing new MQTT connection.
139 77987 [iot_thread] [INFO ][MQTT][77987] Anonymous metrics (SDK language, SDK version) will be provided to AWS IoT. Recompile with AWS_IOT_MQTT_ENABLE_METRI140 78003 [iot_thread] [INFO ][MQTT][78001] (MQTT connection 0x10006ac8, CONNECT operation 0x10006958) Waiting for operation completion.
141 78031 [Tmr Svc] Room 1: power - 1, SetPoint - 24, Fan - 3, AmbientTemp - 21
142 78038 [Tmr Svc] Room 2: power - 1, SetPoint - 23, Fan - 1, AmbientTemp - 21
143 78227 [iot_thread] [INFO ][MQTT][78226] (MQTT connection 0x10006ac8, CONNECT operation 0x10006958) Wait complete with result SUCCESS.
144 78239 [iot_thread] [INFO ][MQTT][78239] New MQTT connection 0x200111e8 established.
145 78247 [iot_thread] [INFO ][Shadow][78247] (WB_bulk_test_RSA1) Modifying Shadow DELTA callback.
146 78256 [iot_thread] [INFO ][Shadow][78256] (WB_bulk_test_RSA1) Adding new DELTA callback.
147 78265 [iot_thread] [INFO ][MQTT][78265] (MQTT connection 0x10006ac8) SUBSCRIBE operation scheduled.
148 78275 [iot_thread] [INFO ][MQTT][78275] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006be0) Waiting for operation completion.
149 78471 [iot_thread] [INFO ][MQTT][78471] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006be0) Wait complete with result SUCCESS.
150 78483 [iot_thread] [INFO ][Shadow][78483] (WB_bulk_test_RSA1) Shadow DELTA callback operation complete with result SUCCESS.
151 78495 [iot_thread] [INFO ][Shadow][78495] (WB_bulk_test_RSA1) Modifying Shadow UPDATED callback.
152 78504 [iot_thread] [INFO ][Shadow][78504] (WB_bulk_test_RSA1) Adding new UPDATED callback.
153 78513 [iot_thread] [INFO ][MQTT][78513] (MQTT connection 0x10006ac8) SUBSCRIBE operation scheduled.
154 78523 [iot_thread] [INFO ][MQTT][78523] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006be0) Waiting for operation completion.
155 78711 [iot_thread] [INFO ][MQTT][78711] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006be0) Wait complete with result SUCCESS.
156 78724 [iot_thread] [INFO ][Shadow][78724] (WB_bulk_test_RSA1) Shadow UPDATED callback operation complete with result SUCCESS.
157 78736 [iot_thread] [INFO ][MQTT][78736] (MQTT connection 0x10006ac8) SUBSCRIBE operation scheduled.
158 78746 [iot_thread] [INFO ][MQTT][78746] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Waiting for operation completion.
159 78941 [iot_thread] [INFO ][MQTT][78940] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Wait complete with result SUCCESS.
160 78955 [iot_thread] [INFO ][MQTT][78953] (MQTT connection 0x10006ac8) SUBSCRIBE operation scheduled.
161 78965 [iot_thread] [INFO ][MQTT][78965] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Waiting for operation completion.
162 79152 [iot_thread] [INFO ][MQTT][79151] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Wait complete with result SUCCESS.
163 79164 [iot_thread] [INFO ][MQTT][79164] (MQTT connection 0x10006ac8) MQTT PUBLISH operation queued.
164 79388 [iot_thread] [INFO ][Shadow][79388] Shadow DELETE of WB_bulk_test_RSA1 was ACCEPTED.
165 79397 [iot_thread] [INFO ][MQTT][79397] (MQTT connection 0x10006ac8) UNSUBSCRIBE operation scheduled.
166 79408 [iot_thread] [INFO ][MQTT][79408] (MQTT connection 0x10006ac8, UNSUBSCRIBE operation 0x10006de0) Waiting for operation completion.
167 79722 [iot_thread] [INFO ][MQTT][79721] (MQTT connection 0x10006ac8, UNSUBSCRIBE operation 0x10006de0) Wait complete with result SUCCESS.
168 79736 [iot_thread] [INFO ][MQTT][79736] (MQTT connection 0x10006ac8) UNSUBSCRIBE operation scheduled.
169 79746 [iot_thread] [INFO ][MQTT][79746] (MQTT connection 0x10006ac8, UNSUBSCRIBE operation 0x10006de0) Waiting for operation completion.
170 79926 [iot_thread] [INFO ][MQTT][79925] (MQTT connection 0x10006ac8, UNSUBSCRIBE operation 0x10006de0) Wait complete with result SUCCESS.
171 79938 [iot_thread] [INFO ][WB_GW][79938] Successfully cleared Shadow of WB_bulk_test_RSA1.
172 79948 [iot_thread] [INFO ][MQTT][79948] (MQTT connection 0x10006ac8) SUBSCRIBE operation scheduled.
173 79958 [iot_thread] [INFO ][MQTT][79958] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Waiting for operation completion.
174 80153 [iot_thread] [INFO ][MQTT][80152] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Wait complete with result SUCCESS.
175 80165 [iot_thread] [INFO ][MQTT][80165] (MQTT connection 0x10006ac8) SUBSCRIBE operation scheduled.
176 80177 [iot_thread] [INFO ][MQTT][80177] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Waiting for operation completion.
177 80356 [iot_thread] [INFO ][MQTT][80355] (MQTT connection 0x10006ac8, SUBSCRIBE operation 0x10006de0) Wait complete with result SUCCESS.
178 80368 [iot_thread] [INFO ][MQTT][80368] (MQTT connection 0x10006ac8) MQTT PUBLISH operation queued.
Up until here everything looks fine, now I try this again but now, after the message received in the main task I get an error message from IotMqtt_Connect :
228 174527 [iot_thread] [ERROR][NET][174527] Failed to establish new connection.
I debug this and found that this is caused in a call to IotMqtt_Connect in this section:
/* Create a new MQTT connection if requested. Otherwise, copy the existing
* network connection. */
if( pNetworkInfo->createNetworkConnection == true )
{
networkStatus = pNetworkInfo->pNetworkInterface->create( pNetworkInfo->u.setup.pNetworkServerInfo,
pNetworkInfo->u.setup.pNetworkCredentialInfo,
&pNetworkConnection );
if( networkStatus == IOT_NETWORK_SUCCESS )
{
networkCreated = true;
/* This MQTT connection owns the network connection it created and
* should destroy it on cleanup. */
ownNetworkConnection = true;
}
else
{
*IOT_SET_AND_GOTO_CLEANUP( IOT_MQTT_NETWORK_ERROR );*
}
}
else
The error I get is a IOT_MQTT_NETWORK_ERROR
I hope this description is clear,
Thanks