ESP8266 sends TCP packets continuously

Sample code below is based on ESP8266_NONOS_SDK.
Note: for ESP8266_RTOS_SDK, please call espconn_init() in the user_init to initialize espconn first.
We suggest to call the next espconn_send in espconn_sent_callback which means that the previous packet is sent.
Or users could enable the espconn_write_finish_callback which means the data sending by espconn_send was written into the write-buffer or already sent, and call the next espconn_send in espconn_write_finish_callback.
The write-buffer can store 8 packets at most, waiting for the TCP ACK. The size of write-buffer is 2920 bytes.
Introduce an API here:
Function: Set option of TCP connection
Prototype:
sint8 espconn_set_opt(
struct espconn *espconn,
uint8 opt
)
Parameter:
struct espconn *espconn : corresponding connected control structure
uint8 opt : Option of TCP connection
bit 0: 1: free memory after TCP disconnection happen need not wait 2 minutes;
bit 1: 1: disable nalgo algorithm during TCP data transmission, quiken the data transmission.
bit 2: 1: enable espconn_write_finish_callback.
bit 3: 1: enable TCP keep-alive function
Return:
0 : succeed
Non-0 : error (please refer to espconn.h for details.)
e.g. ESPCONN_ARG: illegal argument,can’t find TCP connection according to structure espconn
Note:
In general, we need not call this API;
If call espconn_set_opt, please call it in TCP connected callback.
Sample code of send TCP data continually is as bellow
1. call espconn_set_opt to enable write buffer;
2. register a write finish callback;
3. send next packet in write finish callback.

* FunctionName : user_tcp_recv_cb
 * Description  : receive callback.
 * Parameters   : arg -- Additional argument to pass to the callback function
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_tcp_recv_cb(void *arg, char *pusrdata, unsigned short length)
{
   //received some data from tcp connection

    os_printf("tcp recv !!! %s \r\n", pusrdata);

}
/******************************************************************************
 * FunctionName : user_tcp_sent_cb
 * Description  : data sent callback.
 * Parameters   : arg -- Additional argument to pass to the callback function
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_tcp_sent_cb(void *arg)
{
   //data sent successfully

}
/******************************************************************************
 * FunctionName : user_tcp_discon_cb
 * Description  : disconnect callback.
 * Parameters   : arg -- Additional argument to pass to the callback function
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_tcp_discon_cb(void *arg)
{
   //tcp disconnect successfully

    os_printf("tcp disconnect succeed !!! \r\n");
}

/******************************************************************************
 * FunctionName : user_tcp_write_finish
 * Description  : Data need to be sent by espconn_sent has been written into write buffer successfully,
                         call espconn_sent to send next packet is allowed.
 * Parameters   : pespconn -- the espconn used to connetion with the host
 * Returns      : none
*******************************************************************************/

LOCAL void ICACHE_FLASH_ATTR
user_tcp_write_finish(void *arg)
{
    struct espconn *pespconn = arg;

    espconn_sent(pespconn, "Hello World!", 12);
}

/******************************************************************************
 * FunctionName : user_sent_data
 * Description  : Processing the application data and sending it to the host
 * Parameters   : pespconn -- the espconn used to connetion with the host
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_sent_data(struct espconn *pespconn)
{

   espconn_sent(pespconn, "Hello World!", 12);

}

/******************************************************************************
 * FunctionName : user_tcp_connect_cb
 * Description  : A new incoming tcp connection has been connected.
 * Parameters   : arg -- Additional argument to pass to the callback function
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_tcp_connect_cb(void *arg)
{
    struct espconn *pespconn = arg;

    os_printf("connect succeed !!! \r\n");

    espconn_regist_recvcb(pespconn, user_tcp_recv_cb);
    espconn_regist_sentcb(pespconn, user_tcp_sent_cb);
    espconn_regist_disconcb(pespconn, user_tcp_discon_cb);

    espconn_set_opt(pespconn, 0x04); // enable write buffer

    espconn_regist_write_finish(pespconn, user_tcp_write_finish); // register write finish callback

    user_sent_data(pespconn);
}

/******************************************************************************
 * FunctionName : user_tcp_recon_cb
 * Description  : reconnect callback, error occured in TCP connection.
 * Parameters   : arg -- Additional argument to pass to the callback function
 * Returns      : none
*******************************************************************************/
LOCAL void ICACHE_FLASH_ATTR
user_tcp_recon_cb(void *arg, sint8 err)
{
   //error occured , tcp connection broke. user can try to reconnect here. 

    os_printf("reconnect callback, error code %d !!! \r\n",err);
}


/******************************************************************************
 * 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("got ip !!! \r\n");

      // Connect to tcp server as NET_DOMAIN
      user_tcp_conn.proto.tcp = &user_tcp;
      user_tcp_conn.type = ESPCONN_TCP;
      user_tcp_conn.state = ESPCONN_NONE;

      const char esp_server_ip[4] = {X, X, X, X}; // remote IP of tcp server

      os_memcpy(user_tcp_conn.proto.tcp->remote_ip, esp_server_ip, 4); // remote ip of tcp server 

      user_tcp_conn.proto.tcp->remote_port = TCP_SERVER_PORT; // remote port of tcp server

      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); // tcp connect


    } 
   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("connect fail !!! \r\n");
        } 
      else
      {
           //re-arm timer to check ip
            os_timer_setfn(&test_timer, (os_timer_func_t *)user_check_ip, NULL);
            os_timer_arm(&test_timer, 100, 0);
        }
    }
}


/******************************************************************************
 * 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; 

   //need not mac address
   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 a timer to check whether got ip from router succeed or not.
   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);

}


/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
void user_init(void)
{
    os_printf("SDK version:%s\n", system_get_sdk_version());

   //Set softAP + station mode 
   wifi_set_opmode(STATIONAP_MODE); 

   //ESP8266 connect to router
   user_set_station_config(); 
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

全职编程-叶逆天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值