Unable to create a SPIFFS file on the Sysflash on nodemcu ESP8266

I am using mgos_vfs_open function to create a SPIFFS file in the internal flash.
my statement in my code is as follows
int filehandle = mgos_vfs_open("/mnt/appstate",4, 28);
the argument values of 4 and 28 are the ‘int flags’ and ‘int mode’ arguments on the function definition. I suspect these values are incorrect because the filehandle returned in -1 ( error)
Can some one help me put the right args or suggest another way to create a file in the internal flash in which I want to store some status variables to protect against loss of wifi.
I have followed the direction given in the HOWTOs and have executed the

  1. mos call FS.Mkfs command
  2. mos call FS.Mount command
    Both of these execute successfully and return "null’
    I have in addition inserted the following in the config section of the mos.yml file to ensure the fiesystem is mounted on boot.
  • [“sys.mount.path”,“s”,"/mnt",{title: “path of the file”}]
  • [“sys.mount.dev_type”,“s”,“sysflash”,{title: “file located in system flash”}]
  • [“sys.mount.fs_type”,“s”,“SPIFFS”,{title: “Kind of file system”}]
  • [“sys.mount.fs_opts”,“s”,"{“addr”:3145728, “size”: 524288}",{title: “Size and location of file system”}]
    Look forward to a response . Thanks in advance.

Once a filesystem is created and mounted, you can use the standard C file functions (fopen, fread, etc).

BTW, sys.mount.* is deprecated. Use fstab instead.
Ex with this ammendment

- ["devtab.dev0.opts", '{"dev": "sfl0", "offset": 0x300000, "size": 0x80000}']

Thanks for your fast response. Will try this out. Do I still need to issue the
‘mos call FS.Mkfs’ command as before and as shown in the HOWTOs or will fstab do this for me.
And how can I revert to ver 2.12.2 from the latest 2.13 which seems to have some issues.

fstab will take care of everything.

The latest release is 2.14.0.

You can pin to the version you need.

Thanks for your help. I was able to build with fstab and run.using ver 2.13.0 after incorporating fopen / fwrite as suggested by you.
However, I have a question. The file created named “appstate” appears to have been created in the same folder as other files such as conf0.json, conf9.json etc. See screen shot below.
I expected my file to be created in a separate area in a folder called /fld. When I tried to list /fld it was not populated.
I must be doing something wrong. I am reproducing below my “mos.yml” config entries.

  • [“devtab.dev0.name”, “data”]
  • [“devtab.dev0.type”, “part”]
  • [“devtab.dev0.opts”, ‘{“dev”: “sfl0”, “offset”: 0x300000, “size”: 0x80000}’]
  • [“fstab.fs0.dev”, “data”]
  • [“fstab.fs0.type”, “SPIFFS”]
  • [“fstab.fs0.opts”, ‘{“bs”: 4096, “ps”: 128, “es”: 4096}’]
  • [“fstab.fs0.path”, “/fld”]
  • [“fstab.fs0.create”, true]

Please clarify the relationship between “data” and “/fld” above and how this relates with the filename “appstate” mentioned in the fopen statement in my code below.

fp = fopen (“appstate”, “a”);
if ( fp == NULL)
LOG(LL_INFO, (“FilePointer returned is NULL”));
ssize_t byteswritten= fwrite(&out,1,len,fp);
LOG(LL_INFO, ( “No of bytes written to file = %d”, (int) byteswritten));

Thanks in advance for your guidance and response

Did you read this?

The first parameter of fopen is the file’s path, relative or absolute.
fopen ("appstate", "a") opens the file appstate in the current working directory which is the root filesystem created by default.

@nliviu thanks very much for your help.I have now been able to create and use SPIFFS on the ESP8266 internal flash using fstab.

Hi I trying to build a SPIFFS on esp8266 using the fstab, but the mount fails

The mos.yml:

  - ["devtab.dev0.name", "s", "mydevice", {title: "Device name"}]
  - ["devtab.dev0.type", "s", "sysflash", {title: "Device type"}]
  - ["devtab.dev0.opts", "s", '{"addr": 3145728, "size": 524288}', {title: "Device options"}]
  - ["fstab.fs0.dev", "s", "mydevice", {title: "Device name"}]
  - ["fstab.fs0.type", "s", "SPIFFS", {title: "FS type"}]
  - ["fstab.fs0.opts", "s", "", {title: "FS options"}]
  - ["fstab.fs0.path", "s", "/fs", {title: "Mount point"}]

The error:

[Oct  1 16:57:21.730] esp_main.c:172          mongoose 1.0 (20191001-195654)
[Oct  1 16:57:21.730] esp_main.c:174          Mongoose OS 2.15.0 (20191001-195654/2.15.0-g787ac38)
[Oct  1 16:57:21.730] esp_main.c:178          CPU: ESP8266EX, 160 MHz, RAM: 51952 total, 49748 free
[Oct  1 16:57:21.730] esp_main.c:180          SDK 2.2.1(1247cc5); flash: 4M
[Oct  1 16:57:21.730] esp_exc.c:211           Reset cause: 4 (soft reset)
[Oct  1 16:57:21.730] mg_lwip_ev_mgr.c:93     Mongoose 6.15, LwIP 1.4.1
[Oct  1 16:57:21.730] mg_ssl_if_mbedtls.c:57  mbed TLS 2.16.0-cesanta4
[Oct  1 16:57:21.730] mgos_vfs_dev.c:73       sfl0: sysflash (), size 4194304
[Oct  1 16:57:21.730] mgos_vfs_dev.c:73       root: part ({"dev": "sfl0", "offset": 32768, "size": 262144}), size 262144
[Oct  1 16:57:21.730] mgos_vfs.c:147          /: SPIFFS @ root, opts 
[Oct  1 16:57:21.762] mgos_vfs.c:320          /: size 233681, used: 28614, free: 205067
[Oct  1 16:57:21.931] mgos_sys_config.c:368   MAC: a2:20:a6:02:70:a2
[Oct  1 16:57:21.931] mgos_sys_config.c:376   WDT: 30 seconds
[Oct  1 16:57:21.931] mgos_i2c_gpio_maste:250 I2C GPIO init ok (SDA: 4, SCL: 5, freq: 100000)
[Oct  1 16:57:21.931] mgos_vfs_dev.c:73       mydevice: sysflash ({"addr": 3145728, "size": 524288}), size 4194304
[Oct  1 16:57:21.931] mgos_vfs.c:147          /fs: SPIFFS @ mydevice, opts 
[Oct  1 16:57:21.931] mgos_vfs_fs_spiffs.:155 addr 0x0 size 4194304 bs 4096 ps 256 es 4096 nfd 10 encr 0 => -10025
[Oct  1 16:57:21.931] mgos_vfs_fs_spiffs.:203 SPIFFS mount failed
[Oct  1 16:57:21.931] mgos_vfs.c:155          FS SPIFFS : mount failed
[Oct  1 16:57:21.931] mgos_deps_init.c:116    fstab init failed
[Oct  1 16:57:21.931] esp_main.c:186          MG init error: -32
[Oct  1 16:57:21.931] esp_main.c:200          Init failed: -32
[Oct  1 16:57:22.429] mgos_vfs.c:990          Unmounting filesystems

I missing something?
I see some example that sets the fstab.fs0.opts bs, and cs, but I don’t know what these are.
I also not seting FS_SIZE in buildvars. I read that are deprecated.

Any help to understand this I appreciate.

This works:

         - ["devtab.dev0.name", "data"]
         - ["devtab.dev0.type", "part"]
         - ["devtab.dev0.opts", '{"dev": "sfl0", "offset": 0x300000, "size": 0x80000}']
         - ["fstab.fs0.dev", "data"]
         - ["fstab.fs0.type", "SPIFFS"]
         - ["fstab.fs0.opts", '{"bs": 4096, "ps": 128, "es": 4096}']
         - ["fstab.fs0.path", "/fs"]
         - ["fstab.fs0.create", true]

You also need vfs-dev-part in mos.yml

Thanks @nliviu
This works:

  - ["devtab.dev0.name", "mydevice"]
  - ["devtab.dev0.type", "part"]
  - ["devtab.dev0.opts", '{"dev": "sfl0","addr": 0x300000, "size": 0x80000}']
  - ["fstab.fs0.dev", "mydevice"]
  - ["fstab.fs0.type", "SPIFFS"]
  - ["fstab.fs0.opts", '']
  - ["fstab.fs0.path", "/fs"]
  - ["fstab.fs0.create", true]

I didn’t add vfs-dev-part, maybe it is deps in someother lib

But I still do not understand some of this code:

  • why devtab0.type is part and not sysflash
  • why I need to specify dev in dev0.opts, and from where I know its name? (sfl0 in this case)
  • why the files in fs folder from my project isn’t loading to filesystem?
  • what is bs, ps and es?

I didn’t find the answer in the docs, so any help finding this documentation is welcome.

Yes, vfs-dev-part is added by core for ESP8266.

devtab0.type is part because you use a part of the sysflash https://github.com/mongoose-os-libs/vfs-dev-part

Since you are using a part of the system flash, you need to define the base device (sfl0 in this case).

The files from your project’s fs dir go to the root filesystem created by default by the build process.

bs=block size, ps=page size, es=erase size

PS. I don’t understand how it works for you with "addr": 0x300000 instead of "offset": 0x300000. My Wemos D1 minis do not boot with this configuration…

1 Like

Thanks nliviu!! that explain a lot!