We continue with the posts on the ESP8266 and ESP32 by seeing how to configure an asynchronous server with the help of the AsyncServer library.
We will refer to the ESP8266, but the same code is compatible with the ESP32, adjusting the library names. At the end, you have the code for both the ESP8266 and the ESP32.
We have had several posts on how to set up a server with the ESP8266. We have progressively arrived in the last post at a functional solution, serving static Gzip-compressed files from SPIFFS memory.
Is it possible to improve this solution? Yes, thanks to the great ESPAsyncWebServer library available at https://github.com/me-no-dev/ESPAsyncWebServer. Thanks to this Open Source library we can create an asynchronous server, meaning it can handle multiple clients simultaneously.
Furthermore, the ESPAsyncWebServer library has additional advantages such as its speed and ease of use. Of course, it includes built-in support for Gzip-compressed files. In short, it’s a wonderful library with many improvements you can check in the project page documentation.
Therefore, we are going to modify our example server code to work with ESPAsyncWebServer.
First, the main Sketch looks like this. Where we see the most notable change is that we have nothing in the ‘loop’ function. It’s not necessary because, as we said, the ESPAsyncWebServer server is asynchronous (oh yeah!).
#include <ESP8266WiFi.h>
#include <ESPAsyncWebServer.h>
#include <FS.h>
#include "config.h" // Replace with your network data
#include "Server.hpp"
#include "ESP8266_Utils.hpp"
void setup(void)
{
Serial.begin(115200);
SPIFFS.begin();
ConnectWiFi_STA();
InitServer();
}
void loop(void)
{
}
On the other hand, the Server.hpp file has also been modified, to adapt to the new library. However, as we see, its use is very similar. We establish routing for static content in the SPIFFS, an endpoint for GET calls to the URL /hello and, finally, a callback for ‘Not found’.
AsyncWebServer server(80);
void InitServer()
{
server.serveStatic("/", SPIFFS, "/").setDefaultFile("index.html");
server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hello world (from ESP8266)");
});
server.onNotFound([](AsyncWebServerRequest *request) {
request->send(400, "text/plain", "Not found");
});
server.begin();
Serial.println("HTTP server started");
}
Result
That’s it! We upload the same files from the “Hello world” web page that we have in the ‘data’ folder of the previous entries (compressed in Gzip, or not, as you prefer) and access to check, once again, that everything is still working correctly.

The ESPAsyncWebServer library is a great option to include in our projects. In fact, we will use it frequently from now on in our examples as the preferred solution for serving content.
In the next entry we will take a brief break in using ESP8266 as a server, to see the over-the-air (OTA) content update. Then we will start with the examples where we will exchange information between the ESP8266 and the client through different mechanisms (forms, Ajax and websocket). See you soon!
Download the code
All the code from this post is available for download on Github.
Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples
Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples

