I created HTTP server using lwip netcon library.
Also i applied changes as suggested by :
https://github.com/espressif/esp-idf/issues/784.
I have following scenario:
1) When i Create simple http server it works fine.(Tested it with more than 140000 requested).
2) But when used spiffs to store my data in file transition stop at random time and when i try to connect
to http server , i can’t. I have to reboot Board.
3)To Check that is there any issue with spiffs i run standalone test in which i did same functionality of my
http server, without any socket(netcon) API. Which is works fine.
But when I merge Spiffs with simple http My server stop responding after some request(around 1500), And can’t reconnect with it.
My spiffs Partition is as following:
Code: Select all
## Label Usage Type ST Offset Length
0 nvs WiFi data 01 02 00009000 00006000
1 phy_init RF data 01 01 0000f000 00001000
2 factory factory app 00 00 00010000 00100000
3 storage Unknown data 01 82 00110000 00200000
Code: Select all
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
printf("got ip\n");
printf("ip: " IPSTR "\n", IP2STR(&event->event_info.got_ip.ip_info.ip));
printf("netmask: " IPSTR "\n", IP2STR(&event->event_info.got_ip.ip_info.netmask));
printf("gw: " IPSTR "\n", IP2STR(&event->event_info.got_ip.ip_info.gw));
printf("\n");
fflush(stdout);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
doReconnect=true;
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
wifi_config_t ap_config = {
.ap = {
.ssid = "REST_SERVER",
.password = "9876543210",
.authmode=WIFI_AUTH_WPA_WPA2_PSK,
.max_connection=4,
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_AP, &ap_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
Code: Select all
static void http_server(void *pvParameters)
{
ESP_LOGI(TAG,"system_get_free_heap_size in starting of server task =%d\n\n\n",system_get_free_heap_size());
struct netconn *conn , *newconn;
err_t err,err_listen,err_accept;
conn = netconn_new(NETCONN_TCP);
uint32_t total=0;
uint32_t used=0;
if(conn!=NULL)
{
err= netconn_bind(conn, NULL, 80);
if (err == ERR_OK)
ESP_LOGI(TAG,"socket bound");
err_listen=netconn_listen(conn);
if(err_listen==ERR_OK)
{
do{
ESP_LOGI(TAG,"system_get_free_heap_size before accept in request number %d =%d",++counter,system_get_free_heap_size());
ESP_LOGI(TAG,"system_get_time before accept in request number %d =%d",counter,system_get_time());
err_accept = netconn_accept(conn, &newconn);
if (err_accept == ERR_OK)
{
ESP_LOGI(TAG,"connection accept\n");
ESP_LOGI(TAG,"\nsystem_get_free_heap_size after accept =%d\n\n\n",system_get_free_heap_size());
ESP_LOGI(TAG,"\nsystem_get_free_heap_size befor close newconn =%d\n\n\n",system_get_free_heap_size());
newconn->recv_timeout=2000;
xTaskCreate(http_server_netconn_serve,"http_server_netconn_serve",5000,newconn,10,http_server_netconn_serve_Task);
}
} while(err_accept == ERR_OK);
netconn_close(conn);
ESP_LOGI(TAG,"conn is close");
netconn_delete(conn);
} /*err_listen end */
}/*conn end */
vTaskDelete(NULL);
ESP_LOGI(TAG,"http exit");
}
void http_server_netconn_serve(void *pvParameters)
{
struct netbuf *inbuf;
char *buf;
u16_t buflen;
err_t err;
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */
ESP_LOGI(TAG,"waitting for data");
err = netconn_recv(pvParameters, &inbuf);
if (err == ERR_OK)
{
err_t err1=netbuf_data(inbuf, (void**)&buf, &buflen);
if (err1 != ERR_OK)
ESP_LOGI(TAG,"Data recived from client is not copied to buffer");
ESP_LOGI(TAG,"\nbuflen=%d\n",buflen);
ESP_LOGI(TAG,"system_get_free_heap_size befor paser function call=%d",system_get_free_heap_size());
parser(buf,buflen,pvParameters);
ESP_LOGI(TAG,"system_get_free_heap_size after paser function call=%d",system_get_free_heap_size());
netbuf_delete(inbuf);
err=netconn_close(pvParameters);
if (err != ERR_OK)
ESP_LOGI(TAG,"pvParameters not closed succesfully in parser");
err_t err_newconn_d=netconn_delete(pvParameters);
if (err_newconn_d != ERR_OK)
ESP_LOGI(TAG,"newconn not delete succesfully");
ESP_LOGI(TAG,"system_get_free_heap_size after delete and befor free =%d",system_get_free_heap_size());
}
vTaskDelete(http_server_netconn_serve_Task);
task_delete=1;
ESP_LOGI(TAG,"http_server_netconn_serve Task deleted");
}
Where my parser Function Check which kind of requests (POST, GET,DELETE,PUT) is requested from client, and send html formatted string in respond.
When POST is requested so file which is sent in POST request , I stored it in SPIFFS in http_parser's Body callback using fopen and fwrite.
After More than 1000 Requests Server stop responding , And has following log:
Code: Select all
I (525892) wifi: station: 00:13:ef:d4:02:09 leave, AID = 1
I (525902) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
I (529002) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
I (529002) wifi: station: 00:13:ef:d4:02:09 join, AID=1, g, 20
I (580092) wifi: station: 00:13:ef:d4:02:09 leave, AID = 1
I (580092) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
I (582152) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
I (582152) wifi: station: 00:13:ef:d4:02:09 join, AID=1, g, 20
I (592152) wifi: station: 00:13:ef:d4:02:09 leave, AID = 1
I (592152) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
I (595362) wifi: n:1 0, o:1 0, ap:1 1, sta:255 255, prof:1
[code]
Am I missing something?