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

#1

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.
#2

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}']
#3

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.

#4

fstab will take care of everything.

The latest release is 2.14.0.

You can pin to the version you need.

#5

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.
image
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

#6

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.

#7

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