ESP32 Update from SPIFF fails with large bin file

I have two files: helloworld.bin and firmwareLarge.bin. The first one is small and all it does - prints "Hello world" every second. The second one has wi-fi client, BLE and other stuff. The code below works ok with helloworld.bin, but fails on firmwareLarge.bin.
Please note that firmwareLarge.bin file's size in SPIFF is smaller than the actual file size (http client) for some reason. Are there some size limitations? Also, why if I restart ESP32 and try to read small "helloworld.bin", its size is 81920 and not actually saved 208288? Does ESP32 compress it or what?

The code I'm using.

#include <WiFi.h>
#include <HTTPClient.h>
#include <Arduino.h>
#include "FS.h"
#include "SPIFFS.h"
#include <HTTPUpdate.h>
#include <Update.h>

#define FORMAT_SPIFFS_IF_FAILED true

char *ssid = "*****";
char *pass = "****";

void setup()
{
#pragma region SETUP

  Serial.begin(115200);

  if (!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED))
  {
    Serial.println("SPIFFS Mount Failed");
    return;
  }

  if (SPIFFS.remove("/firmware.bin"))
  {
    Serial.println("− file deleted");
  }
  else
  {
    Serial.println("− delete failed");
  }

  WiFi.begin(ssid, pass);
  int startTimeForConnection = millis();

  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
    if ((millis() - startTimeForConnection) > 30 * 1000)
    {
      Serial.println("");
      Serial.println("Cannot connect with such credentials or wifi is not available");
    }
  }

  Serial.println("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());

#pragma endregion

#pragma region DownloadSPIFF

  if (WiFi.status() == WL_CONNECTED)
  {
    Serial.println("Start calling");
    HTTPClient client;
    //client.begin("https://kwater.kelectronics.net/api/getfirmware/helloworld.bin");
    client.begin("https://kwater.kelectronics.net/api/getfirmware/firmwareLarge.bin");
    client.addHeader("Content-Type", "application/json");

    int httpResponseCode = client.GET();

    if (httpResponseCode == 200)
    {
      Serial.println("200");
      int len = client.getSize();
      Serial.printf("FW Size: %u\n", len);

      uint16_t sizeOfBuffer = 4096;
      uint8_t buff[sizeOfBuffer] = {0};
      WiFiClient *stream = client.getStreamPtr();

      Serial.println("Download firmware...");

      File fileWrite = SPIFFS.open("/firmware.bin", "w");
      ulong numberOfBytesWritten = 0;

      if (!fileWrite)
      {
        Serial.println("Error opening file for writing");
        return;
      }

      int initialProgress = len / sizeOfBuffer + 1;
      int progress = initialProgress;

      while (client.connected() && (len > 0 || len == -1))
      {
        size_t size = stream->available();
        if (size)
        {
          int c = stream->readBytes(buff, ((size > sizeof(buff)) ? sizeof(buff) : size));

          for (int i = 0; i < ((size > sizeof(buff)) ? sizeof(buff) : size); i++)
          {
            fileWrite.write((uint16_t)buff[i]);
            numberOfBytesWritten++;
          }

          progress--;
          double progressPercent = static_cast<double>(progress) / initialProgress * 100;
          Serial.print(progressPercent);
          Serial.print("% ");

          if (len > 0)
          {
            len -= c;
          }
        }
        delay(1);
      }

      fileWrite.close();
      Serial.println();
      Serial.println("DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!");
      Serial.println("DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!");
      Serial.println("DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!");
      Serial.print("Written=");
      Serial.print(numberOfBytesWritten);
      Serial.println();
    }
    else
    {
      // Serial.println(httpResponseCode);
    }
  }
  else
  {
    Serial.println("Not connected");
  }

#pragma endregion

#pragma region UpdateFirmware

  File fileUpdate = SPIFFS.open("/firmware.bin");

  if (!fileUpdate)
  {
    Serial.println("Failed to open file for reading");
    return;
  }

  Serial.println("Starting update..");

  size_t fileSize = fileUpdate.size();

  Serial.print("fileSize=");
  Serial.print(fileSize);
  Serial.println("");

  if (!Update.begin(fileSize))
  {
    Serial.println("Cannot do the update");
    return;
  };

  Serial.println("Update.writeStream start");
  Update.writeStream(fileUpdate);
  Serial.println("Update.writeStream end");

  if (Update.end())
  {
    Serial.println("Successful update");
  }
  else
  {
    Serial.println("Error Occurred: " + String(Update.getError()));
    return;
  }

  fileUpdate.close();

  Serial.println("Reset in 4 seconds...");
  delay(4000);

  ESP.restart();

#pragma endregion
}

void loop()
{
}

helloworld.bin works fine

.......Connected to WiFi network with IP Address: 
192.168.2.58
Start calling
200
FW Size: 208288
Download firmware...
98.04% 96.08% 94.12% ............ 1.96% 0.00% -1.96% 
DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!
DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!
DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!
Written=208288
Starting update..
fileSize=208288
Update.writeStream start
Update.writeStream end
Successful update
Reset in 4 seconds...
ets Jun  8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:2
load:0x3fff0030,len:1184
load:0x40078000,len:12784
load:0x40080400,len:3032
entry 0x400805e4
Hello world
Hello world
Hello world
Hello world

firmwareLarge.bin fails (not all bytes are there)

....Connected to WiFi network with IP Address: 
192.168.2.58
Start calling
200
FW Size: 1592160
Download firmware...
99.74% 99.49% .......... 0.26% 0.00% -0.26% 
DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!
DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!
DownLoadFirmWareToSpiff Done!!!!!!!!!!!!!!!!!!!!!!!!!
Written=1592160
Starting update..
fileSize=1065984 ***////Why??? Why its not 1065984???***
Update.writeStream start
Update.writeStream end
E (656711) bootloader_mmap: spi_flash_mmap failed: 0x102
E (656712) esp_image: bootloader_mmap(0x2c6720, 0x20c000) failed
Error Occurred: 9

Ok. I got it. There is not enough space in SPIFFS.

  Serial.print("Done reading files. TotalSpace=");
  Serial.print(SPIFFS.totalBytes());
  Serial.print(" UsedSpace=");
  Serial.print(SPIFFS.usedBytes());
  Serial.print(" FreeSpace=");
  Serial.print(SPIFFS.totalBytes()-SPIFFS.usedBytes());

Done reading files. TotalSpace=1378241 UsedSpace=109436 FreeSpace=1268805

But still, why is "helloworld.bin" which size is 208288 when I write it to SPIFFs after restart is:
FILE: firmware.bin SIZE: 81920

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