espconn reconnect callback

rith87
Posts: 32
Joined: Tue Mar 03, 2015 6:04 pm

espconn reconnect callback

Postby rith87 » Tue Feb 21, 2017 6:25 pm

I'm using the ESP to download a 500 kB file. If my download speed is fast and I download the file in < 17s, everything is good. However, if the ESP download speed is slow, the reconnect callback is called and reports that the connection has been reset. Is there any way to understand why the connection is reset?

rith87
Posts: 32
Joined: Tue Mar 03, 2015 6:04 pm

Re: espconn reconnect callback

Postby rith87 » Thu Feb 23, 2017 6:28 pm

I managed to repro this issue with the following code:

Code: Select all

#define HTTP_HEADER "Connection: keep-alive\r\n\
Cache-Control: no-cache\r\n\
User-Agent: rBoot-Sample/1.0\r\n\
Accept: */*\r\n\r\n"

void ICACHE_FLASH_ATTR user_tcp_recv_cb(void *arg, char *pusrdata, unsigned short length)
{
  os_printf("Block received: %d\n", length);
}

void ICACHE_FLASH_ATTR user_tcp_discon_cb(void *arg)
{
  os_printf("Time [%d]: ", system_get_time());
  os_printf("Disconnect callback\n");
}

void ICACHE_FLASH_ATTR user_tcp_connect_cb(void *arg)
{
  char request[REQUEST_SIZE] = {0};
  os_printf("Connect callback\n");

  espconn *conn = (espconn *) arg;
  espconn_regist_recvcb(conn, user_tcp_recv_cb);
  espconn_regist_disconcb(conn, user_tcp_discon_cb);

  os_sprintf(request,
      "GET %s HTTP/1.1\r\nHost: %s\r\n%s\r\n",
      OTA_PATH, OTA_HOST, HTTP_HEADER);

  os_printf("Time [%d]:", system_get_time());
  os_printf("Request: %s\n", request);
  espconn_sent(conn, (uint8_t *) request, os_strlen((char*)request));
}

void ICACHE_FLASH_ATTR user_tcp_recon_cb(void *arg, sint8 errType)
{
  os_printf("Time [%d]: ", system_get_time());
  os_printf("Reconnect: %d\n", errType);
}

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

#if 1
      // 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] = OTA_IP;
       uint32_t keepalive_time = 60;

       os_memcpy(user_tcp_conn.proto.tcp->remote_ip, esp_server_ip, 4);

       user_tcp_conn.proto.tcp->remote_port = 80;
     
       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_set_opt(&user_tcp_conn, ESPCONN_KEEPALIVE);
       espconn_set_keepalive(&user_tcp_conn, ESPCONN_KEEPIDLE, &keepalive_time);

       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("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] = SSID_PASSWORD;
   struct station_config stationConf;

   os_printf("Set station config\n");

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

}

#ifdef ADD_HEAP_PRINT_OUT
void ICACHE_FLASH_ATTR
user_heap_print_out()
{
  LOG_STATEMENT(INFO, "Remaining heap:%d\n", system_get_free_heap_size());
}

void ICACHE_FLASH_ATTR
user_routine_heap_print_out()
{
  os_timer_disarm(&test_timer);
  os_timer_setfn(&test_timer, (os_timer_func_t *)user_heap_print_out, NULL);
  os_timer_arm(&test_timer, 3000, 1); 
}
#endif

/******************************************************************************
 * FunctionName : user_init
 * Description  : entry of user application, init user function here
 * Parameters   : none
 * Returns      : none
*******************************************************************************/
extern "C" ICACHE_FLASH_ATTR void user_init(void)
{
  // Initialize UART
  uart_init(BIT_RATE_115200, BIT_RATE_115200);

  // Configure the UART
  os_printf("\r\nSystem init...\r\n");
  do_global_ctors();
  os_printf("\r\nGlobal constructors invoked\r\n");

  user_set_station_config(); 
}

Alexis
Posts: 5
Joined: Thu Jul 28, 2016 9:38 am

Re: espconn reconnect callback

Postby Alexis » Tue Nov 21, 2017 2:49 pm

Hello rith78,

It seems I've the same issue as you.
When I try to download a file for OTA, the connection reset.

Did you manage to solve this problem ?

Thanks,

AlexLuo
Posts: 1
Joined: Wed Nov 22, 2017 5:16 am

Re: espconn reconnect callback

Postby AlexLuo » Wed Nov 22, 2017 7:42 am

I don't have the experience about it.
It's good for me about the reconnect call back working after reset, but don't know if the reconnect callback response when the connection broken. I would like to try it but it did happen after I disconnect the server.
For your case, please verify the reset from server side or client side. If it's from server side, you can check the configuration on server.
Thank you for sharing your experience. If possible, can disconnect wifi or tcp connection to see the reconnect callback response -- it still happen?

Who is online

Users browsing this forum: No registered users and 6 guests