• This project uses two files on the SD card to layout CSV file format for data logging and read analog inputs in engineering units.
    Mylog.cfg
    Mycalibrations.cal
    The following link lays out the object used to calibrate the analog inputs into engineering units. It was used to create the file “Mycalibrations.cal” on the SD card.
    http://forum.espruino.com/conversations/­286215/

    The following code was used to create the log configuration file:
    SaveLog.js

    var LogObj1={
      Fname:"xxx.csv",
      Interval:1000, //milliseconds between samples
      items:[
        // header is text at top of column in csv file
        // cmd source for data in each column
        // can be in a different order
        // and have more or fewer items
        {header:"N",cmd:"count"},
        {header:"Date",cmd:"DMY",Args:["-","-"]}­,
        {header:"Time",cmd:"HMS",Args:[":",":"]}­,
        {header:"A0",cmd:"AR",Args:["A0",2]},
        {header:"A0",cmd:"SAR",Args:["A0",2]},
        {header:"Board Temp C",cmd:"BTC",Args:[2]},
        {header:"Board Temp F",cmd:"BTF",Args:[2]}
      ],
    
      count:0, // Number of lines logged
      N:10 // stop logging after count>N
    }; //end LogObj1
    
    
    function saveObject(myobject) {
      var Q;
      var sss=JSON.stringify(myobject);
     if (Q===undefined) {
      Q = E.openFile("Mylog.cfg", "w");
      Q.write(sss);
      Q.close();
      Q = undefined;
     }//endif
    }//end doLog
    
    saveObject(LogObj1); 
    var a=require("fs").readFileSync("Mylog.cfg"­);
    console.log(a);
    console.log(' ');
    
    

    The left screen when the log configuration file is created:

    >echo(0);
    {"Fname":"xxx.csv","Interval":1000,"item­s":[{"header":"N","cmd":"count"},{"heade­r":"Date","cmd":"DMY","Args":["-","-"]},­{"header":"Time","cmd":"HMS","Args":[":"­,":"]},{"header":"A0","cmd":"AR","Args":­["A0",2]},{"header":"A0","cmd":"SAR","Ar­gs":["A0",2]},{"header":"Board Temp C","cmd":"BTC","Args":[2]},{"header":"Bo­ard Temp F","cmd":"BTF","Args":[2]}],"count":0,"N­":10}
     
    =undefined
    >
    
    

    The logging program. The test portion opens the calibration file and the configuration file and creates a calibration object and two instances of the configuration. The layout is identical (could be different) except that the filename to write, and logging interval in one is 1 second and the other is 3 seconds.
    LogCSV3.js
    The console output when the program executes:

    >echo(0);
    N,Date,Time,A0,Inlet psig,Board Temp C,Board Temp F
    N,Date,Time,A0,Inlet psig,Board Temp C,Board Temp F
    =undefined
    1,4-4-2016,21:27:21,0.79,79.96,34.43,94.­32
    2,4-4-2016,21:27:22,0.79,79.96,34.62,93.­93
    3,4-4-2016,21:27:23,0.79,80.06,34.46,93.­93
    1,4-4-2016,21:27:23,0.79,79.98,34.84,93.­93
    4,4-4-2016,21:27:24,0.79,79.96,34.40,94.­26
    5,4-4-2016,21:27:25,0.79,79.96,34.25,94.­32
    6,4-4-2016,21:27:26,0.79,80.01,34.62,93.­93
    2,4-4-2016,21:27:26,0.79,79.96,34.46,93.­93
    7,4-4-2016,21:27:27,0.79,79.98,34.62,93.­59
    8,4-4-2016,21:27:28,0.79,79.98,34.22,93.­93
    9,4-4-2016,21:27:29,0.79,79.96,34.62,94.­26
    3,4-4-2016,21:27:29,0.79,79.98,34.59,93.­93
    10,4-4-2016,21:27:30,0.79,80.01,34.62,93­.98
    4,4-4-2016,21:27:32,0.79,80.01,34.25,93.­93
    5,4-4-2016,21:27:35,0.79,80.03,34.84,93.­54
    6,4-4-2016,21:27:38,0.79,79.98,35.02,94.­26
    7,4-4-2016,21:27:41,0.79,80.01,35.24,94.­26
    8,4-4-2016,21:27:44,0.79,80.06,34.40,93.­93
    9,4-4-2016,21:27:47,0.79,79.98,34.62,94.­32
    10,4-4-2016,21:27:50,0.79,79.96,34.25,94­.26
    >
    
    

    The CSV files created are: xxx.csv and xx1.csv


    6 Attachments

  • Equipment: Espruino board, USB cable, WebIDE and SD card.
    This program makes use of three objects saved in local modules in a WebIDE project.
    The first object “CalObj” makes use of a file “Mycalibrations.cal” located on the SD card in the Espruino board.

    {"calibrations":[
    {"name":"Inlet","units":"psig","pin":"A0­","slope":100.13897386464,"Intercept":-0­.03419722673},
    {"name":"Outlet","units":"psig","pin":"A­1","slope":50,"Intercept":-25},
    {"name":"Motor","units":"Amps","pin":"A2­","slope":1,"Intercept":0},
    {"name":"Supply","units":"Volts","pin":"­A3","slope":1,"Intercept":0},
    {"name":"Flow","units":"Liters/minute","­pin":"A4","slope":1,"Intercept":0}
    ]}
    

    The following link lays out the object used to calibrate the analog inputs into engineering units. It was used to create the file “Mycalibrations.cal” on the SD card.
    http://forum.espruino.com/conversations/­¬286215/

    The second object “TimeObj.js” is used for its stopwatch function and ability to format date and time from a pattern stored in an array. See the following link.
    http://forum.espruino.com/conversations/­286424/

    The third object “LogObj.js” uses the previous two object modules and a “.cfg” file stored on the SD card. This program uses three different “.cfg” files an produces three different “.csv” files. The program used to create the “.cfg” files is “saveLogCfgXX1.js”
    One of the “.cfg” files looks like this:

    {"Fname":"xx1.csv",
    "Interval":1000,
    "items":[
    {"header":"N","cmd":"count"},
    {"header":"Date","cmd":"DATE","Args":["M­","-","D","-","Y"]},
    {"header":"Time","cmd":"TIME","Args":["H­",":","M",":","S"]},
    {"header":"A0","cmd":"SAR","Args":["A0",­2]},
    {"header":"A1","cmd":"SAR","Args":["A1",­2]},
    {"header":"A2","cmd":"AR","Args":["A2",2­]},
    {"header":"Board Temp C","cmd":"BTC","Args":[2]},
    {"header":"Board Temp F","cmd":"BTF","Args":[2]}
    ],
    "count":0,
    "N":10
    }
    
    

    The “Fname” is the name of the csv file to create.
    The “Interval” is the milliseconds between each data sample.
    The “Items” array contains records that set up the data to log to the csv file.
    The “Header” is the text at the top of a column in the csv file.
    The “cmd” can be “count”,“DATE”,“TIME”, “SAR”,”AR”,”BTC” or “BTF”
    Date and time commands use the information in the “Args” to format the data.
    AR is analog read without calibrations applied.
    SAR is a calibrated analog reading. For SAR the header information comes from the calibration file.
    For AR and SAR Args[0] is the pin name, and Args[1] is used in a toFixed(Args[1]) to format the data.
    The BTC and BTF are the board temperature in F or C units.
    Count is used to count and N is the number of samples to take

    Programs to load into Project:
    LogCSV6a.js, saveLogCfgXX1.js

    Modules to load into Modules:
    TimeObj.js, LogObj.js and CalObj.js
    Files on SD card in the Espruino:
    Mycalibrations.cal, Mylog1.cfg, Mylog2.dfg, and Mylog3.cfg
    Files created on the SD card by this program:
    Xx1.csv, xx2.csv, and xx3.csv.

    LogCSV6a.js listing:

    function ReadObjectFile(fname){
     return JSON.parse(require("fs").readFileSync(fn­ame));
    }//end ReadObjectFile
    
    
    
    function TestIt(){
     var T= new (require("TimeObj"))();
     T.updateTime();
    //start the stopwatch
     T.SWstart();
    // output day of the week
     console.log(T.getDay());
    
     Cal=new (require("CalObj"))(ReadObjectFile("Myca­librations.cal")); 
     Log=new (require("LogObj"))(ReadObjectFile("Mylo­g1.cfg"),Cal,T);
     Log1=new (require("LogObj"))(ReadObjectFile("Mylo­g2.cfg"),Cal,T);
     Log2=new (require("LogObj"))(ReadObjectFile("Mylo­g3.cfg"),Cal,T);
    
    //Changes to the onjects can be masde
    // Log1.A.Interval=3000;
    // Log1.A.Fname="xx1.csv";
    
    //start the logging 
     Log.doHeaders();
     Log.start();
    
     Log1.doHeaders();
     Log1.start();
    
     Log2.doHeaders();
     Log2.start();
    // output the elapsed time  
     console.log(T.SWstop().toFixed(3)," ms");
    }//end TestIt
    
    setBusyIndicator(LED1);
    TestIt();
    
    

    and the console output:

    >echo(0);
    Fri
    1680.916  ms
    =undefined
    xx1.csv Finished
    11005.221  ms
    xx2.csv Finished
    21572.389  ms
    xx3.csv Finished
    3202
    
    

    14 Attachments

  • That's really cool stuff. Did you monitor RAM usage (with process.memory())?

  • Trying it with process.memory().usage inserted.

    function ReadObjectFile(fname){
     return JSON.parse(require("fs").readFileSync(fn­ame));
    }//end ReadObjectFile
    
    
    
    function TestIt(){
    var j=0;  
     console.log(j," ",process.memory().usage);j++;
     var T= new (require("TimeObj"))();
     console.log(j," ",process.memory().usage);j++;
     T.updateTime();
    //start the stopwatch
     T.SWstart();
    // output day of the week
     console.log(T.getDay());
    
     Cal=new (require("CalObj"))(ReadObjectFile("Myca­librations.cal")); 
     console.log(j," ",process.memory().usage);j++;
     Log=new (require("LogObj"))(ReadObjectFile("Mylo­g1.cfg"),Cal,T);
     console.log(j," ",process.memory().usage);j++;
     Log1=new (require("LogObj"))(ReadObjectFile("Mylo­g2.cfg"),Cal,T);
     console.log(j," ",process.memory().usage);j++;
     Log2=new (require("LogObj"))(ReadObjectFile("Mylo­g3.cfg"),Cal,T);
     console.log(j," ",process.memory().usage);j++;
    
    //Changes to the onjects can be masde
    // Log1.A.Interval=3000;
    // Log1.A.Fname="xx1.csv";
    
    //start the logging 
     Log.doHeaders();
     Log.start();
     console.log(j," ",process.memory().usage);j++;
    
     Log1.doHeaders();
     Log1.start();
     console.log(j," ",process.memory().usage);j++;
    
     Log2.doHeaders();
     Log2.start();
    // output the elapsed time  
     console.log(T.SWstop().toFixed(3)," ms");
     console.log(j," ",process.memory().usage);j++;
    }//end TestIt
    
    setBusyIndicator(LED1);
    TestIt();
    
    

    The output:

    >echo(0);
    0   445
    1   453
    Sat
    2   571
    3   717
    4   827
    5   937
    6   958
    7   975
    418.646  ms
    8   997
    =undefined
    xx1.csv Finished
    10472.984  ms
    xx2.csv Finished
    20561.429  ms
    xx3.csv Finished
    30603.546  ms
    >console.log(process.memory().usage);
    957
    =undefined
    >
    
  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Data Logging Using a Configuration File and Calibrated Analog Inputs

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions