Table of Contents
For NodeMCU ESP8266
For PLATFORMIO User
https://docs.elegantota.pro/getting-started/installation As ElegantOTA supports multiple architectures, PlatformIO will try to include all the dependencies automatically which will often result in compilation errors. To mitigate this issue, please follow these steps:
- Delete .pio/libdeps folder (if it exists) in your project before proceeding.
- Open platformio.ini file of your project.
- Add the following line inside your platformio.ini file:
lib_compat_mode = strict
- Save the changes to the platformio.ini file.
In platformio.ini:
lib_deps = # RECOMMENDED # Accept new functionality in a backwards compatible manner and patches ayushsharma82/ElegantOTA @ ^3.1.6
Synchronous
[env:nodemcuv2] platform = espressif8266 board = nodemcuv2 framework = arduino lib_compat_mode = strict lib_deps = ayushsharma82/ElegantOTA @ ^3.1.6
#include <Arduino.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <WiFiClient.h>
#include <WebServer.h>
#endif
#include <ElegantOTA.h>
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
#if defined(ESP8266)
ESP8266WebServer server(80);
#elif defined(ESP32)
WebServer server(80);
#endif
void setup(void) {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", []() {
server.send(200, "text/plain", "Hi! This is ElegantOTA Demo.");
});
ElegantOTA.begin(&server); // Start ElegantOTA
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
server.handleClient();
ElegantOTA.loop();
}
Asynchronous
If you use PlatformIO then we can use a neat trick! You can set build_flags in your platformio.ini file that enables Async Mode of ElegantOTA.
- Open platformio.ini file of your project
- Paste this line build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1 at bottom
https://docs.elegantota.pro/getting-started/async-mode
Requirements for ESP8266
(mathieucarbou) esphome-ESPAsyncTCP - v2.0.0 and ESPAsyncWebServer - v3.3.20
Requirements for ESP32
(mathieucarbou) AsyncTCP @ v3.2.10 and ESPAsyncWebServer - v3.3.20
[env:nodemcuv2] platform = espressif8266 board = nodemcuv2 framework = arduino lib_compat_mode = strict lib_deps = ayushsharma82/ElegantOTA @ ^3.1.6 esp32async/ESPAsyncTCP@^2.0.0 mathieucarbou/ESPAsyncWebServer@^3.6.0 build_flags=-DELEGANTOTA_USE_ASYNC_WEBSERVER=1
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#include <ESPAsyncWebServer.h>
#include <ElegantOTA.h>
const char* ssid = "........";
const char* password = "........";
AsyncWebServer server(80);
unsigned long ota_progress_millis = 0;
void onOTAStart() {
// Log when OTA has started
Serial.println("OTA update started!");
// <Add your own code here>
}
void onOTAProgress(size_t current, size_t final) {
// Log every 1 second
if (millis() - ota_progress_millis > 1000) {
ota_progress_millis = millis();
Serial.printf("OTA Progress Current: %u bytes, Final: %u bytes\n", current, final);
}
}
void onOTAEnd(bool success) {
// Log when OTA has finished
if (success) {
Serial.println("OTA update finished successfully!");
} else {
Serial.println("There was an error during OTA update!");
}
// <Add your own code here>
}
void setup(void) {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to ");
Serial.println(ssid);
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
server.on("/", HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hi! This is ElegantOTA AsyncDemo.");
});
ElegantOTA.begin(&server); // Start ElegantOTA
// ElegantOTA callbacks
ElegantOTA.onStart(onOTAStart);
ElegantOTA.onProgress(onOTAProgress);
ElegantOTA.onEnd(onOTAEnd);
server.begin();
Serial.println("HTTP server started");
}
void loop(void) {
ElegantOTA.loop();
}
New Async
In PlatformIO
lib_deps =
ayushsharma82/ElegantOTA @ ^3.1.6 ; Modern OTA
me-no-dev/ESPAsyncWebServer @ ^1.2.3 ; Required for ElegantOTA
me-no-dev/ESPAsyncTCP @ ^1.2.2 ; Required for ESPAsyncWebServer
build_flags = -DELEGANTOTA_USE_ASYNC_WEBSERVER=1
In main.cpp, the include SHOULD BE in this order:
#include <ESP8266WiFi.h> #include <ESPAsyncTCP.h> #include <ESPAsyncWebServer.h> #include <ElegantOTA.h> AsyncWebServer server(80);
then in setup()
server.on("/", WebRequestMethod::HTTP_GET, [](AsyncWebServerRequest *request) {
request->send(200, "text/plain", "Hello");
});
ElegantOTA.begin(&server);
server.begin();
in loop()
ElegantOTA.loop();
