ESP8266 station模式下建立client、server TCP连接

程序实现内容:

1.在station模式下,ESP8266作为client、server进行TCP连接
2.实现数据的发送、接收(同时回传)
实现思路:
TCP网络通信分层为:应用层、网络层、数据链路层、物理层;
1. 设置ESP8266为station模式,在数据链路层连接AP,获取IP地址;
2. 在网络层进行TCP连接:作为client连接远程server,作为server监听远程client信息;

数据链路层:

1. 设置ESP8266为station模式:wifi_set_opmode(STATION_MODE);

2. 配置连接到AP的相关参数,在该步骤中,需要知道AP的名称( ssid )、密码( password ): 官方所给的SDK中,该配置执行后,wifi模块自动与AP进行连接

 void user_set_station_config(){

   struct station_config stationconf;

   char ssid[]     = "Hello world";
char password[] = "chenyuping"; stationconf.bssid_set = ;
memset(stationconf.ssid, , sizeof(stationconf.ssid));
memset(stationconf.password, , sizeof(stationconf.password)); memcpy(stationconf.ssid, ssid, sizeof(ssid));
memcpy(stationconf.password, password, sizeof(password)); wifi_station_set_config(&stationconf); return ;
}

为确保ESP8266已同AP建立稳定的连接并且已获取到IP地址,可注册该事件的回调函数进行判定:
1.注册回调函数

 wifi_set_event_handler_cb(wifi_handle_even_cb);

 void wifi_handle_even_cb(System_Event_t *evt){
switch(evt->event_id){
case EVENT_STAMODE_GOT_IP:
flag_sta_conip = TRUE;
printf("Have got IP!!\n");
break;
defaut: printf("Have'n got IP\n");
break;
}
}

2.注册定时器,对回调函数中的flag_sta_conip进行判定以确定是否已获得IP地址

   os_timer_disarm(&ser_timer);
os_timer_setfn(&ser_timer, espconn_ser_timer_cb, NULL);
os_timer_arm(&ser_timer, , ); os_timer_disarm(&cli_timer);
os_timer_setfn(&cli_timer, espconn_cli_timer_cb, NULL);
os_timer_arm(&cli_timer, , );

网络层:

1. 检测ESP8266是否已同AP建立稳定连接,若是,则开始建立TCP client连接:

 void espconn_cli_timer_cb(void *timer_arg)
{
struct ip_info ipconfig; os_timer_disarm(&cli_timer);
wifi_get_ip_info(STATION_IF, &ipconfig); if (flag_sta_conip){
printf("connect successful !!!\n");
espconn_tcp_client_connect();
}
else {
printf("connect fail !!!\n");
os_timer_setfn(&cli_timer, espconn_cli_timer_cb, NULL);
os_timer_arm(&cli_timer, , );
}
}

建立TCP client连接(在该设置中,需要知道远程server的IP地址及端口号),同时注册连接、收发数据的回调函数:

 void espconn_tcp_client_connect(){

   user_tcp_conn.proto.tcp = &user_tcp;
user_tcp_conn.type = ESPCONN_TCP;
user_tcp_conn.state = ESPCONN_NONE; const char esp_tcp_server_ip[] = {, , , }; // remote IP of TCP server memcpy(user_tcp_conn.proto.tcp->remote_ip, esp_tcp_server_ip, );
user_tcp_conn.proto.tcp->remote_port = ; espconn_regist_connectcb(&user_tcp_conn, espconn_connect_cb);
espconn_regist_reconcb(&user_tcp_conn, espconn_reconnect_cb); espconn_regist_sentcb(&user_tcp_conn, espconn_sent_cb);
espconn_regist_recvcb(&user_tcp_conn, espconn_recv_data_cb); espconn_connect(&user_tcp_conn); }

2. ESP8266开启server服务(在该步骤中,通过对espconn_accept()函数返回值的判定,TRUE则关闭设置server开启的定时器,FALSE则继续保持开通,直到开启成功)

 void espconn_ser_timer_cb(void *timer_arg)
{
user_tcp_conn.proto.tcp = &user_tcp;
user_tcp_conn.type = ESPCONN_TCP;
user_tcp_conn.state = ESPCONN_NONE; os_timer_disarm(&ser_timer); user_tcp_conn.proto.tcp->local_port = espconn_port();
printf("The local port is %d\n",user_tcp_conn.proto.tcp->local_port); sint8 ret = espconn_accept(&user_tcp_conn);
printf("%d\n", ret);
if(!ret){
printf("Begin to listen!!!\n");
}
else{
printf("Fail to listen!!!\n");
os_timer_setfn(&ser_timer, espconn_ser_timer_cb, NULL);
os_timer_arm(&ser_timer, , );
}
}

接收数据的回调函数:

 espconn_regist_recvcb(&user_tcp_conn, espconn_recv_data_cb);

 void espconn_recv_data_cb(void *arg, char *pdata, unsigned short len)
{
uint8 *pDat;
const char str[] = "Light ON"; pDat = (uint8 *)malloc(len + );
memcpy(pDat, pdata, len);
*(pDat+len) = ;
// pDat[len] = 0; printf("The receiver data is %s",pDat);
printf("\n\n"); if(memcmp(pDat, str, sizeof(str)) == ) {
printf("Now Light is Run!\n");
espconn_send(&user_tcp_conn, "Now Light is Run!\n", );
} free(pDat);
}

发送数据的回调函数:

 espconn_regist_sentcb(&user_tcp_conn, espconn_sent_cb);

 void espconn_ser_timer_cb(void *timer_arg)
{
user_tcp_conn.proto.tcp = &user_tcp;
user_tcp_conn.type = ESPCONN_TCP;
user_tcp_conn.state = ESPCONN_NONE; os_timer_disarm(&ser_timer);
user_tcp_conn.proto.tcp->local_port = espconn_port();
printf("The local port is %d\n",user_tcp_conn.proto.tcp->local_port); sint8 ret = espconn_accept(&user_tcp_conn);
printf("%d\n", ret);
if(!ret){
printf("Begin to listen!!!\n");
}
else{
printf("Fail to listen!!!\n");
os_timer_setfn(&ser_timer, espconn_ser_timer_cb, NULL);
os_timer_arm(&ser_timer, , );
}
}

实现效果:
ESP8266 station模式下建立client、server TCP连接

ESP8266 station模式下建立client、server TCP连接

上一篇:EM算法(3):EM算法运用


下一篇:onvif协议client与server对接