ESP32 (arduino c++) - Upload file from web

242 Views Asked by At

I'm working with an ESP32 in the Arduino IDE. I'm using the WifiManager libraries. I need to load a .bin file from a web page using a button to select the file. After uploading the file, I need to read it and store it in a buffer to send it through UART

This is my code. The button code to select the file:

const char HTTP_UPDATE_Lora[] PROGMEM = "Cargar nuevo Firmware LoRa <br/><form method='POST' action='uLora' enctype='multipart/form-data' onchange=\"(function(el {document.getElementById('uploadhex').style.display = el.value=='' ? 'none' : 'initial';})(this)\"><input type='file' name='update LoRa' accept='.bin'><button id='uploadhex' type='submit' class='h D'>Update</button></form>";//<small><a href='http://192.168.4.1/update' target='_blank'>* Puede que no funcione dentro del portal cautivo. Abrir en el navegador http://192.168.4.1</a><small>";

const char R_updatedoneLora[] PROGMEM = "/uLora";

server->on(WM_G(R_updatedoneLora), HTTP_POST, std::bind(&WiFiManager::handleUpdatingLora, this));

The problem I'm having is that if the file size is larger than 1436 bytes, it seems like the reading buffer[1024] only reads the initial 1436 bytes and then overwrites the remaining bytes until the file is complete. In other words, I'm unable to obtain the original file. I need to know when and how the buffer is being loaded. While investigating the code, I came across the "WebServer" class, which includes the following structure:

typedef struct {
  HTTPUploadStatus status;
  String  filename;
  String  name;
  String  type;
  size_t  totalSize;    // file size
  size_t  currentSize;  // size of data currently in buf
  uint8_t buf[HTTP_UPLOAD_BUFLEN];
} HTTPUpload;

HTTPUpload& upload() { return *_currentUpload; }

This is the function in which I try to read the buffer:

void WiFiManager::handleUpdatingLora(){

    HTTPUpload& upload = server->upload();
    
    int app_size = upload.totalSize;
    size_t sizeq = upload.currentSize;
    int sizefCount=0;
    String tabla="";
    String str="";
    int sizef=0;
    
    String page = getHTTPHead(FPSTR(S_options)); // @token options
    
    page += FPSTR("<br> Handle Update Done ULora<br>");
    page += FPSTR("Global variable: ");
    page += (String(globalVariable));
    page += FPSTR("<br>");
    
    page += FPSTR("<br> Status");
    page += (String(upload.status));
    page += FPSTR("<br>");
    
    globalVariable++;
    
    for ( uint32_t i = 0; i < app_size; ) //1500
    {
        if (( app_size - i) >= ETX_OTA_DATA_MAX_SIZE){
          sizef = ETX_OTA_DATA_MAX_SIZE; //Manda 1024 caracteres
        }
        else{
          sizef = app_size - i; //sizef indica cuantos caracteres restan
        }
        page += FPSTR("<br>SizeF Count: ");
        page += (String(sizefCount));
        page += FPSTR(" <br>Total size: ");
        page += String(upload.totalSize);
        page += FPSTR(" <br>Current Size: ");
        page += String(upload.currentSize);
        page += FPSTR(" <br>sizef: ");
        page += (String(sizef));
        page += FPSTR(" <br>Buff Size: ");
        page += (String(sizeof(upload.buf))); //1436
        page += FPSTR(" <br>Desde: ");
        page += (String(sizefCount*1024));
        page += FPSTR(" <br>Hasta: ");
        page += (String(sizef+sizefCount*1024));
        
        page += FPSTR(" <br>buff: ");
        //for (size_t p = sizefCount*1024; p < sizef+sizefCount*1024-1; p++) {
        for (size_t p = 0; p < sizef; p++) {
          str += String(upload.buf[p], HEX);  // Convertir cada elemento a cadena hexadecimal y concatenar
          
          tabla+=String(p+sizefCount*1024);
          tabla+="->";
          tabla+=String(upload.buf[p], HEX);
          tabla+="<br>";
        }
        page += str;
        page += FPSTR("<br>");
        str="";
        page += FPSTR("***<br>");
        i += sizef;
        sizefCount++;
    }
    page += FPSTR("<br> Inicio Tabla <br>");
    page +=tabla;
    page +="<br>";
    server->send(200, FPSTR(HTTP_HEAD_CT), page);
}```

In this handleUpdatingLora() function, I created a webpage for debugging purposes since I don't have the UART available for debugging.
0

There are 0 best solutions below