Language: EN

como-hacer-un-servidor-asincrono-en-esp8266

How to create an asynchronous server on ESP8266 or ESP32

We continue with the entries of ESP8266 and ESP32 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 for the ESP32, adjusting the name of the libraries. At the end you have the code for both the ESP8266 and the ESP32.

We have been seeing several entries on how to set up a server with the ESP8266. Gradually, we have reached a functional solution in the last entry, serving compressed static files in Gzip from the 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, that is, it is capable of attending to several clients simultaneously.

In addition, the ESPAsyncWebServer library has additional advantages such as its speed, ease of use. Of course, it incorporates the management of compressed files in Gzip as standard. In other words, a wonderful library that has many improvements that you can consult in the documentation of the project page.

Therefore, we are going to modify our example server code to work with ESPAsyncWebServer.

First, the main sketch looks like this. Where we see that the most noticeable change is that we have nothing in the ‘loop’ function. It is not necessary since, as we have 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.

esp8266-servidor-spiffs-hello-world

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.

github-full

Version for ESP8266: https://github.com/luisllamasbinaburo/ESP8266-Examples

Version for ESP32: https://github.com/luisllamasbinaburo/ESP32-Examples