Search code examples
arduinoesp8266arduino-esp8266platformio

Arduino Esp8266 LittleFS upload for updating


On my ESP8266 I run a small Webserver. On one page I am able to upload a binary file for the firmware and hit update. The Esp8266 updates its code and makes a reboot. After this the new code is run. So far so good. In my webserver on the ESP8266 I have some files to provide like index.html and some javascript files. I packed these files in the data directory and created a LittleFS partition. I can make changes in Platformio and upload the littleFS.bin and after a reboot the new files are served.

Now I would like to upload the littleFS.bin also to the ESP8266 an make an update via the website. But this fails.

Here is some code I tried but I get Error messages all the time.

  server.on("/updatespiffs", HTTP_POST, []() {
    server.sendHeader("Connection", "close");
    server.send(200, "text/plain", (Update.hasError()) ? "spiffsFAIL" : "spiffsOK");
    delay(5000);
    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());
      LittleFS.end();

      uint32_t maxSketchSpace = 1300000;
      if (!Update.begin(maxSketchSpace, U_FS)) { //start with max available size
        Update.printError(Serial);
      }
    } else if (upload.status == UPLOAD_FILE_WRITE) {
      uint32_t xy = Update.write(upload.buf, upload.currentSize);
      if (xy != 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);
    }

It complains about ERROR[4]: Not Enough Space

Has anyone implemented something already? Thank you Niki


Solution

  • Try to replace

    uint32_t maxSketchSpace = 1300000;
    

    with

    uint32_t maxSketchSpace = ((size_t) &_FS_end - (size_t) &_FS_start);