Skip to content

Instantly share code, notes, and snippets.

@joelongstreet
Last active August 29, 2015 14:03
Show Gist options
  • Save joelongstreet/c2dd538c453907500fb0 to your computer and use it in GitHub Desktop.
Save joelongstreet/c2dd538c453907500fb0 to your computer and use it in GitHub Desktop.
A node script for a raspberry pi which collects temperature data and logs it to a .csv
var fs = require('fs');
var path = require('path');
var gpio = require('gpio');
var filePath = path.join(process.cwd(), 'log.csv');
var tempSensor = gpio.export(4, {
direction: 'in',
interval: 5000,
ready: startApp
});
var indicator = gpio.export(5, {
direction: 'out'
});
var updateCSV = function(newTemperature){
var csv = fs.readFileSync(filePath, { encoding: 'utf8' });
console.log(csv);
csv += [
newTemperature,
new Date().getTime(),
'\n'
].join(', ');
fs.writeFileSync(filePath, csv, { encoding: 'utf8' });
indicator.set(0);
};
var startApp = function(){
tempSensor.on('change', function(temperature){
indicator.set(1);
setTimeout(function(){
updateCSV(temperature);
}, 2000);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment