Page 1 of 1

WebUpdate sketch and server.on paramters explanation

PostPosted: Wed May 31, 2023 4:53 pm
by GastonMelo
Hi to all, Im trying to understand the code used in the WebUpdate sketch (Im using arduino 1.18.19). In that code you will find this lines:
Code: Select allserver.on("/update", HTTP_POST, []() {
      server.sendHeader("Connection", "close");
      server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
    }, []() {
      HTTPUpload& upload = server.upload();
      if (upload.status == UPLOAD_FILE_START) {
        Serial.printf("Update: %s\n", upload.filename.c_str());
        uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
        if (!Update.begin(maxSketchSpace)) { //start with max available size
      } else if (upload.status == UPLOAD_FILE_WRITE) {
        if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
      } else if (upload.status == UPLOAD_FILE_END) {
        if (Update.end(true)) { //true to set the size to the current progress
          Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
        } else {

I don't understand what this flags needs to be checked :

Code: Select allupload.status == UPLOAD_FILE_START
upload.status == UPLOAD_FILE_WRITE
upload.status == UPLOAD_FILE_END

I don't understand what those lines do in the code. Why do I need to check those flags? If I upload the file the data will be stored in server.upload(). Why do I need to ask about those flags?
I guess this is more a question regarding how a server works and not related to the program, but tried to find information about this and I can't find 'till now anything similar to what I found in this code.
If any of you can point to some reference, will be appreciated.
I guess I just want to understand each part of the code to get the big picture on how this code woks and not just cut and paste the code.

Re: WebUpdate sketch and server.on paramters explanation

PostPosted: Thu Jun 01, 2023 2:39 am
by QuickFix
Data transfer is done is smaller pieces (which is common to all TCP/IP traffic, not only on an ESP/Arduino):
  1. Start data transfer(UPLOAD_FILE_START)
  2. Send x Bytes of data (UPLOAD_FILE_WRITE)
  3. Repeat step 2 until all bytes are sent
  4. Close data transfer (UPLOAD_FILE_END)

Re: WebUpdate sketch and server.on paramters explanation

PostPosted: Thu Jun 01, 2023 5:10 am
by GastonMelo
Thanks for your response!! Thanks