Chat freely about anything...

User avatar
By PaulS
#83662 Hello,

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

Below are the functions which program the ESP82566 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);

}