Last active
August 29, 2015 14:03
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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