一、问题
- 在用WEB访问时,无法收到回复,排查后发现是因为访问时,建立了多条链接,但程序上,只用了最原始那一个espconn,导致发到错误的链路上
- 在recv回调中,马上返回大数据,偶尔会有丢失
二、解决方法
-
recv后,保存当前的espconn,之后数据返回到目前这个conn
void ICACHE_FLASH_ATTR static server_recv(void *arg,char *pdata,unsigned short len){ //os_printf("收到PC发来的数据:%s.\r\n",pdata); //espconn_sent((struct espconn *)arg,"已经收到\r\n",strlen("已经收到\r\n")); //串口数据透传 //GPIO_OUTPUT_SET(GPIO_ID_PIN(12),1); //os_printf("server data = %d %02x %02x %02x %02x \r\n",len,pdata[0],pdata[1],pdata[2],pdata[3]); //uart0_tx_buffer(pdata,len); //GPIO_OUTPUT_SET(GPIO_ID_PIN(12),0); connect_conn = arg; webserver_recv(&web_conn,pdata,len); }
/****************************************************************************** * FunctionName : data_send * Description : processing the data as http format and send to the client or server * Parameters : arg -- argument to set for client or server * responseOK -- true or false * psend -- The send data * Returns : *******************************************************************************/ static void ICACHE_FLASH_ATTR data_send(void *arg, bool responseOK, char *psend) { uint16 length = 0; char *pbuf = NULL; char httphead[256]; struct espconn *ptrespconn = arg; int *pfd=arg; int fd=*pfd; os_memset(httphead, 0, 256); if (responseOK) { os_sprintf(httphead, "HTTP/1.0 200 OK\r\nContent-Length: %d\r\nServer: lwIP/1.4.0\r\n", psend ? strlen(psend): 0); if (psend) { os_sprintf(httphead + strlen(httphead), "Content-type: text/html; charset=utf-8\r\nPragma: no-cache\r\n\r\n"); length = strlen(httphead) + strlen(psend); pbuf = (char *)os_zalloc(length + 1); os_memcpy(pbuf, httphead, strlen(httphead)); os_memcpy(pbuf + strlen(httphead), psend, strlen(psend)); } else { os_sprintf(httphead + strlen(httphead), "\n"); length = strlen(httphead); } } else { os_sprintf(httphead, "HTTP/1.0 400 BadRequest\r\nContent-Length: 0\r\nServer: lwIP/1.4.0\r\n\n"); length = strlen(httphead); } if (psend) { //new_tcp_send(fd,pbuf,length); espconn_sent(ptrespconn, pbuf, length); os_printf("send\r\n\r\n%s\r\n\r\n",pbuf); } else { //new_tcp_send(fd,httphead,length); espconn_sent(ptrespconn, httphead, length); } if (pbuf) { os_free(pbuf); pbuf = NULL; } }
-
定时器100ms后再发送数据
void web_send_timer_cb() { //static uint8_t time = 0; data_send(connect_conn, true, html); os_free(html); html = NULL; //os_timer_arm(&web_send_timer,200,0); /* if(time++==1) { time = 0; os_timer_disarm(&web_send_timer); os_free(html); html = NULL; } */ }
html = (char *)os_zalloc(WEBCONFIG_SIZE); if(html == NULL){ os_printf("os_zalloc error!\r\n"); goto _temp_exit; } #if 1 os_memset(html,0,WEBCONFIG_SIZE); os_strcpy(html,(char *)WEB_CONFIG); #else ret = spi_flash_read(964*1024, (uint32 *)html, WEBCONFIG_SIZE); // start address:0x10000 + 0xC0000 if(ret != SPI_FLASH_RESULT_OK){ os_printf("spi_flash_read err:%d\r\n", ret); os_free(html); html = NULL; goto _temp_exit; } #endif html[WEBCONFIG_SIZE] = 0; // put 0 to the end os_timer_disarm(&web_send_timer); os_timer_setfn(&web_send_timer, web_send_timer_cb , NULL); //a demo to process the data in uart rx buffer os_timer_arm(&web_send_timer,100,0);