Hi,
I'm trying to reconnect with broker.In my case CC3200 is connected with mobile hotspot (i.e acquired the IP address) and mobile doesn.t have reliable internet connection.
When I disconnected with broker i'm using below commands to reconnect again but its fails -
I changed the code in disconnection event as per my need.Here, whenever its get disconnected from broker its comes to this event and here led turns on to show its doesn't have internet but when its goes to BrokerReconnect() function its not connected with broker again.If it get connected successfully then its led turns off but its not connecting with broker again.
///////////////////////////////////*****************************/////////////////////////////////////
main.c
static void
sl_MqttDisconnect(void *app_hndl)
{
volatile unsigned long g_ulStatus = 0;
connect_config *local_con_conf;
local_con_conf = app_hndl;
int iConnBroker = 0;
UART_PRINT("disconnect from broker %s\r\n",
(local_con_conf->broker_config).server_info.server_addr);
local_con_conf->is_connected = false;
int iCount = 0;
GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); //LP
sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);
iConnBroker--;
BrokerReconnect(); // Created 1 function for re connection
if(iConnBroker < 1)
{
//
// device not connected to any broker
//
BrokerReconnect(); // Created 1 function for re connection
}
}
///////////////////////////////////*****************************/////////////////////////////////////
void BrokerReconnect() ///Broker Reconnection function
{
int iCount = 0;
int iNumBroker = 0;
int iConnBroker = 0;
connect_config *local_con_conf = (connect_config *)app_hndl;
connect_to_broker:
while(iCount < iNumBroker)
{
//create client context
local_con_conf[iCount].clt_ctx =
sl_ExtLib_MqttClientCtxCreate(&local_con_conf[iCount].broker_config,
&local_con_conf[iCount].CallBAcks,
&(local_con_conf[iCount]));
//
// Set Client ID
//
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_CLIENT_ID,
local_con_conf[iCount].client_id,
strlen((char*)(local_con_conf[iCount].client_id)));
//
// Set will Params
//
if(local_con_conf[iCount].will_params.will_topic != NULL)
{
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_WILL_PARAM,
&(local_con_conf[iCount].will_params),
sizeof(SlMqttWill_t));
}
//
// setting username and password
//
if(local_con_conf[iCount].usr_name != NULL)
{
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_USER_NAME,
local_con_conf[iCount].usr_name,
strlen((char*)local_con_conf[iCount].usr_name));
if(local_con_conf[iCount].usr_pwd != NULL)
{
sl_ExtLib_MqttClientSet((void*)local_con_conf[iCount].clt_ctx,
SL_MQTT_PARAM_PASS_WORD,
local_con_conf[iCount].usr_pwd,
strlen((char*)local_con_conf[iCount].usr_pwd));
}
}
//
// connectin to the broker
//
if((sl_ExtLib_MqttClientConnect((void*)local_con_conf[iCount].clt_ctx,
local_con_conf[iCount].is_clean,
local_con_conf[iCount].keep_alive_time) & 0xFF) != 0)
{
UART_PRINT("\n\rBroker connect fail for conn no. %d \n\r",iCount+1);
GPIO_IF_LedOn(MCU_GREEN_LED_GPIO);
//delete the context for this connection
sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);
break;
}
else
{
UART_PRINT("\n\rSuccess: conn to Broker no. %d\n\r ", iCount+1);
local_con_conf[iCount].is_connected = true;
GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);
iConnBroker++;
}
//
// Subscribe to topics
//
if(sl_ExtLib_MqttClientSub((void*)local_con_conf[iCount].clt_ctx,
local_con_conf[iCount].topic,
local_con_conf[iCount].qos, TOPIC_COUNT) < 0)
{
UART_PRINT("\n\r Subscription Error for conn no. %d\n\r", iCount+1);
UART_PRINT("Disconnecting from the broker\r\n");
sl_ExtLib_MqttClientDisconnect(local_con_conf[iCount].clt_ctx);
local_con_conf[iCount].is_connected = false;
//delete the context for this connection
sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);
iConnBroker--;
break;
}
else
{
int iSub;
UART_PRINT("Client subscribed on following topics:\n\r");
for(iSub = 0; iSub < local_con_conf[iCount].num_topics; iSub++)
{
UART_PRINT("%s\n\r", local_con_conf[iCount].topic[iSub]);
}
}
iCount++;
}
if(iConnBroker < 1)
{
//
// no succesful connection to broker
//
goto connect_to_broker;
}
iCount = 0;
GPIO_IF_LedOff(MCU_GREEN_LED_GPIO);
}
Thanks & Regards,
Shashank