一、编译烧录
二、实验现象
程序功能:接收tcp服务器发来的信息,串口输出;同时串口发送,回传
三、源码分析
1.程序入口
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_init(void)
{
os_printf("SDK version:%s\n", system_get_sdk_version());
os_printf("hello_ai\n");
tcpuser_init();
}
2.设置为station+ap模式,设置路由器参数连接
/******************************************************************************
* FunctionName : user_init
* Description : entry of user application, init user function here
* Parameters : none
* Returns : none
*******************************************************************************/
void tcpuser_init(void)
{
//Set softAP + station mode
wifi_set_opmode(STATIONAP_MODE);
//ESP8266 connect to router
user_set_station_config();
}
3. Set timer to check whether router allotted an IP
/******************************************************************************
* FunctionName : user_set_station_config
* Description : set the router info which ESP8266 station will connect to
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_set_station_config(void)
{
// Wifi configuration
char ssid[32] = SSID;
char password[64] = PASSWORD;
struct station_config stationConf;
os_memset(stationConf.ssid, 0, 32);
os_memset(stationConf.password, 0, 64);
// No MAC-specific scanning
stationConf.bssid_set = 0;
//Set ap settings
os_memcpy(&stationConf.ssid, ssid, 32);
os_memcpy(&stationConf.password, password, 64);
wifi_station_set_config(&stationConf);
// Set timer to check whether router allotted an IP
os_timer_disarm(&test_timer);
os_timer_setfn(&test_timer, (os_timer_func_t *)user_check_ip, NULL);
os_timer_arm(&test_timer, 100, 0);
}
4.user_check_ip()函数中,以tcp客户端的方式连接tcp服务器
/******************************************************************************
* FunctionName : user_check_ip
* Description : check whether get ip addr or not
* Parameters : none
* Returns : none
*******************************************************************************/
void ICACHE_FLASH_ATTR
user_check_ip(void)
{
struct ip_info ipconfig;
//disarm timer first
os_timer_disarm(&test_timer);
//get ip info of ESP8266 station
wifi_get_ip_info(STATION_IF, &ipconfig);
if (wifi_station_get_connect_status() == STATION_GOT_IP && ipconfig.ip.addr != 0)
{
os_printf("Connected to router and assigned IP!\r\n");
// Connect to tcp server as NET_DOMAIN
user_tcp_conn.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp));
user_tcp_conn.type = ESPCONN_TCP;
user_tcp_conn.state = ESPCONN_NONE;
#ifdef DNS_ENABLE
tcp_server_ip.addr = 0;
espconn_gethostbyname(&user_tcp_conn, NET_DOMAIN, &tcp_server_ip, user_dns_found); // DNS function
os_timer_setfn(&test_timer, (os_timer_func_t *)user_dns_check_cb, &user_tcp_conn);
os_timer_arm(&test_timer, 1000, 0);
#else
const char esp_tcp_server_ip[4] = {192,168,1,2}; // remote IP of TCP server
os_memcpy(user_tcp_conn.proto.tcp->remote_ip,esp_tcp_server_ip,4);
user_tcp_conn.proto.tcp->remote_port =8234; // remote port
user_tcp_conn.proto.tcp->local_port = espconn_port(); //local port of ESP8266
espconn_regist_connectcb(&user_tcp_conn, user_tcp_connect_cb); // register connect callback
espconn_regist_reconcb(&user_tcp_conn, user_tcp_recon_cb); // register reconnect callback as error handler
espconn_connect(&user_tcp_conn);
#endif
}
else
{
if ((wifi_station_get_connect_status() == STATION_WRONG_PASSWORD ||
wifi_station_get_connect_status() == STATION_NO_AP_FOUND ||
wifi_station_get_connect_status() == STATION_CONNECT_FAIL))
{
os_printf("Connection to router failed!\r\n");
}
else
{
//re-arm timer to check ip
os_timer_setfn(&test_timer, (os_timer_func_t *)user_check_ip, NULL);