• What I would like to achieve is to store settings into a config file preferably in json format into a config file of some sort(maybe settings.js?) Which will then get loaded and updated/saved onto a sdcard.

    Does anyone have an example that I can work from?
    I managed to get this far. However, I am stuck at how to pipe the data to the console screen. If possible, I would like to eventually pipe the file data to a serial port and also pipe data from the serial port to the file located on the sdcard.

    function re() {   var f = E.openFile("settings.js","r"); 
        if (f !== undefined) {
        f.pipe(console.log(f)); //<--Need to stream to console and serial port. 
        } else {
        console.log("What?"); 
        }
     }
    
    

    At the moment, the console is only receiving: {}. Inside the settings.js file I have:

    {"sensors":[{"phHigh":60},{"phLow":58},{­"ecHigh":800},{"ecLow":700},{"tempHigh":­90},{"tempLow":50}]}
    
    

    The console.log should output the contents of settings.js, but instead console.log only outputs { }.

  • Sounds like you might be trying to do two different things?

    For settings, presumably you can load and save the whole file in one go? so:

    var mySettings = { "foo" : "bar" };
    require('fs').writeFile("settings.js",JS­ON.stringify(mySettings));
    mySettings = JSON.parse(require('fs').readFile("setti­ngs.js"));
    // or to write to the console
    console.log(require('fs').readFile("sett­ings.js"));
    

    If the file is so big that it won't fit in memory and you need to pipe it, you need to pipe to the actual serial device rather than the function console.log:

    function re() {   var f = E.openFile("settings.js","r"); 
        if (f !== undefined) {
        f.pipe(USB); // or 'Serial1'/etc
        } else {
        console.log("What?"); 
        }
     }
    
  • Thanks. I will give that code a go when I get home from work.

  • @Gordon piping the data to Serial1 worked. I can now view the settings and sensor data in real time by piping it out to my Wifi module then out to my TCP server on the internet.

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

create a config.js file to store setting values in json format?

Posted by Avatar for d0773d @d0773d

Actions