ESP8266当作TCP服务器,手机应用去连接,手机应用会重复连接,连接前会把之前的连接给断开,但是ESP8266的连接数一直在增加,当达到5个连接后,就再也不能连接通信了,现在的做法是,在ESP8266中把上一个连接断开,代码如下,但是不生效,帮忙看看哪有问题
static struct espconn led_wifi_conn;
static struct espconn *laster_wifi_conn;
static struct espconn *current_wifi_conn;
static esp_tcp led_wifi_tcp;
static uint8 led_tcp_dis;
// 此函数一直没有调用
static void led_wifi_disconnect_callback(void *arg)
{
os_printf("led_wifi_disconnect_callback\n");
}
// 此函数一直没有调用
static void led_wifi_reconnect_callback(void *arg,sint8 err)
{
os_printf("led_wifi_reconnect_callback err = %d\n",err);
}
static void led_wifi_rev_callback(void *arg, char *pdata, unsigned short len)
{
// os_printf("led_wifi_rev_callback led = %d\n",len);
UART1GetData(pdata,len);
}
static void led_wifi_send_callback(void *arg)
{
// os_printf("led_wifi_send_callback\n");
}
static void led_wifi_connect_callback(void *arg)
{
struct espconn *conn;
conn = (struct espconn *)arg;
if ((unsigned int)arg == (unsigned int)&led_wifi_conn)
os_printf("IS the same\n");
else
os_printf("NOT the same\n");
os_printf("led_wifi_connect_callback\n");
os_printf("conn->link_cnt = %d\n",conn->link_cnt);
os_printf("conn->state = %d\n",conn->state);
os_printf("current_wifi_conn = 0x%x\n",(unsigned int)conn);
current_wifi_conn = conn;
led_tcp_dis = 1;
conn->proto.tcp = &led_wifi_tcp;
espconn_regist_disconcb(conn,led_wifi_disconnect_callback);
espconn_regist_reconcb(conn,led_wifi_reconnect_callback);
espconn_regist_recvcb(conn, led_wifi_rev_callback);
espconn_regist_sentcb(conn, led_wifi_send_callback);
}
static void led_wifi_build_tcp(void)
{
// Build TCP server
led_wifi_conn.type = ESPCONN_TCP;
led_wifi_conn.state = ESPCONN_NONE;
led_wifi_conn.proto.tcp = &led_wifi_tcp;
led_wifi_conn.proto.tcp->local_port = WIFI_SERVER_PORT;
espconn_regist_connectcb(&led_wifi_conn,led_wifi_connect_callback);
espconn_accept(&led_wifi_conn);
// Set TCP alive 1800s
espconn_regist_time(&led_wifi_conn,WIFI_TCP_TIME,1);
}
//
// Check if need disconnect TCP connect
// 此函数在一个任务中周期性调用,用来断开上一个连接
void LEDWifiProc(void)
{
sint8 ret;
if (0) {
os_printf("LEDWifiProc = 0x%x\n",(unsigned int)laster_wifi_conn);
ret = espconn_disconnect(laster_wifi_conn);
os_printf("disconnect ret = %d\n",ret);
ret = espconn_delete(laster_wifi_conn);
os_printf("delete ret = %d\n",ret);
laster_wifi_conn = current_wifi_conn;
led_tcp_dis = 0;
}
}
sint8 led_wifi_send_data(uint8 *data,unsigned short len)
{
if (data == NULL || len == 0)
return -1;
return espconn_send(&led_wifi_conn,data,len);
}
void IRAM_ATTR led_wifi_event_handle(System_Event_t *event)
{
os_printf("led_wifi_event_handle evt = %d\n",event->event_id);
}
void led_wifi_init(void)
{
unsigned int ret;
struct softap_config cfg;
led_tcp_dis = 0;
// WIFI mode init
wifi_set_opmode(STATIONAP_MODE);
// Set WIFI default name and password
wifi_softap_get_config(&cfg);
// Load device name save in flash
// When new device,the softAP authmode will OPEN ?
if (cfg.authmode != AUTH_WPA_WPA2_PSK || cfg.max_connection != 1) {
sprintf(cfg.ssid,"%s",WIFI_DEFAULT_SSID);
sprintf(cfg.password,"%s",WIFI_DEFAULT_PASSWD);
}
cfg.authmode = AUTH_WPA_WPA2_PSK;
cfg.ssid_len = 0;
cfg.max_connection = 1;
wifi_softap_set_config(&cfg);
// wifi_set_event_handler_cb(led_wifi_event_handle);
led_wifi_build_tcp();
laster_wifi_conn = &led_wifi_conn;
current_wifi_conn = &led_wifi_conn;
os_printf("led_wifi_conn = 0x%x\n",(unsigned int)&led_wifi_conn);
}
串口打印LOG如下:
SDK version:1.3.0(68c9e7b)
led_wifi_conn = 0x3fff044c
mode : sta(5c:cf:7f:12:65:42) + softAP(5e:cf:7f:12:65:42)
add if0
dhcp server start:(ip:192.168.4.1,mask:255.255.255.0,gw:192.168.4.1)
add if1
bcn 100
add 1
aid 1
station: 68:3e:34:61:d5:61 join, AID = 1
NOT the same
led_wifi_connect_callback
conn->link_cnt = 0
conn->state = 3
current_wifi_conn = 0x3fffde20
espconn_server_close, close
NOT the same
led_wifi_connect_callback
conn->link_cnt = 1
conn->state = 3
current_wifi_conn = 0x3fffdcb0
espconn_server_close, close
NOT the same
led_wifi_connect_callback
conn->link_cnt = 2
conn->state = 3
current_wifi_conn = 0x3fffdd78
espconn_server_close, close
NOT the same
led_wifi_connect_callback
conn->link_cnt = 3
conn->state = 3
current_wifi_conn = 0x3fffdf58
espconn_server_close, close
NOT the same
led_wifi_connect_callback
conn->link_cnt = 4
conn->state = 3
current_wifi_conn = 0x3fffdf28
espconn_server_close, close
当conn->link_cnt = 4后,再也不能连接,不管LEDWifiProc有没有执行disconnect,都没用
THSStatistics: Posted by harryhu — Tue Mar 01, 2016 11:07 pm
]]>