I have written a code to log accelerometer values from RuuviTag using Espruino. I am able to save the values in a file using Storage class. This is the code I have written.
var period = 10; // milliseconds
var st = require(“Storage”);
var myFile = st.open(“data.csv”,“a”);
const length = 200; // number of records
var ruuvi = require(“Ruuvitag”);
var records = [];
function log() {
ruuvi.setAccelOn(true);
var date = new Date();
var isoDateTime = new Date(date.getTime() - (date.getTimezoneOffset() * 60000));
var line = isoDateTime + “,” + ruuvi.getAccelData().x +"," + ruuvi.getAccelData().y + “,” + ruuvi.getAccelData().z + “\n”;
if(records.push(line) > length){
records.splice(0,1);
}
ruuvi.setAccelOn(false);
}
function onInit() {
E.setTimeZone(5.5);
setInterval(log, period);
myFile.write(“Sl No,Time,X,Y,Z\n”);
}
function getData() {
records.forEach( function(d,i,o) {
myFile.write(i+1+ “,” + d);
});
records.splice(0, records.length);
}
The problem I am facing is that sometimes the program crashes. Also the values recorded for the first time after calling the getData() function don’t get updated and stay the same. Also the number of values recorded remains the same even though I have specified the length.
Can someone please help me as to what I should do in order to rectify these errors?