ESP32 "fs" examples not working as expected.

Posted on
  • My Goal is to be able to use the ESP32 to serve a React.js or Vue.js single page app. Then use WS to communicate to back to the ESP32. The SPA file will eventually be over 2 Megs. But for now I just want to serve a html file out of the flash file system using the FAT protocol.

    When trying to use example code from the espruin0.com/ESP32 page, none of the following works as is shown in the example.

    console.log(require( "Flash").getFree());
    var fs = require("fs");
    if ( typeof(fs.readdirSync()) === 'undefined' ) {

    console.log('Formatting FS');
    E.flashFatFS({ format: true });
    

    }
    fs.writeFileSync("hello.txt", "Hello World");
    console.log(fs.readFileSync("hello.txt")­); // prints "Hello World"
    fs.appendFileSync("hello.txt", "!!!");
    console.log(fs.readFileSync("hello.txt")­); // prints "Hello World!!!"

    The results I get are:
    For the "console.log(require( "Flash").getFree());" command:

    [
    { "addr": 1114112, "length": 3080191 }
    ]
    For the rest of the code:
    Uncaught Error: Unable to mount media : NO_FILESYSTEM
    at line 6 col 28
    if ( typeof(fs.readdirSync()) === 'undefined' ) {

    I am relatively new so there is a strong chance I am doing something wrong.

  • @user80339,

    " none of the following works as is shown in the example. "

    It appears the snippet is reading and writing to a file as expected, but the snippet is not identical to the code example at http://www.espruino.com/ESP32

    Would you please post your code in it's entirety as one legible code block so that we may determine the differences between that shown in the above page and that in the snippet that is proving difficult.

  • Thank you for the response.
    Here's the code in it's entirety:

    print("Before getFree()");
    console.log(require( "Flash").getFree());
    print("After getFree()");
    
    var fs = require("fs");
    if ( typeof(fs.readdirSync()) === 'undefined' ) {
      console.log('Formatting FS');
      E.flashFatFS({ format: true });
    }
    fs.writeFileSync("hello.txt", "Hello World");
    console.log(fs.readFileSync("hello.txt")­); // prints "Hello World"
    fs.appendFileSync("hello.txt", "!!!");
    console.log(fs.readFileSync("hello.txt")­); // prints "Hello World!!!"
    

    I put a couple of print statements in to delineate where the memory table should be printed.
    The output from the code above is:
    Before getFree()
    [
    { "addr": 1114112, "length": 3080191 }
    ]
    After getFree()
    Uncaught Error: Unable to mount media : NO_FILESYSTEM
    at line 6 col 28
    if ( typeof(fs.readdirSync()) === 'undefined' ) {

  • Thank you for posting a more legible sample. Allows for easier cut-n-paste

    I originally thought I spotted a case typo, but double checking the online reference proved otherwise.

    Anyone else able to chime in?

    Are lines 11 and 13 actually printing what is shown as a comment or are the comments leftover from the sample?
    Do you get the same error when commenting out lines 2 and 6,7,8,9
    What is the current version of Espruino?

  • Hi @user80339,
    The FileSystem library fs was updated to throw errors, so that these could be caught (It used to just output a warning when the docs were written)

    I have updated the docs, your code needs to look like this:

    try {
      fs.readdirSync();
     } catch (e) { //'Uncaught Error: Unable to mount media : NO_FILESYSTEM'
      console.log('Formatting FS - only need to do once');
      E.flashFatFS({ format: true });
    }
    fs.writeFileSync("hello.txt", "Hello World");
    console.log(fs.readFileSync("hello.txt")­); // prints "Hello World"
    fs.appendFileSync("hello.txt", "!!!");
    console.log(fs.readFileSync("hello.txt")­); // prints "Hello World!!!"
    

    If the filesystem is attempted to be read and can't, then the filesystem is created.

    Please note that this only needs to occur once. If the flash memory is erased, them you will need to format again.

    The try/catch above prevents E.flashFatFS({ format: true }); from erasing your files.

  • @Robin, Thank you for the feedback. You did catch a typo. I found and changed it while redoing my post.
    @Wilberforce, Thank you for the solution and the explaination. Works like a champ now. I might be back soon when I try to reflash the firmware to implement the SD card.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

ESP32 "fs" examples not working as expected.

Posted by Avatar for miCatotonic @miCatotonic

Actions