DuboisP

ESP32_Empty_for_OTA

Nov 2nd, 2025 (edited)
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.09 KB | Source Code | 0 0
  1. /*
  2.    This small program prepares an ESP32 module to be flashed Over The Air with a .bin file
  3.    
  4.    In the Tools menu make sure that you have the following settings:
  5.  
  6.    Board: "ESP32 Dev Module"
  7.    Flash Size: "4MB (32Mb)"
  8.    Partition Scheme with OTA is needed
  9.    Partition Scheme: Minimal SPIFFS (1.9MB APP with OTA/190KB SPIFFS)
  10.    or
  11.    Partition Scheme: Default 4MB with spiffs (1.2MB APP/1.5MB SPIFFS)
  12.    Core Debug Level: "None"
  13.    PS RAM: "Disabled"
  14.    Port: "COMX"  (please enter your COM port number instead of X)
  15.  
  16.    Once you upload this firmware to the ESP32 module, it will be prepaired to receive the .bin file.
  17.    You will notice that an Access Point with ssid ESP32_Empty_for_OTA is created.
  18.    Default IP is 192.168.4.1 in Autonomous mode. Default password is 12345678.
  19.    Then in a browser you type serverIP ou serverIP/update.
  20. */
  21.  
  22. #include <ArduinoOTA.h>
  23. #include <AsyncTCP.h>               // name=Async TCP             https://github.com/ESP32Async/AsyncTCP.git
  24. #include <ElegantOTA.h>             // name=ElegantOTA            https://github.com/ayushsharma82/ElegantOTA  
  25.                                     // warning if library is updated https://docs.elegantota.pro/getting-started/async-mode
  26. #include <ESPAsyncWebServer.h>      // name=ESP Async WebServer   https://github.com/ESP32Async/ESPAsyncWebServer
  27. #include <ESPmDNS.h>
  28. #include <WiFi.h>
  29.  
  30. #define HOSTNAME        "ESP32_Empty_for_OTA"    
  31.  
  32. #define SSID_NAME_STA   "*********"
  33. #define PASSWORD_STA    "*********"
  34.  
  35. #define SSID_NAME_AP    HOSTNAME
  36. #define PASSWORD_AP     "12345678"  // Mini. 8 car
  37.  
  38. // Initialize HTTP server
  39. AsyncWebServer server(80);
  40.  
  41. void setup() {
  42.    Serial.begin(38400);
  43.    
  44.    WiFi.mode(WIFI_AP_STA);
  45.    WiFi.setHostname(HOSTNAME);  
  46.    
  47.    WiFi.setMinSecurity(WIFI_AUTH_WPA_PSK);  
  48.    WiFi.begin(SSID_NAME_STA, PASSWORD_STA, 0, NULL, true);
  49.    while (WiFi.status() != WL_CONNECTED) {
  50.       delay(500);
  51.       Serial.print(".");
  52.    }
  53.    Serial.println("\nIP server in External mode:   " + WiFi.localIP().toString());
  54.    
  55.    WiFi.softAP(SSID_NAME_AP, PASSWORD_AP);
  56.    delay(500);
  57.    Serial.println("IP server in Autonomous mode: " + WiFi.softAPIP().toString());
  58.    
  59.    OTA_Init();
  60.    
  61.    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
  62.       request->redirect("/update");
  63.    });
  64.  
  65.    server.on("/default", HTTP_GET, [](AsyncWebServerRequest *request){
  66.       request->send(200, "text/plain", "Hello from ESP32 redirect\n\nOnly enter serverIP or serverIP/update");
  67.    });
  68.    
  69.    ElegantOTA.begin(&server);    // Start ElegantOTA  warning if library is updated https://docs.elegantota.pro/getting-started/async-mode
  70.    // Start HTTP server
  71.    server.begin();
  72. }
  73.  
  74. void loop() {
  75.    ArduinoOTA.handle();
  76.    ElegantOTA.loop();
  77. }
  78.  
  79. void OTA_Init(){
  80.    ArduinoOTA.setHostname(HOSTNAME);               // Hostname defaults to esp3232-[MAC]
  81.    ArduinoOTA.setPassword(PASSWORD_AP);            // No authentication by default
  82.    //ArduinoOTA.setHostname(APSTAHostName.c_str());     // Hostname defaults to esp3232-[MAC]
  83.    //ArduinoOTA.setPassword(Global_PASSWORD.c_str());  // No authentication by default
  84.    ArduinoOTA
  85.    .onStart([]() {
  86.       String type;
  87.       if (ArduinoOTA.getCommand() == U_FLASH)
  88.          type = "sketch";
  89.       else // U_SPIFFS
  90.          type = "filesystem";
  91.          // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end()
  92.       Serial.println("Start updating " + type);
  93.    })
  94.    .onEnd([]() {
  95.       Serial.println("\nEnd");
  96.    })
  97.    .onProgress([](unsigned int progress, unsigned int total) {
  98.       Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  99.    })
  100.    .onError([](ota_error_t error) {
  101.       Serial.printf("Error[%u]: ", error);
  102.       if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
  103.       else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
  104.       else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
  105.       else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
  106.       else if (error == OTA_END_ERROR) Serial.println("End Failed");
  107.    });
  108.    ArduinoOTA.begin();
  109. }
  110.  
Advertisement
Add Comment
Please, Sign In to add comment