This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3200: Wifi Reconnection Issue

Part Number: CC3200
Other Parts Discussed in Thread: , CC3100

Dear team,

Below are the setup details I'm using: 

  1. CC3200 Wi-Fi chip.
  2. SDK: CC3200SDK_1.3.0
  3. Service Pack: CC3100_CC3200_ServicePack_1.0.1.11-2.10.0.0

Aim: Collecting the Vibration data and publishing it on the MQTT server.

  • Vibration sensor (SPI protocol)
  • WPA2 Wi-Fi security

Reference Example code:

  • I'm using the MQTT client code, and made the changes according to the requirements

Issue: Reconnection to the same network

  • The device connects to the network and starts to send data only for few minutes (10/20 minutes)
  • Later its disconnects and tries to reconnect, but it never reconnects
  • If we turn off the "ROUTER" before 10 minutes the device reconnects to the network
  • In the same way If we turn off the "ROUTER" after10/20 minutes the device never reconnects to the network 
  • It need to be turn OFF and turn ON again to connect the same network

What may be the issue for this?, I would appreciate your efforts in getting in touch with the above issue as early as possible.

Below is the code snippet of the MQTT client code I have used,  I request you to  please provide with a proper solution? 

//*****************************************************************************
//
//! Task implementing MQTT client communication to other web client through
//!    a broker
//!
//! \param  none
//!
//! This function
//!    1. Initializes network driver and connects to the default AP
//!    2. Initializes the mqtt library and set up MQTT connection configurations
//!    3. set up the button events and their callbacks(for publishing)
//!    4. handles the callback signals
//!
//! \return None
//!
//*****************************************************************************

void MqttClient(void *pvParameters)
{
	long lRetVal = -1;
	int iCount = 0;
	int iNumBroker = 0;
	int iConnBroker = 0;
	unsigned char policyVal;

//	connect_config *local_con_conf = (connect_config *)app_hndl;
	local_con_conf = (connect_config *)app_hndl;

	// Reset The state of the machine
	Network_IF_ResetMCUStateMachine();

	// Start the driver
	lRetVal = Network_IF_InitDriver(ROLE_STA);
	if(lRetVal < 0)
	{
		LOOP_FOREVER();
	}

	// switch on Yellow LED to indicate Simplelink is properly up
	MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON


	// Start Timer to blink Red LED till AP connection
	LedTimerConfigNStart();

	// Initialize AP security params
	SecurityParams.Key = (signed char *)SECURITY_KEY;
	SecurityParams.KeyLen = strlen(SECURITY_KEY);
	SecurityParams.Type = SECURITY_TYPE;

	//
	// Connect to the Access Point
	//
	lRetVal = Network_IF_ConnectAP(SSID_NAME, SecurityParams);
	if(lRetVal < 0)
	{
		UART_PRINT("Connection to an AP failed\n\r");
		LOOP_FOREVER();
	}

	lRetVal = sl_WlanProfileAdd(SSID_NAME,strlen(SSID_NAME),0,&SecurityParams,0,1,0);



	//set AUTO policy
	lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,
			SL_CONNECTION_POLICY(1,1,0,0,0),
			&policyVal, 1 /*PolicyValLen*/);		//SL_CONNECTION_POLICY is set to auto and fast config connection mode.

	g_uiSimplelinkRole =  sl_Start(NULL,NULL,NULL);

	//waiting for the device to Auto Connect
	while ( (!IS_IP_ACQUIRED(g_ulStatus))&&
			g_ucConnectTimeout < AUTO_CONNECTION_TIMEOUT_COUNT)
	{
		//Turn RED LED On
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
		osi_Sleep(50);

		//Turn RED LED Off
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		osi_Sleep(50);

		g_ucConnectTimeout++;
	}


	// Disable the LED blinking Timer as Device is connected to AP
	LedTimerDeinitStop();

	// Switch ON RED LED to indicate that Device acquired an IP
	MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
	UtilsDelay(20000000);
	MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF

	// Register Push Button Handlers
	Button_IF_Init(pushButtonInterruptHandler2,pushButtonInterruptHandler3);
	ReconnectNow:
	if(boolRestart)
	{
//		sl_ExtLib_MqttClientDisconnect((void*)local_con_conf[iCount].clt_ctx);
		/* Clear all stored profiles and reset the policies */
		lRetVal = sl_WlanProfileDel(0xFF);
//		ASSERT_ON_ERROR(lRetVal);
		sl_WlanDisconnect();


		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
		UtilsDelay(100000);
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		UtilsDelay(100000);
		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON

		// Start Timer to blink Red LED till AP connection
		LedTimerConfigNStart();
		SetConnectionPolicy();
		// Disable the LED blinking Timer as Device is connected to AP
		LedTimerDeinitStop();

		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		boolRestart = false;
	}

	connect_to_broker:
	// Initialze MQTT client lib
	lRetVal = sl_ExtLib_MqttClientInit(&Mqtt_Client);
	if(lRetVal != 0)
	{
		// lib initialization failed
		LOOP_FOREVER();
	}

	/******************* connection to the broker ***************************/
	iNumBroker = sizeof(usr_connect_config)/sizeof(connect_config);
	if(iNumBroker > MAX_BROKER_CONN)
	{
		LOOP_FOREVER();
	}

//	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)
		{

			//delete the context for this connection
			sl_ExtLib_MqttClientCtxDelete(local_con_conf[iCount].clt_ctx);

			break;
		}
		else
		{
			local_con_conf[iCount].is_connected = true;
			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)
		{
			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;
			for(iSub = 0; iSub < local_con_conf[iCount].num_topics; iSub++)
			{
			}
		}
		iCount++;
	}


	if(iConnBroker < 1)
	{
		// no succesful connection to broker
		goto end;
	}

	iCount = 0;

	for(;;)
	{
		if(boolSmartConfig)             //Conditional entry check for Force Smart Configuration
		{
			boolRestart=true;
			boolSmartConfig = false;
			goto ReconnectNow;
		}

		//Check If AP is disconnected
		if(boolBrokerDisconnected)
		{
			//Jumps to Reconnection Wait state
			goto end;
		}

		ReadData();  //Read the vibration Sensor data



#ifdef RS485_SERIAL
		sprintf(dataBuffer,"{\"Vibration,VID\": \"%s\", \"x\": \"%s\", \"y\": \"%s\", \"z\": \"%s\" }\n\r",charDeviceID,ucXaxisBuffer,ucYaxisBuffer,ucZaxisBuffer);
#endif

		sl_ExtLib_MqttClientSend((void*)local_con_conf[iCount].clt_ctx,      //MQTT Send Function
				pub_topic,(char *)dataBuffer,strlen(dataBuffer),QOS2,RETAIN);

		osi_Sleep(50);			//50ms delay
	}

	end:
		lRetVal = CheckInternetConnection();
		//Wait Untill AP/Internet connection is established
		while(lRetVal < 0)
		{

			IS_IP_ACQUIRED(g_ulStatus);
			IS_CONNECTED(g_ulStatus);
			lRetVal = CheckInternetConnection();

			MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, GPIO_PIN_4);	//Yellow LED ON
			osi_Sleep(12000);
			MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
			osi_Sleep(3000);

			cReconnectCntr++;		//Count no of attempts of reconnection

			if(boolSmartConfig)
			{
				lRetVal = 1;
				lRetVal = sl_WlanProfileDel(0xFF);
				sl_WlanDisconnect();
				boolRestart=true;
				boolSmartConfig = false;
				goto ReconnectNow;
			}

			if( cReconnectCntr > 8 )		// 1 count = 15 secs therefore 4 count = 1 mins
			{
				cReconnectCntr = 0;
				PowerCycleDevice();	// soft resets the device - data is lost
			}

		}

		MAP_GPIOPinWrite(GPIOA3_BASE, GPIO_PIN_4, 0);	//Yellow LED OFF
		local_con_conf->is_connected = true;
		boolBrokerDisconnected = false;
		//If AP connection is sucessfull jump to reintiate MQTT broker Connection
//		PowerCycleDevice();
		goto connect_to_broker;
}

Let me know if I'm wrong. Thanks in advance.

    

Looking for a quick response.

Thanks and Regards,

Shivaprasad.

  • Can you tell the reason for the disconnection?(reason code in the disconnection event). are you getting disconnected because the router is turned off?

    Are you using auto-connect/fast-connect for the connection policy?

  • I appreciate you for looking into this issue at the earliest.

    are you getting disconnected because the router is turned off?

    -> Yes, as I'm creating a scenario for testing the reconnection to the same network. Example: If there is no network then the device should go into reconnection state and wait there until the network is UP.  Please go through the below steps for understanding the testing scenario.

    • I connect the device to the network, the device is connected and the data is being published.
    • After sometime I turn off the network (router).
    • Now the device goes into the reconnection state and waits to reconnect to the same network.
    • If I turn ON the router within 10/20 minutes, the device gets connected to the network  and data is published.
    • But if I turn ON the router after 20 minutes or so the device does not reconnect to the network at all. I need to restart the device every time to make it connect to the network.

    Are you using auto-connect/fast-connect for the connection policy?

    -> Yes. I'm using the combination of both as you can see below,

    lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1,1,0,0,0), &policyVal, 1 /*PolicyValLen*/);

     

    Kindly let me know if I'm missing out on anything. 

    Thank you for your time.

    Thanks and Regards,

    Shivaprasad.

  • Hi,

    Generally, the periodic scan is telescopic, i.e. when the AP cannot be found, it triggers scan in increasing interval until it gets to 14 seconds gap between scans and keeps this way forever.

    I tested locally and it goes this way for 30 minutes until I stopped it. If I leave it as is for 30 minutes and then plug in the AP, it reconnects as expected.

    I am using the latest SP whereas you use a much older one 2.10.

    can you please upgrade for the test and try it out?

    you can find it here https://www.ti.com/tool/download/CC3200SDK

    Shlomi