Webserver

burkulesomesh43
Posts: 48
Joined: Tue Aug 14, 2018 6:21 am

Webserver

Postby burkulesomesh43 » Tue Oct 09, 2018 10:30 am

Hi,
I am using Webserver code.
https://github.com/cmmakerclub/esp32-we ... ain/main.c

In this code I have used fatfs also.

static void http_server_netconn_serve(struct netconn *conn)
{
ESP_LOGI(FHANDLE_TAG,"http_server_netconn_serve");

struct netbuf *inbuf;
char *Rec_buf;
u16_t buflen;
err_t err;

char *ssid_res = NULL;
char *pass_res = NULL;

wifi_config_t conf;
esp_wifi_get_config(WIFI_IF_STA,&conf);
ESP_LOGI(WEB_TAG,"Current ssid= %s, pass= %s\n",conf.sta.ssid,conf.sta.password);

char ssid_repl[]="Default_ssid";
char pass_repl[]="Default_passkey";
char *cur_ssid=(char*)conf.sta.ssid;
char *cur_pass=(char*)conf.sta.password;

ssid_res = replaceWord(http_index_hml,ssid_repl,cur_ssid);
pass_res = replaceWord(ssid_res,pass_repl, cur_pass);

err = netconn_recv(conn, &inbuf);

if (err == ERR_OK) {
netbuf_data(inbuf, (void**)&Rec_buf, &buflen);

/* Is this an HTTP GET command? (only check the first 5 chars, since
there are other formats for GET, and we're keeping it very simple )*/
ESP_LOGI(WEB_TAG,"buffer = <%s> \n", Rec_buf);
if (buflen>=5 &&
Rec_buf[0]=='G' &&
Rec_buf[1]=='E' &&
Rec_buf[2]=='T' &&
Rec_buf[3]==' ' &&
Rec_buf[4]=='/' ) {
ESP_LOGI(WEB_TAG,"Rec_buf[5] = %c\n", Rec_buf[5]);
/* Send the HTML header
* subtract 1 from the size, since we dont send the \0 in the string
* NETCONN_NOCOPY: our data is const static, so no need to copy it
*/
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
if(Rec_buf[5]=='s') {
char Rec_cpy1[600]={0},Rec_cpy2[600]={0};
strcpy(Rec_cpy1,Rec_buf);
strcpy(Rec_cpy2,Rec_buf);
ESP_LOGI(FHANDLE_TAG,"Rec_cpy1 = %s \n", Rec_cpy1);
ESP_LOGI(FHANDLE_TAG,"Rec_cpy2 = %s \n", Rec_cpy2);

char *A,*B,*get_ssid,*get_pass,*temp,*temp1;
A=strstr(Rec_cpy1,"ssid=");
get_ssid=strstr(Rec_cpy1,"&Passkey=");
temp=A+strlen("ssid=");
*get_ssid='\0';
get_ssid=temp;
ESP_LOGI(FHANDLE_TAG,"ssid = %s\n",get_ssid);
ESP_LOGI(FHANDLE_TAG,"Rec_cpy2 = %s \n", Rec_cpy2);
B=strstr(Rec_cpy2,"Passkey=");
get_pass=strstr(Rec_cpy2," HTTP/1.1");
temp1=B+strlen("Passkey=");
*get_pass='\0';
get_pass=temp1;
ESP_LOGI(FHANDLE_TAG,"pass = %s\n",get_pass);


ESP_LOGI(FHANDLE_TAG, "Opening file");
FILE *f = fopen("/spiflash/hello.txt", "wb");
if (f == NULL) {
ESP_LOGE(FHANDLE_TAG, "Failed to open file for writing");
return;
}
fprintf(f, "ssid=%s&Passkey=%s HTTP/1.1",get_ssid,get_pass);
fclose(f);
ESP_LOGI(FHANDLE_TAG, "File written");

esp_wifi_stop();
wifi_config_t sta_config = {
.sta = {
.ssid="Niruha",
.password ="1234567890" ,
.bssid_set = false
}
};

ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
else {
netconn_write(conn, pass_res, strlen(pass_res), NETCONN_NOCOPY);
}
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(conn);

/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
}

As I flash the code, esp32 get connected to the specified network and it gets some IP.
when I open webserver using that ip(for ex. http://192.168.32.113), it will show the webpage on browser for first time. If I try to browse it once again it resets the esp32.
Also the webserver is taking too much time(Around 15 seconds) to open.
Can you please explain what will be the reason for that?

Who is online

Users browsing this forum: WiFive and 3 guests