Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Intel Edison Temperature Logger With RBG-LCD
// Load Grove module
var groveSensor = require('jsupm_grove');
var FormData = require('form-data');
var fs = require('fs');
// we want mraa to be at least version 0.6.1
var mraa = require('mraa');
var version = mraa.getVersion();
var tSampFileName='t_samp.csv';
var filrUpldDstnUrl = "http://192.168.1.101:8099/fileupld/";
if (version >= 'v0.6.1') {
console.log('mraa version (' + version + ') ok');
startApp();
}
else {
console.log('meaa version(' + version + ') is old - this code may not work');
}
/**
* Refresh the data periodically
*
*/
function showData(display) {
setInterval(function() {
var red = 0;
var green = 0;
var blue =0;
var t=getTemperature();
if(t<24){
blue=250;
}
else if(t<27){
green=250;
}
else{
red=250;
}
display.setColor(red, green, blue);
display.clear();
display.setCursor(0,0);
display.write('Temperature:' + t+ 'C');
var ts=Date.now();
var smp= t +","+ts+"\n";
console.log(smp);
addToFile(smp);
}, 5000);
}
var temp=new groveSensor.GroveTemp(0);
function getTemperature(){
var data=temp.value();
return data;
//return (data*500)/1024;
}
function startApp() {
var lcd = require('jsupm_i2clcd');
var display = new lcd.Jhd1313m1(0, 0x3E, 0x62);
showData(display);
startSensorWatch();
}
function addToFile(data){
fs.appendFile(tSampFileName, data, function (err) {
if (err) throw err;
//console.log('data added to the file');
});
}
function startSensorWatch() {
var touch_sensor_value = 0, last_t_sensor_value;
//Touch Sensor connected to D2 connector
var digital_pin_D2 = new mraa.Gpio(2);
digital_pin_D2.dir(mraa.DIR_IN);
setInterval(function () {
touch_sensor_value = digital_pin_D2.read();
if (touch_sensor_value === 1 && last_t_sensor_value === 0) {
//move the data file to a new file and load it to the server,then remove the file
var newFileName="t_smp"+Date.now()+".csv";
fs.rename(tSampFileName,newFileName,function(err){
if(err){
console.log(err);
}
else{
var form = new FormData();
form.append('userFile', fs.createReadStream(newFileName));
form.submit(filrUpldDstnUrl, function(err, res) {
if(err){
console.log('could not upload the file');
}
res.resume();
console.log('file upld res:' ,res.statusCode);
fs.unlink(newFileName,function(){
console.log("file has been removed "+newFileName);
});
});
}
});
} else if (touch_sensor_value === 0 && last_t_sensor_value === 1) {
}
last_t_sensor_value = touch_sensor_value;
}, 300);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.