4 Replies Latest reply on Sep 3, 2015 1:25 PM by Intel_Peter

    Saving data to file - Help needed

    KimLorentz

      I am programming an Intel Edison using Sparkfun boards and want to save data to a file on the SD card connected to the Edison.

      13041-01.jpg13034-03.jpg1231-00.jpg

      I am programing in Java Script(.js) using Intel XDK IoT Edition.

      Does anyone know how to save data I get from the i2c (ADXL345 Triple-Axis Accelerometer) to an Excel file?

       

      UTDATE:

       

      This is my current code and I get data from the ADXL345

       

      // Load accelerometer
      var adxl345 = require('jsupm_adxl345');
      
      
      // Instantiate on I2C bus
      var adxl = new adxl345.Adxl345(1); // I2C bus 1.
      
      
      setInterval(function()
      {
          adxl.update(); // Update the data
          var raw = adxl.getRawValues(); // Read raw sensor data
          var force = adxl.getAcceleration(); // Read acceleration force (g)
          var rawvalues = raw.getitem(0) + " " + raw.getitem(1) + " " + raw.getitem(2);
          //console.log("Raw Values: " + rawvalues);
          console.log("ForceX: " + force.getitem(0).toFixed(2) + " g","   ", "ForceY: " + force.getitem(1).toFixed(2) + " g","   ", "ForceZ: " + force.getitem(2).toFixed(2) + " g");
      }, 100);
      
        • 1. Re: Saving data to file - Help needed
          Intel_Peter

          Hello KimLorentz,

           

          You could try creating a .xls file with How to use fs.createWriteStream?. I created a quick example with UART. It receives serial data and then writes it on a .xls file. This is the simple code that I wrote:

           

          var m = require('mraa');
          var x;
          var y = 0;
          var fs = require('fs');
          var ws = fs.createWriteStream('uart_to_xls.xls');
          u = new m.Uart(0);
          u.setBaudRate(9600);
          console.log("Test for reading UART");
          while(y < 10)
          {
            x = u.readStr(12);
            ws.write(x);
            y++;
          }
          ws.end();
          
          

           

          I tested it with another board which had an Arduino sketch with a modified version of the blink example. This is the sketch's code:

           

          void setup()
          {
            Serial.begin(9600);
            pinMode(13, OUTPUT);
          }
          
          void loop() {
            digitalWrite(13, HIGH);
            Serial.println("LED on High");
            delay(1000);
            digitalWrite(13, LOW);
            Serial.println("LED on Low");
            delay(1000);
          }
          
          

           

          As you can see both codes are very simple, the JavaScript code is a little bit more complex however all the info to create this example I got it from the link I posted above and the MRAA documentation. I hope this information helps you. You can take the example as a start and modify it in order to collect your data and add it to your .xls file.

           

          Peter.

          • 2. Re: Saving data to file - Help needed
            KimLorentz

            Thank you for the hints for my project.

            I got the code working and sharing it under here for everybody to use

             

            Most of my time I used on File System Node.js v0.12.7 Manual & Documentation.

            All you need can you find there.

            Have fun using the code everybody.

             

            // Load accelerometer
            var adxl345 = require('jsupm_adxl345');
            
            
            // Instantiate on I2C bus
            var adxl = new adxl345.Adxl345(1); // I2C bus 1.
            
            
            // Create .txt file
            var fs = require('fs');
            var ws = fs.createWriteStream('/media/sdcard/adxl345data.txt');
            
            
            setInterval(function()
            {
                adxl.update(); // Update the data
                var raw = adxl.getRawValues(); // Read raw sensor data
                var force = adxl.getAcceleration(); // Read acceleration force (g)
                var rawvalues = raw.getitem(0) + " " + raw.getitem(1) + " " + raw.getitem(2);
                ws.write("X: ");
                ws.write(force.getitem(0).toFixed(2)); ws.write(", ");
                ws.write("Y: ");
                ws.write(force.getitem(1).toFixed(2)); ws.write(", ");
                ws.write("Z: ");
                ws.write(force.getitem(2).toFixed(2)); ws.write('\n');
                //console.log("ForceX: " + force.getitem(0).toFixed(2) + " g","   ", "ForceY: " + force.getitem(1).toFixed(2) + " g","   ", "ForceZ: " + force.getitem(2).toFixed(2) + " g" + '\n');
            }, 100);
            
            • 3. Re: Saving data to file - Help needed
              KimLorentz

              Next parts is to make it create a new file everytime the Edison starts and leve the old file alone.

               

              Got any hints?

               

              Here is what I want the file name to be.

               

              Date:                29082015

              Log number:     _1

               

              The file will be 29082015_1.txt and if you restart the Edison the same day the file will be 29082015_2.txt and if you start the next day 30082015_1.txt

              • 4. Re: Saving data to file - Help needed
                Intel_Peter

                This one is a bit trickier. You might want to  try with the example in NewFilePerDate.js. It creates a new file by date and changes its "_X" termination. Take a look at it, you might find it helpful.

                 

                Peter.