PlatformIO OTA on ESP8266

Thank you @sivar2311.
I got the OTA update via platformio IDE now working. It is like you say: UDP is listening and waiting for connection, handling authentification and enables the wificlient if everything is up.

I got now another question/trouble - regarding the webserver based update. Within the webserver i added a file upload selector.

  1. after the firmware is uploaded, i got issue with resetting the esp with esp.restart() but esp.reset() works but should be avoided.
  2. LittleFS/ SPIFFS Upload does never start due of lack of space.

Could it both be possible, that i have an calculation issue in my update.write() function? I found a reference in the commendwhich calculates in a differ way(?)

void SSEWrapper::handleDoUpdate(AsyncWebServerRequest *request, const String& filename, size_t index, uint8_t *data, size_t len, bool final) {
  unsigned long deltaT = millis();
  if (!index){
    DEBUG_SERIAL.print("[OTA] Update: ");
    DEBUG_SERIAL.println(filename);
    SSEWrapper::content_len = request->contentLength();
    DEBUG_SERIAL.print("[OTA] Filesize recived [KB]: ");
    DEBUG_SERIAL.println(SSEWrapper::content_len);

    // if filename includes spiffs/littlefs, update the spiffs/littlefs partition
    int cmd = ((filename.indexOf("spiffs") > -1) || (filename.indexOf("littlefs") > -1)) ? U_FS : U_FLASH;
    Update.runAsync(true);

    /*
      uint32_t update_size = 0;
      // Filesystem (SPIFFS // LITTLEFS)
      if (cmd = U_FS){
        update_size = ((ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000);
      } // Firmware
      else if (cmd = U_FLASH){
        update_size = (size_t)FS_end - (size_t)FS_start);
      }
    */

    if (!Update.begin(SSEWrapper::content_len, cmd)) {
      #ifdef SERIAL_OUTPUT_DEBUGGING
        Update.printError(Serial);
      #endif
    }

    request->send(200, "text/html", "<head><meta http-equiv='refresh' content='20;URL=/'/></head><body>Upload started! Please wait while the device reboots</body>");
  }

  if (Update.write(data, len) != len) {
    #ifdef SERIAL_OUTPUT_DEBUGGING
      Update.printError(Serial);
    #endif
  }else{
    size_t temp_size = (Update.progress()*100)/Update.size();
    if(old_size != temp_size){
      DEBUG_SERIAL.print("[OTA] Progress: ");
      DEBUG_SERIAL.println(temp_size);
      old_size = temp_size;
    }
  }

  if(final){
    if(Update.hasError()){
      DEBUG_SERIAL.println(Update.getErrorString());
    }
    if(!Update.end(true)){
      #ifdef SERIAL_OUTPUT_DEBUGGING
        Update.printError(Serial);
      #endif
    } else {
      DEBUG_SERIAL.println("[OTA] Update complete!");
      DEBUG_SERIAL.println("[OTA] Reboot!");

      // Deactivating WiFi 
      WiFi.mode(WIFI_OFF);

      // Wait 1s until reboot
      delay(1000);

      // ESP8266 Restart after Update
      // Using reset() Methode instead of restart() because with restart there is no "real" restart of ESP8266!
      ESP.reset();
    }
  }
  DEBUG_SERIAL_INFORMATION.print("handleDoUpdate [ms]: ");
  DEBUG_SERIAL_INFORMATION.println(millis()-deltaT);
}

Edit by sivar2311: Code formatted as preformatted text for better readability.
Please note: How to post logs and code in PlatformIO Community Forum