浅析ESP8266_NONOS_SDK-2.2.0_tcpclient

本文详细分析了ESP8266_NONOS_SDK-2.2.0版本下TCP客户端的实现过程,包括编译烧录步骤、实验现象和源码解析。程序主要功能是接收TCP服务器信息并串口输出,同时能回传串口接收的数据。源码重点介绍了连接TCP服务器、注册回调函数、接收和发送处理等功能。
摘要由CSDN通过智能技术生成

一、编译烧录

二、实验现象

程序功能:接收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);
        
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值