Ota issue latter upload new firmware

Can any one help me to understand what the problem I am facing during OTA upload?
When I try a single example of OTA Upload via web navagator sketch it runs very well, but when trying in my large code 1.3mb, it seems to work during writing but bootloader show some error mmaping in the first 20 bite alocation of partition.
I tried to alocate the buffer in heap as someone told me but always the same error.
One valid information is that when a cut of my void loopRTO(main task), it works every 4 tryings, what makes me think memory could be a problem, but this is the information about memory free.

Final memory free: 138904
Min. Memory available: 97712
Max use of stack: 5708
Heap Free:: 184536
Total LittleFS: 1179648
Usage LittleFS: 8192
Total Sketch: 1273008

This is what I see on Serial monitor:

13:57:34.214 -> Recebendo pacote: 1436 bytes, index: 1270080
13:57:34.214 -> Recebendo pacote: 1236 bytes, index: 1271516
13:57:34.256 -> E (46096) bootloader_mmap: tried to bootloader_mmap twice
13:57:34.256 -> E (46096) esp_image: bootloader_mmap(0x170020, 0x3db40) failed
13:57:34.256 -> Could Not Activate The Firmware

This is my code for ota upload:

server.on("/update", HTTP_GET, [](AsyncWebServerRequest *request) {
   
    request->send(200, "text/html", "<!DOCTYPE html>"
                                      "<html>"
                                      "<head>"
                                      "<title>ESP32 OTA Update</title>"
                                      "</head>"
                                      "<body>"
                                      "<h1>Atualizacao de Firmware OTA - Atual 7.1.0</h1>"
                                      "<form method='POST' action='/uploadapp' enctype='multipart/form-data'>"
                                      "<input type='file' name='update'><br><br>"
                                      "<input type='submit' value='Upload'>"
                                      "</form>"
                                      "</body>"
                                      "</html>");
  });


  // Rota para o upload do firmware
  server.on("/uploadapp", HTTP_POST, [](AsyncWebServerRequest *request) {
    vTaskDelete(looptask);
    server.end();
  }, [](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final) {
    static uint8_t* otaBuffer = NULL;
    static size_t otaBufferSize = 10240; // Aumente o tamanho do buffer
    if (!index) {
      Serial.println("Iniciando atualização");
      otaBuffer = (uint8_t*) malloc(otaBufferSize);  // Alocar buffer na heap
      if (!otaBuffer) {
        Serial.println("Erro ao alocar memória para o buffer OTA");
        return request->send(500, "text/plain", "Erro ao alocar memória para o buffer");
      }
      if (!Update.begin(UPDATE_SIZE_UNKNOWN)) {
        Update.printError(Serial);
        free(otaBuffer);  // Liberar buffer da heap em caso de erro
        otaBuffer = NULL;
        return request->send(500, "text/plain", "Erro ao iniciar atualização");
      }
    }

    while (len > 0) {
      size_t toWrite = len > otaBufferSize ? otaBufferSize : len;
      memcpy(otaBuffer, data, toWrite);
      if (Update.write(otaBuffer, toWrite) != toWrite) {
        Update.printError(Serial);
        free(otaBuffer);  // Liberar buffer da heap em caso de erro
        otaBuffer = NULL;
        return request->send(500, "text/plain", "Erro ao escrever dados");
      }
      data += toWrite;
      len -= toWrite;
      Serial.printf("Recebendo pacote: %d bytes, index: %d\n", toWrite, index);
    }

    if (final) {
      if (Update.end(true)) {
        Serial.println("Atualização concluída com sucesso");
        request->send(200, "text/plain", "Atualizacao concluida com sucesso");
        delay(1000);
        ESP.restart();
      } else {
        Update.printError(Serial);
        request->send(500, "text/plain", "Erro ao finalizar atualizacao");
      }
      free(otaBuffer);  // Liberar buffer da heap após conclusão
      otaBuffer = NULL;
    }
  });

Any Ideas?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.