微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 无线和射频 > TI WIFI设计交流 > WiFi断线后如何重连?能不能自动重连?

WiFi断线后如何重连?能不能自动重连?

时间:10-02 整理:3721RD 点击:

我用wlan_station例程,连上小米路由器后,通信都没有问题。主动把路由器断电,3200会收到disconnect消息,但是路由器上电后3200不会自动重连。

请教下这种断线情况该怎么处理来重连?能够配制成自动重连么?谢谢!

请参考本帖: https://e2e.ti.com/support/wireless_connectivity/simplelink_wifi_cc31xx_cc32xx/f/968/p/449742/1623542

建议参考一下SDK中的例子cc3200-sdk\example\connection_policy

关于详细的测试备注如下:

static long SetConnectionPolicy()
{
unsigned char policyVal;
long lRetVal = -1;

/* Clear all stored profiles and reset the policies */
lRetVal = sl_WlanProfileDel(0xFF);
ASSERT_ON_ERROR(lRetVal);

lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), 0, 0);
ASSERT_ON_ERROR(lRetVal);

//----------------------------------------------(1)通过增加profile方式连接AP-OK-----------------------------------------------
//Add Profile
/* user needs to change SSID_NAME = "<Secured AP>"
SECURITY_TYPE = SL_SEC_TYPE_WPA
SECURITY_KEY = "<password>"
and set the priority as per requirement to connect with a secured AP */
SlSecParams_t secParams;
secParams.Key = SECURITY_KEY;
secParams.KeyLen = strlen(SECURITY_KEY);
secParams.Type = SECURITY_TYPE;
lRetVal = sl_WlanProfileAdd(SSID_NAME,strlen(SSID_NAME),0,&secParams,0,1,0);
ASSERT_ON_ERROR(lRetVal);

//set AUTO policy automatically reconnect to one of its stored profiles 自动模式下CC3200会从SPI_Flash中读取存储的profile配置AP信息并连接AP
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,0),&policyVal, 1 /*PolicyValLen*/);
ASSERT_ON_ERROR(lRetVal);

//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
//
// ****** Put breakpoint here ******
// If control comes here- means device connected to AP in auto mode
//
//注意并未采用主动连接的方式:lRetVal = sl_WlanConnect((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
//-------------------------------------------------------------------------------------------------------------------------

// Disconnect from AP
lRetVal = sl_WlanDisconnect();
if(0 == lRetVal)
{
// Wait
while(IS_CONNECTED(g_ulStatus))
{
#ifndef SL_PLATFORM_MULTI_THREADED
_SlNonOsMainLoopTask();
#endif
}
}

//----------------------------------------------(2)通过SmartConfig方式连接AP-OK-----------------------------------------------
//delete profiles
lRetVal = sl_WlanProfileDel(0xFF);
ASSERT_ON_ERROR(lRetVal);

//set Auto SmartConfig policy
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,1),&policyVal,0);
ASSERT_ON_ERROR(lRetVal);

// reset the NWP
lRetVal = ResetNwp(); //重启NWP,相当于网络断掉
ASSERT_ON_ERROR(lRetVal);

// wait for smart config to complete and device to connect to an AP
//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask(); //查看NWP返回的消息!---在这处理消息来处理 SimpleLinkGeneralEventHandler()/SimpleLinkWlanEventHandler/..等回调函数!
}
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);
//
// ****** Put breakpoint here ******
// If control comes here- means device connected to AP in smartconfig mode
//
//备注:配置完sl_WlanPolicySet()后并未调用lRetVal = sl_WlanSmartConfigStart()但是通过手机Smart Starter进行SmartConfig也能配置CC3200连接AP!---而在provisioning_smartconfig例程中是调用了的!
//-------------------------------------------------------------------------------------------------------------------------

//----------------------------------------------(3)Fast方式连接AP-OK-----------------------------------------------
/* Delete all profiles (0xFF) stored */
lRetVal = sl_WlanProfileDel(0xFF);
ASSERT_ON_ERROR(lRetVal);

// reset the NWP
lRetVal = ResetNwp(); //重启NWP,相当于网络断掉
ASSERT_ON_ERROR(lRetVal);

/* Set connection policy to Fast, Device will connect to last connected AP.
* This feature can be used to reconnect to AP */
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0); //Fast模式特点可以用在重连AP--YES 测试OK
ASSERT_ON_ERROR(lRetVal);

/* Connect to the open AP */
// lRetVal = sl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, 0, 0);
//------------------------增加-----------------------------

secParams.Key = (signed char*)SECURITY_KEY;
secParams.KeyLen = strlen(SECURITY_KEY);
secParams.Type = SECURITY_TYPE;
lRetVal = sl_WlanConnect((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0);
//---------------------------------------------------------
ASSERT_ON_ERROR(lRetVal);

//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION);
CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED);

// // Add unsecured AP profile with priority 6 (7 is highest) ---之前的SDK不支持这种模式,必须要增加Profile形式?现在可以删除这个?===Test-OK,可以删除!
// // Because of a limitation in SDK-0.5 which prevents the automatic addition
// // of the profile (fast), the application is required to explicitly add it.
// // The limitation shall be addressed in subsequent SDK release, and the below
// // lines for adding the profile can be removed then...!
// secParams.Key = "";
// secParams.KeyLen = 0;
// secParams.Type = SL_SEC_TYPE_OPEN;
// lRetVal = sl_WlanProfileAdd((signed char*)SSID_NAME,strlen(SSID_NAME), 0, &secParams, 0, 6, 0);
// ASSERT_ON_ERROR(lRetVal);

// reset the NWP
lRetVal = ResetNwp(); //重启NWP,相当于网络断掉,注意此时并未保存profile,CC3200选择最近联网的AP中进行连接!
ASSERT_ON_ERROR(lRetVal);

/* Wait for the connection to be established */
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}
//
// ****** Put breakpoint here ******
// If control comes here- means device connected to AP in FAST mode
//
//-------------------------------------------------------------------------------------------------------------------------

//remove all policies
lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(0,0,0,0,0),&policyVal,0);
ASSERT_ON_ERROR(lRetVal);

return SUCCESS;

}

补充一个UDP测试的AP断开重连的程序

if(Flag_SL_WLAN_DISCONNECT_EVENT==1) //每次AP断开后都会进入到SimpleLinkWlanEventHandler()函数中,产生SL_WLAN_DISCONNECT_EVENT事件---测试OK
{
unsigned char policyVal;
UART_PRINT("AP Disconnect Try Reconnect\n\r");
// lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0); //通过Fast模式无法重连AP,
// (1)
// ResetNwp(); //可能是在通信过程中突然将AP关闭后,CC3200并未保存AP的信息,导致即使重启NWP也无法连接AP现象,即使重启好了AP也不行
// 使能profile重启NWP也无法连接AP,而例程中的方法是在AP连接的状态下将NWP重启,是可以连接AP的,可能在NWP重启sl_stop时保存了AP信息!
// WlanConnect(); //通过AP断开后启动OK,自己重连的方式测试OK---每次AP断开后都可以进入到SimpleLinkWlanEventHandler()中断函数中,
// (2) 可以自动重连
// lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,0),&policyVal, 1 /*PolicyValLen*/);
// ASSERT_ON_ERROR(lRetVal);
// ResetNwp();
// (3) 在(2)基础上什么也不用做,直接等待,CC3200会自动重连的!然后连接建立UDP开始发送数据
//wait until IP is acquired
while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))
{
_SlNonOsMainLoopTask();
}

信息很详细,非常感谢!

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top