栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

ESP8266 NONOSDK Web Server 服务注意事项

ESP8266 NONOSDK Web Server 服务注意事项

一、问题
  1. 在用WEB访问时,无法收到回复,排查后发现是因为访问时,建立了多条链接,但程序上,只用了最原始那一个espconn,导致发到错误的链路上
  2. 在recv回调中,马上返回大数据,偶尔会有丢失
二、解决方法
  1. recv后,保存当前的espconn,之后数据返回到目前这个conn

    void ICACHE_FLASH_ATTR static
    server_recv(void *arg,char *pdata,unsigned short len){
    	//os_printf("收到PC发来的数据:%s.rn",pdata);
    	//espconn_sent((struct espconn *)arg,"已经收到rn",strlen("已经收到rn"));
    	//串口数据透传
    	//GPIO_OUTPUT_SET(GPIO_ID_PIN(12),1); 
    	//os_printf("server data = %d %02x %02x %02x %02x rn",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);
    
    }
    
    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 OKrnContent-Length: %drnServer: lwIP/1.4.0rn",
                       psend ? strlen(psend): 0);
    
            if (psend) {
                os_sprintf(httphead + strlen(httphead),
                           "Content-type: text/html; charset=utf-8rnPragma: no-cachernrn");
                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 BadRequestrnContent-Length: 0rnServer: lwIP/1.4.0rnn");
            length = strlen(httphead);
        }
    
        if (psend) {
        	//new_tcp_send(fd,pbuf,length);
            espconn_sent(ptrespconn, pbuf, length);
    		os_printf("sendrnrn%srnrn",pbuf);
        } else {
    		//new_tcp_send(fd,httphead,length);
            espconn_sent(ptrespconn, httphead, length);
        }
    
        if (pbuf) {
            os_free(pbuf);
            pbuf = NULL;
        }
    }
    
  2. 定时器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);
    	
    	
    
    }
    
                        html = (char *)os_zalloc(WEBCONFIG_SIZE);
                        if(html == NULL){
                            os_printf("os_zalloc error!rn");
                            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:%drn", 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);
    
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/674081.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号