Flash memory data erase


If you are asking a question, please follow this template:

  1. My goal is: don’t wants tp erase previous data while uploading new code
  2. My actions are: I store the data in esp32 flash memory but when I manually upload new code via USB Cable then the previous data is erase but if we are giving OTA then flash memory does not erase the data.is there any method to which doesn’t erase the flash memory while uploading the code via USB.
  3. The result I see is: when I upload new code via USB then data of flash memory will store as it is.
  4. My expectation & question is: is there any partition systems which store the previous data in flash.

When flashing a new firmware the expected behavior is to write the application and the filesystem in the first slot.

When doing OTA update, the new firmware (application and filesystem) is written in the available slot and upon boot the files from the previous filesystem are copied over in the new filesystem.

If you need to preserve some files after flashing a new version, you can create an extra filesystem and save the data there.


thank you so much…can you provide me any example so I can understand it better.


A simple example to send the log files to the extra partition.
Clone the empty app and modify mos.yml

  - ["debug.level", 2]
  # fstab options
  - ["fstab.fs0.opts", '{"bs": 4096, "es": 4096}']
  - ["fstab.fs0.path", "/ext"]
  - ["fstab.fs0.create", true] # use this line only when first flashing the firmware
  #- ["fstab.fs0.created", true] # use this line after the first flash of the firmware
  - ["file_logger.enable", true]
  - ["file_logger.dir", "/ext"]

  - origin: https://github.com/mongoose-os-libs/file-logger
  - origin: https://github.com/mongoose-os-libs/fstab
  - origin: https://github.com/mongoose-os-libs/ota-http-server
  - origin: https://github.com/mongoose-os-libs/rpc-service-config
  - origin: https://github.com/mongoose-os-libs/rpc-service-fs
  - origin: https://github.com/mongoose-os-libs/rpc-uart
  - origin: https://github.com/mongoose-os-libs/rpc-ws
  - origin: https://github.com/mongoose-os-libs/sntp
  - origin: https://github.com/mongoose-os-libs/vfs-fs-spiffs
  - origin: https://github.com/mongoose-os-libs/wifi

  - when: mos.platform == "esp32"
      # esp32 partition
        - ["fstab.fs0.dev", "fs_ext"]
        - ["fstab.fs0.type", "SPIFFS"]
        ESP_IDF_EXTRA_PARTITION: fs_ext,data,spiffs,,256K
  - when: mos.platform == "esp8266"
        - origin: https://github.com/mongoose-os-libs/vfs-dev-part
        - ["devtab.dev0.name", "data"]
        - ["devtab.dev0.type", "part"]
        - ["devtab.dev0.opts", '{"dev": "sfl0", "offset": 0x300000, "size": 0x40000}']
        - ["fstab.fs0.dev", "data"]
        - ["fstab.fs0.type", "SPIFFS"]
        - ["fstab.fs0.opts", '{"bs": 4096, "es": 4096}']
        - ["fstab.fs0.path", "/ext"]
mos ls -l ext
log_000-19700101-000115.log 11335
log_001-19700101-000001.log 3933
log_002-20210224-131209.log 3978
log_003-20210224-131624.log 3978
log_004-20210224-132039.log 2107

thank you so much @nliviu


#include “mgos.h”
static void timer_cb()
char filename1[40] = “/ext/append.csv”;
f = fopen(filename1, “a”);
fprintf(f, “1,2,3,4,5,6,7\n”);
LOG(LL_INFO, ("%s",“Reading file”));
f = fopen(filename1, “r”);
if (f == NULL)
LOG(LL_INFO, ("%s",“Failed to open file for reading”));
char line[64];
fgets(line, sizeof(line), f);
pos = strchr(line, ‘\n’);
if (pos)
*pos = ‘\0’;
LOG(LL_INFO, (“Read from file: %s”, line));
enum mgos_app_init_result mgos_app_init(void)
mgos_set_timer(100 , true, timer_cb, NULL);

this is my code still my flash memory doesn’t store the previous data when the new code flash


Use ["fstab.fs0.created", true] when you build the firmware after the first flash and comment out ["fstab.fs0.create", true].


thank you…now its work for me


Of course it works. You didn’t read these notes

  - ["fstab.fs0.create", true] # use this line only when first flashing the firmware
  #- ["fstab.fs0.created", true] # use this line after the first flash of the firmware