Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By GastonMelo
#96501 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");
      ESP.restart();
    }, []() {
      HTTPUpload& upload = server.upload();
      if (upload.status == UPLOAD_FILE_START) {
        Serial.setDebugOutput(true);
        WiFiUDP::stopAll();
        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
          Update.printError(Serial);
        }
      } else if (upload.status == UPLOAD_FILE_WRITE) {
        if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
          Update.printError(Serial);
        }
      } 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 {
          Update.printError(Serial);
        }
        Serial.setDebugOutput(false);
      }
      yield();
    });


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.
Thanks
Gastón
User avatar
By QuickFix
#96504 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)