Skip to main content

ESP32: Webserver using esp-idf SDK

Web-Server or Web-socket oe http socket is most commonly used method to read or write data from Ethernet or now as we move to new technology WiFi in embedded universe. It was already there, but with cheap and easily available ESP modules it has taken a big leap forward.

Here in this topic, i will explain you to added Web Server to ESP32 module using eclipse.

If you are new to ESP32 using eclipse you start from here for adding ESP32 in eclipse.

Ok lets start with ESP32 Webserver, initialization,

       

        struct netconn *conn, *newconn;
 err_t err;
 conn = netconn_new(NETCONN_TCP); // Create TCP socket
 netconn_bind(conn, IP_ADDR_ANY, 80); // bind socket
 netconn_listen(conn);

       
 

Now, write the main body to accept web client connection and to decrypt http frame.

       
     do {
  err = netconn_accept(conn, &newconn); // accept incoming connection
  if (err == ERR_OK) {
   ESP_LOGI(TAG, "Accepted Conn %d\n", err);
   http_server_netconn_serve(newconn); // decrypt packet 
   netconn_delete(newconn);
  }
  vTaskDelay( (TickType_t)10); /* allows the freeRTOS scheduler to take over if needed */
 } while(err == ERR_OK);
 

Now writing the http decryption code,
       
   void http_server_netconn_serve(struct netconn *conn) {

 struct netbuf *inbuf;
 char *buf = NULL;
 u16_t buflen;
 err_t err;
 const char new_line[2] = "\n";

 err = netconn_recv(conn, &inbuf);
 if (err == ERR_OK) {

  netbuf_data(inbuf, (void**)&buf, &buflen);
  /* extract the first line of the request */
  char *save_ptr = buf;
  char *line = strtok_r(save_ptr, new_line, &save_ptr);
  if(line) {

   // default page
   if(strstr(line, "GET / ")) {
    ESP_LOGI(TAG, "Index page \n");
    netconn_write(conn, http_html_hdr, sizeof(http_html_hdr) - 1, NETCONN_NOCOPY);
    netconn_write(conn, index_html_start, index_html_end - index_html_start, NETCONN_NOCOPY);
   }
   else{
    netconn_write(conn, http_400_hdr, sizeof(http_400_hdr) - 1, NETCONN_NOCOPY);
   }
                }
  else{
   netconn_write(conn, http_404_hdr, sizeof(http_404_hdr) - 1, NETCONN_NOCOPY);
  }
 }

 /* free the buffer */
 netbuf_delete(inbuf);
}
 

Adding index.htm file to project, You use this method embedding-binary-data
follow these steps,
1. You can set a variable COMPONENT_EMBED_FILES in component.mk, giving the names of the files to embed in this way:
       
     COMPONENT_EMBED_FILES := index.html
 
2.The file’s contents will be added to the .rodata section in flash, and are available via symbol names as follows: 
       
extern const uint8_t index_html_start[] asm("_binary_index_htm_start");
extern const uint8_t index_html_end[] asm("_binary_index_htm_end");
 

Reference: 

Comments

Popular posts from this blog

ESP32: UART interrupt Handling

I am Sushant, from Mumbai, India. I am an embedded firmware engineer, i love to write code for micro controllers. This may not be a professional post, you may have to tolerate my unprofessional writing skills. Anyways todays topic in ESP32, a very commonly and trending MCU. I had come across project where there was a need to write my own code for UART, i tried or you can searched google even on page 2, but could find any sample code or example. I found the solution, not easy way, had to put in some hours, but i would like to put this example in blog so everybody can use it or understand it, It is simple code for UART receive interrupt, So Lets start, Everybody must be aware of how to initialise UART to use as RTOS task in ESP32, oh yeah and also i have used ESP-IDF to demostrate this code, so for arduino geeks this may not help, not a arduino FAN :). Firstly,  Configuring parameters of an UART; uart_config_t uart_config = { .baud_rate = 115200, .data_bits =