Creating a Basic HTTP Server with ESP32

This tutorial shows how to create a basic “HTTP Server” application for the ESP32 chip and debug it with VisualGDB. We will use the Sparkfun ESP32 Thing board with the Olimex ARM-USB-OCD-H JTAG programmer to program and debug the board.

Before you begin, install VisualGDB 5.2 or later. We also recommend following the basic ESP32 tutorial to check that the JTAG connection with your device is working properly.

  1. Start Visual Studio and open the VisualGDB Embedded Project Wizard:01-demoprjname
  2. Proceed with the default settings on the first page:02-msbuild
  3. Select the ESP32 toolchain and the regular ESP32 device:03-esp32
  4. On the Sample Selection page select the “HTTP Server” sample and optionally change the subnet number:04-server
  5. Finally select the interface you want to use for debugging. If you are using one of the Olimex JTAG debuggers, enable the “set explicit speed” checkbox:05-debug
  6. Press “Finish” to generate your project. Before you can build it, open the Project Properties and change the optimization level for the debug configuration from -O0 to -Og as the default -O0 level prevents the ESP-IDF code from working correctly:06-optimize
  7. The project generated by VisualGDB does 3 vital steps. First it enables the DHCP server so that the devices connecting to its Wi-Fi network will automatically get assigned IP addresses:
        tcpip_adapter_init();
        tcpip_adapter_ip_info_t info = { 0, };
        IP4_ADDR(&info.ip, 192, 168, 123, 1);
        IP4_ADDR(&info.gw, 192, 168, 123, 1);
        IP4_ADDR(&info.netmask, 255, 255, 255, 0);
        ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
        ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
        ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));

    Then it starts the Wi-Fi hardware in the Access Point mode:

        wifi_config_t wifi_config;
        memset(&wifi_config, 0, sizeof(wifi_config));
        strcpy(wifi_config.ap.ssid, "ESP32_VisualGDB");
        wifi_config.ap.ssid_len = strlen(wifi_config.ap.ssid);
        wifi_config.ap.max_connection = 4;
        
        ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
        ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifi_config));
        ESP_ERROR_CHECK(esp_wifi_start());

    Finally it creates a thread that listens for connections on port 80 and responds to each one. Note that the last part uses the regular socket functions like bind() and accept() to work with the sockets:

    void ServerTask(void *pvParameters)
    {
        struct sockaddr_in server_addr, client_addr;
        int server_sock;
        socklen_t sin_size = sizeof(client_addr);
        bzero(&server_addr, sizeof(struct sockaddr_in));
        server_addr.sin_family = AF_INET;
        server_addr.sin_addr.s_addr = INADDR_ANY;
        server_addr.sin_port = htons(80);
     
        server_sock = socket(AF_INET, SOCK_STREAM, 0);
        bind(server_sock, (struct sockaddr *)(&server_addr), sizeof(struct sockaddr));
        listen(server_sock, 5);
        for (;;) 
        {
            int client_sock = accept(server_sock, (struct sockaddr *) &client_addr, &sin_size);
            //...
        }
    }
  8. The project is based on the open-source ESP-IDF framework, so you can use features like Go-to-definition and Code Map to quickly explore how different functions interact:codemap
  9. Build the project via Build->Build Solution:07-build
  10. Now press F5 to automatically program the SPI FLASH and begin debugging your project:08-load
  11. Once the project is running, connect to the ESP32_VisualGDB Wi-Fi network from your device:09-wifi
  12. Once the connection is successful, open 192.168.<subnet>.1 in your browser:10-page
  13. Set a breakpoint after the call to read() and refresh your browser page. The breakpoint will hit and you will see the HTTP request sent your the browser to the device:11-watchNote that the ESP32 tools have several known limitations. Please refer to this post for a discussion on known issues and workarounds.