-->
Page 1 of 1

Corrupted data returned from ESP8285 when in SOFTAP mode

PostPosted: Wed Sep 11, 2019 8:27 am
by PaulS
Hello,

I have a simple code which sets ESP8285 as an AP and creates a TCP socket beween an Android App and the ESP8285 with the latter being the server. The app sends commands (strings) to the ESP8285 which are printed on the serial interface for debug. The problem is that, most of the time the messages received by the ESP8285 are truncated and/or corrupted. If I connect the same two devices to a shared AP and then create a TCP socket, the messages are received correctly.

Below are the functions which program the ESP8285 as an AP and a server, as well as the receive callback function which does not do anything rather than printing the received message to the serial interface.
Any help would be appreciated.

Code: Select allvoid wifi_createAP()
{
   
   if (wifi_get_opmode() != SOFTAP_MODE)
   {
      wifi_set_opmode(SOFTAP_MODE);
   }
   
   //AP configuration
   struct softap_config softapConf;
   
   os_memset(softapConf.ssid, 0, 32);
   os_memcpy(&softapConf.ssid, "ESP", 5);
   softapConf.ssid_len = 3;
   os_memset(softapConf.password, 0, 64);
   softapConf.channel = 1;
   softapConf.authmode = AUTH_OPEN;
   softapConf.ssid_hidden = 0;
   softapConf.max_connection = 4;
   softapConf.beacon_interval = 100;
   
   wifi_softap_set_config_current(&softapConf);
   
   
   if(wifi_softap_dhcps_status() != DHCP_STARTED)
   {
      if(!wifi_softap_dhcps_start())
         {
            return ReturnERROR();
         }
   }
   
   debug_print("\r\nAP is created!\r\n");
}


bool wifi_create_server(int argc, char** argv)
{
   int8_t   status      = 0;
   uint16_t port = parse_u16(argv[0], 0);
   uint16_t server_timeout = parse_u16(argv[1], 0);
   
   if (espconn_ptr == NULL)
       {
           espconn_ptr = (struct espconn*)os_zalloc(sizeof(struct espconn));
       }
   
   espconn_ptr->type  = ESPCONN_TCP;
   espconn_ptr->state = ESPCONN_NONE;
   
   if (espconn_ptr->proto.tcp == NULL)
    {
      espconn_ptr->proto.tcp = (esp_tcp*)os_zalloc(sizeof(esp_tcp));
    }
     
   espconn_ptr->proto.tcp->local_port  = port;
     
   espconn_regist_connectcb(espconn_ptr, user_tcp_server_connect_cb);
   espconn_regist_reconcb(espconn_ptr, user_tcp_server_recon_cb);
   status = espconn_accept(espconn_ptr);
   espconn_regist_time(espconn_ptr, server_timeout, 0);

  if (status != 0)
    {
      return ReturnERROR();
    }

  debug_print("Server created\r\n");
     
  return true;     
}


void user_tcp_recv_cb(void* arg, char* pusrdata, unsigned short length)
{

  debug_print("%s\r\n", pusrdata);

}

Re: Corrupted data returned from ESP8285 when in SOFTAP mode

PostPosted: Wed Sep 11, 2019 8:22 pm
by davydnorris
One thing I've noticed straight away is that you're just printing the received data directly. The SDK uses receive buffers and they get reused, so it's possible that by the time your print routine gets around to printing and sending the data to the console via the UART, the buffer is gone.

The other thing is to pay attention to the length parameter as the received data isn't necessarily null terminated.

Can you try using os_memcpy to copy 'length' characters from 'pusrdata' into a buffer of your own, null terminate it and then print that? See if that works better. Also it may be that the receive call back is being called after a delay in receive and partial data needs to be reassembled. It may also be that partial data is received.