CCCPaste

ESP32_Empty_for_OTA

/*
This small program prepares an ESP32 module to be flashed Over The Air with a .bin file

In the Tools menu make sure that you have the following settings:

Board: "ESP32 Dev Module"
Flash Size: "4MB (32Mb)"
Partition Scheme with OTA is needed
Partition Scheme: Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)
or
Partition Scheme: Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS)
Core Debug Level: "None"
PS RAM: "Disabled"
Port: "COMX" (please enter your COM port number instead of X)

Once you upload this firmware to the ESP32 module, it will be prepaired to receive the .bin file.
You will notice that an Access Point with ssid ESP32_Empty_for_OTA is created.
Default IP is 192.168.4.1 in Autonomous mode. Default password is 12345678.
Then in a browser you type serverIP ou serverIP/update.
*/

#include <ArduinoOTA.h>
#include <AsyncTCP.h> // name=Async TCP https://github.com/ESP32Async/AsyncTCP.git
#include <ElegantOTA.h> // name=ElegantOTA https://github.com/ayushsharma82/ElegantOTA
// warning if library is updated https://docs.elegantota.pro/getting-started/async-mode
#include <ESPAsyncWebServer.h> // name=ESP Async WebServer https://github.com/ESP32Async/ESPAsyncWebServer
#include <ESPmDNS.h>
#include <WiFi.h>

#define HOSTNAME "ESP32_Empty_for_OTA"

#define SSID_NAME_STA "*********"
#define PASSWORD_STA "*********"

#define SSID_NAME_AP HOSTNAME
#define PASSWORD_AP "12345678" // Mini. 8 car

// Initialize HTTP server
AsyncWebServer server(80);

void setup() {
Serial.begin(38400);

WiFi.mode(WIFI_AP_STA);
WiFi.setHostname(HOSTNAME);

WiFi.begin(SSID_NAME_STA, PASSWORD_STA);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nIP server in External mode: " + WiFi.localIP().toString());

WiFi.softAP(SSID_NAME_AP, PASSWORD_AP);
delay(500);
Serial.println("IP server in Autonomous mode: " + WiFi.softAPIP().toString());

OTA_Init();

server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
request->redirect("/update");
});

server.on("/default", HTTP_GET, [](AsyncWebServerRequest *request){
request->send(200, "text/plain", "Hello from ESP32 redirect\n\nOnly enter serverIP or serverIP/update");
});

ElegantOTA.begin(&server); // Start ElegantOTA warning if library is updated https://docs.elegantota.pro/getting-started/async-mode
// Start HTTP server
server.begin();
}

void loop() {
ArduinoOTA.handle();
ElegantOTA.loop();
}

void OTA_Init(){
ArduinoOTA.setHostname(HOSTNAME); // Hostname defaults to esp3232-[MAC]
ArduinoOTA.setPassword(PASSWORD_AP); // No authentication by default
//ArduinoOTA.setHostname(APSTAHostName.c_str()); // Hostname defaults to esp3232-[MAC]
//ArduinoOTA.setPassword(Global_PASSWORD.c_str()); // No authentication by default
ArduinoOTA
.onStart([]() {
String type;
if (ArduinoOTA.getCommand() == U_FLASH)
type = "sketch";
else // U_SPIFFS
type = "filesystem";
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
Serial.println("Start updating " + type);
})
.onEnd([]() {
Serial.println("\nEnd");
})
.onProgress([](unsigned int progress, unsigned int total) {
Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
})
.onError([](ota_error_t error) {
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
});
ArduinoOTA.begin();
}