Async Mode
Serve the ElegantOTA portal through ESPAsyncWebServer, with the dependencies and configuration required on each platform.
By default, ElegantOTA registers its routes on the synchronous web server of your platform's Arduino core (WebServer or ESP8266WebServer). In async mode, it uses ESPAsyncWebServer (AsyncWebServer) instead.
Enable async mode only if your project uses ESPAsyncWebServer. You cannot use the synchronous web server and ESPAsyncWebServer in the same firmware.
Dependencies
Install the following libraries for your platform:
- (ESP32Async) AsyncTCP >= v3.5.0
- (ESP32Async) ESPAsyncWebServer >= v3.12.0
In PlatformIO, the ElegantOTA library manifest declares esp32async/ESPAsyncWebServer@^3.12.0 as a dependency. Add the TCP library for your platform to lib_deps yourself, for example:
lib_deps =
ayushsharma82/ElegantOTA@^4.0.0
esp32async/AsyncTCP@^3.5.0 ; ESP32
; esp32async/ESPAsyncTCP@^2.0.0 ; ESP8266
; ayushsharma82/RPAsyncTCP@^1.3.2 ; RP2040 / RP2350Use a different fork of a dependency
If your project depends on a fork of ESPAsyncWebServer that is not listed above, prevent PlatformIO from installing the declared dependency. This configuration is not tested.
- Install ElegantOTA manually, as described in Installation.
- Open the
library.jsonfile in the ElegantOTA library folder and delete thedependenciesentry.
Enable async mode
Async mode is controlled by the ELEGANTOTA_USE_ASYNC_WEBSERVER macro, which defaults to 0.
The Arduino IDE cannot set build flags, so you enable async mode in the library source:
-
Open your Arduino libraries folder.
-
Open the
srcfolder of the ElegantOTA library. -
In
ElegantOTA.h(ElegantOTA Pro:ElegantOTAPro.h), set theELEGANTOTA_USE_ASYNC_WEBSERVERmacro to1:#define ELEGANTOTA_USE_ASYNC_WEBSERVER 1 -
Save the file.
Repeat this change after you update the library, because an update replaces the file.
Usage
In async mode, pass a pointer to your AsyncWebServer to ElegantOTA.begin(). Keep calling ElegantOTA.loop() from loop(); it is still responsible for rebooting the device after a successful update and, in ElegantOTA Pro, for direct downloads and update channel checks.
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <ESPAsyncTCP.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <AsyncTCP.h>
#elif defined(TARGET_RP2040) || defined(TARGET_RP2350) || defined(PICO_RP2040) || defined(PICO_RP2350)
#include <WiFi.h>
#include <RPAsyncTCP.h>
#endif
#include <ESPAsyncWebServer.h>
#include <ElegantOTA.h>
AsyncWebServer server(80);
void setup() {
// ... connect to Wi-Fi
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! This is ElegantOTA AsyncDemo.");
});
ElegantOTA.begin(&server);
server.begin();
}
void loop() {
ElegantOTA.loop();
}Benefits
- Non-blocking request handling:
ESPAsyncWebServerhandles HTTP requests outside yourloop()function, so the main loop does not have to call a request handler to serve the portal. - Responsiveness: the device can continue other work while it serves the portal and receives an update.