ESP32 Web Server Example with ESP-IDF: Step-by-Step Guide

Where is the ESP32 web server example in ESP-IDF?
ESP-IDF ships its own ready-made web server project, so you do not need to write one from scratch. It lives inside the ESP-IDF tree at examples/protocols/http_server/simple. ESP-IDF's example projects are normally found under ${IDF_PATH}/examples, where IDF_PATH is the environment variable pointing at your esp-idf directory.
This example gives you an HTTP server that accepts browser connections. On its own it expects the board to already be on a network; if you want the ESP32 to create its own Wi-Fi network for a phone to join, there is a second example you will combine with it: examples/wifi/getting_started/softAP. One maker on Stack Overflow did exactly this to build a self-contained device where a phone connects directly to the ESP32 and opens its web page with HTML forms and buttons, with no internet connection involved.
It helps to understand how ESP-IDF sees a project like this. The build-system documentation describes a web server project as a stack of components: the ESP-IDF base libraries, the Wi-Fi drivers, a TCP/IP stack, FreeRTOS, the web server itself, any sensor driver, and your main code tying it together. Each component is compiled into a static library and linked into your app, and each can expose settings through a text-based menu (menuconfig). That is why the example projects all have a configuration menu for things like the Wi-Fi SSID and the HTTP basic-auth password.
| Term | Meaning in ESP-IDF |
|---|---|
| Project | A directory with everything needed to build one app (plus bootloader, partition table) |
| Component | Standalone module compiled to a .a library and linked into the app |
| sdkconfig | The single project configuration file, edited with idf.py menuconfig |
| Target | The chip you build for; list them with idf.py --list-targets |
\2A typical ESP32 development board, the kind connected over USB to flash the ESP-IDF web server example. Image: Edwiyanto, CC BY-SA 4.0, via Wikimedia Commons
What do I need before I start?
Prerequisites gathered from the sources:
- An ESP32 development board connected over USB. The Stack Overflow author used an ESP32 module connected via USB from an Ubuntu 20 virtual machine. Any ESP-IDF supported target works; check
idf.py --list-targetsfor the list in your ESP-IDF version. - ESP-IDF installed. ESP-IDF is deliberately kept out of your project folder and linked through the
IDF_PATHenvironment variable. Before usingidf.pyin a fresh shell you source the export script:
. $IDF_PATH/export.sh
- Python. The build-system docs state ESP-IDF works well with Python 3.11+ (one third-party ESP-IDF project in the sources lists Python 3.8+ as its own floor; follow your ESP-IDF version's requirement).
- Toolchain on your PATH. The compiler toolchain is also not part of the project; it must be installed and reachable from the command line. The ESP-IDF install script handles this.
- An editor or IDE. Plain terminal is fine. Some people use PlatformIO with the ESP-IDF framework inside VS Code, and ESP-IDF also works with any IDE that understands CMake, pointing it at the project's
CMakeLists.txt. For special steps like flashing from an IDE, Espressif recommends callingidf.py. - A phone or laptop with a browser to act as the client.
You do not need Wi-Fi infrastructure if you take the softAP route, since the ESP32 becomes the hotspot.
\2Espressif ESP-WROOM-32 Wi-Fi and Bluetooth module, the radio module found on many ESP32 dev boards. Image: Brian Krent, CC BY-SA 4.0, via Wikimedia Commons
How do I build and run the ESP-IDF web server example?
-
Copy the example out of the ESP-IDF tree. Working on a copy keeps your ESP-IDF checkout clean. The project directory you end up with should contain
main/and aCMakeLists.txtat its root. -
Open a shell with ESP-IDF activated.
. $IDF_PATH/export.sh
- Configure the project. Run the text menu and look for the example's own settings (the Stack Overflow author mentions Wi-Fi SSID and the HTTP basic-auth password as the items the example exposes):
idf.py menuconfig
Everything you change here is saved in sdkconfig in the project root. A project has exactly one of these files.
- Build. This runs CMake to configure and Ninja to compile:
idf.py build
If you want to speed things up on a multi-core machine, pass a job count:
idf.py -j 6 build
The same value can be set once with the IDF_PY_BUILD_JOBS environment variable.
- Flash.
idf.pywrapsesptoolfor flashing and prints each command it runs so you can see what is happening. If you prefer to drive the build tool directly, Ninja exposes equivalent targets from thebuild/directory:
ESPPORT=/dev/ttyUSB0 ninja flash
Available Ninja/Make targets are flash (everything), app-flash (app only) and bootloader-flash (bootloader only). ESPPORT and ESPBAUD select the serial port and baud rate. The VAR=value command syntax at the start is Bash-style and works on Linux and macOS; with make you can instead append the variables at the end, which works everywhere:
make -j3 app-flash ESPPORT=COM4 ESPBAUD=2000000
-
Monitor the serial output. ESP-IDF projects follow the same build, flash and monitor flow, so after flashing, open the serial monitor through
idf.pyto watch the boot log and the IP address the board reports. -
Rebuild after changes. You do not need to re-run CMake manually. After the first build,
ninja(oridf.py build) is enough; Ninja re-invokes CMake automatically when the project needs reconfiguring.
How do I combine the web server with a Wi-Fi access point?
The http_server/simple example and the softAP example are separate projects, and a common goal is to run both at once so a phone can join the ESP32's own network and open the page. The Stack Overflow thread covers exactly this.
The code side is straightforward in the author's experience: the access point keeps running in the background even after the main function returns, so you can bring the web-server code into the softAP project's main function and start it there. The author hard-coded the device address as 192.168.1.1 for their design (another ESP-IDF server project in the sources used 192.168.4.1 as the address devices connect to; use whatever your softAP configuration assigns).
The trickier part is keeping idf.py menuconfig useful after the merge, because each example defines its own menu entries. The accepted answer is simple:
- Menu items are declared in the component's
Kconfig.projbuildfile. - Open
main/Kconfig.projbuildin the project you are pulling from and paste its content intomain/Kconfig.projbuildof the project you are keeping. - Re-run
idf.py menuconfig; both sets of options now appear in one menu.
This is the same mechanism larger ESP-IDF projects use. For example, the ESP32JTAG firmware (an ESP32-S3 project with an HTTPS web server) defines its board profiles in main/Kconfig.projbuild and asks you to select them in menuconfig. That project also shows a useful habit when you maintain several configurations: give each a separate build directory so the sdkconfig files do not collide:
idf.py -B build_board_esp32s3_devkit menuconfig
idf.py -B build_board_esp32s3_devkit build
| Need | Where to look |
|---|---|
| Wi-Fi SSID / password for the AP | softAP example's Kconfig.projbuild entries, via menuconfig |
| HTTP basic-auth password | http_server/simple entries, via menuconfig |
| Merged menu | Single main/Kconfig.projbuild containing both |
How do I verify the ESP32 web server is working?
Use the serial log first, then the browser.
- Check the boot log. After flashing, watch the monitor output for the Wi-Fi stage (station connected, or access point started) and for the HTTP server starting. If you merged the two examples, you should see both happen in sequence.
- Join the network. In station mode, your phone or PC must be on the same network as the ESP32. In softAP mode, connect to the SSID you set in
menuconfig. - Open the address in a browser. Use the IP the board logged, or the fixed AP address from your configuration (the sources mention
192.168.1.1and192.168.4.1in two different designs; yours depends on your settings). - Enter credentials if prompted. The simple example can protect pages with HTTP basic auth, with the password set in
menuconfig. - Test with one client first. Confirm a single device loads pages quickly before adding more. One maker found that a server working perfectly with one client slowed to a crawl with two or three, which points at a server design issue rather than Wi-Fi (see the next section).
If nothing shows up in the serial monitor at all, re-check that the flash actually went to the right port: ESPPORT or the equivalent idf.py port option must match the USB device the board enumerated as (/dev/ttyUSB0-style on Linux, COM4-style on Windows in the sources' examples).
What are the common mistakes with an ESP-IDF web server?
Hand-rolling a socket loop that serves one client at a time. A detailed Stack Overflow report describes a custom server built on raw sockets (PlatformIO + ESP-IDF in VS Code) that worked with one device but stalled for 30–60 seconds or more when two or three devices requested pages, randomly favouring one client. The author's own diagnosis was that the socket task accepted one connection and handled everything for it before looping back to accept() the next. Spawning a task per socket, raising the listen() backlog from 1 to 32, setting keepalive to 0, binding to a specific address instead of INADDR_ANY, pinning tasks to another core and adding vTaskDelay() calls did not fix it. Bluetooth was also enabled on that device. The lesson for beginners: start from the http_server/simple example rather than a bare tcp_server loop, and keep Bluetooth off until the web server behaves.
Running the two examples separately and expecting them to talk. http_server/simple and softAP are independent projects. Merge the code and the Kconfig.projbuild content as described above; otherwise either the AP is missing or the menu options disappear.
Editing settings in code instead of menuconfig. The menu exists to reduce human error when changing SSIDs or passwords. Keeping settings in Kconfig.projbuild means they stay editable through idf.py menuconfig and land in sdkconfig.
Forgetting IDF_PATH and the export script. ESP-IDF and the toolchain live outside your project. If idf.py is not found or the wrong version builds, you probably opened a new shell without sourcing export.sh.
Sharing one build/ directory between different configurations. If you flip between boards or feature sets, use idf.py -B <dir> to give each its own build directory and sdkconfig, as the ESP32JTAG project does.
Mixing shell syntaxes. ESPPORT=/dev/ttyUSB0 ninja flash works in Bash on Linux/macOS but not in Windows Command Prompt; put variables at the end with make, or set them in your OS or IDE.
Can I go further: HTTPS, auth and APIs on ESP-IDF?
Yes, and the sources include a real-world example of how far an ESP-IDF web server can go. The open-source ESP32JTAG firmware (ESP32-S3, 8 MB flash, PSRAM required, ESP-IDF v5.5.2 or newer) exposes its whole configuration UI through an HTTPS web server with an embedded TLS certificate, protects its configuration pages with basic auth, supports both AP mode (standalone hotspot) and STA mode, does Wi-Fi provisioning, delivers OTA firmware updates over HTTPS, and bridges UART traffic over WebSocket. It even applies settings without a reboot via a POST /api/sreset_config endpoint.
That is well beyond the simple example, but it is built with the same tools you used above: export.sh, idf.py -B <dir> menuconfig, idf.py -B <dir> build, and board profiles declared in main/Kconfig.projbuild. Treat the simple HTTP example as your foundation, add the softAP merge when you need a standalone device, and look at projects like this one when you want TLS, authentication or a JSON-style API on top.
Frequently asked questions
Which ESP-IDF example gives me a web server on ESP32?
examples/protocols/http_server/simple inside the ESP-IDF tree. For a self-hosted Wi-Fi network, combine it with examples/wifi/getting_started/softAP.
How do I keep menuconfig working after merging two examples?
Copy the content of one project's main/Kconfig.projbuild into the other's. The menu items from both then appear in idf.py menuconfig.
Why does my ESP-IDF web server slow down with more than one device?
In the reported case the custom socket task handled one accepted connection fully before accepting the next; a per-socket task, larger listen backlog and keepalive tweaks did not help. Start from the http_server example instead of a raw socket loop.
Do I need to run CMake again after every code change?
No. After the first build, running ninja (or idf.py build) is enough; Ninja re-runs CMake automatically when reconfiguration is needed.
Which Python version does ESP-IDF want?
The build-system docs say ESP-IDF works well with Python 3.11+.
The ESP-IDF http_server/simple example is the right starting point for an ESP32 web server: configure with menuconfig, build with idf.py, flash, and open the board's IP. Merging it with the softAP example and its Kconfig.projbuild gives you a phone-to-ESP32 device with no router, and avoiding a hand-written single-client socket loop saves the most common performance headache.
Sources
github.comESP-IDF Tests with Pytest Guide (esp-idf docs)github.comBuild System (esp-idf docs)stackoverflow.comESP32 - combining two or more SDK example projects into onestackoverflow.comESP32 webserver slows/hangs with more than 1 device accessing pagesgithub.comEZ32Inc/esp32jtag_firmware (GitHub README)This guide summarises the public sources listed above in our own words. Check the original documentation before relying on exact figures.


