Intel® Makers
Intel® Edison, Intel® Joule™, Intel® Curie™, Intel® Galileo
Announcements
Welcome - This is a Peer-to-Peer Forum only. Intel has discontinued these products but you may find support from other customers on this Forum
9868 Discussions

Saving data to file - Help needed

KSels
New Contributor II
1,051 Views

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.

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 Solution
Pedro_M_Intel
Employee
123 Views

Hello KimLorentz,

You could try creating a .xls file with https://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream 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 http://iotdk.intel.com/docs/master/mraa/node/classes/uart.html 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.

View solution in original post

4 Replies
Pedro_M_Intel
Employee
124 Views

Hello KimLorentz,

You could try creating a .xls file with https://docs.nodejitsu.com/articles/advanced/streams/how-to-use-fs-create-write-stream 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 http://iotdk.intel.com/docs/master/mraa/node/classes/uart.html 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.

View solution in original post

KSels
New Contributor II
123 Views

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 https://nodejs.org/api/fs.html# fs_fs_createwritestream_path_options 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);

KSels
New Contributor II
123 Views

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

Pedro_M_Intel
Employee
123 Views

This one is a bit trickier. You might want to try with the example in https://github.com/MakersTeam/Edison/blob/master/JavaScript-Examples/NewFilePerDate.js 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.

Reply