Permalink
Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
162 lines (130 sloc) 4.86 KB
/*jslint node:true, vars:true, bitwise:true, unparam:true */
/*jshint unused:true */
//Setup express
var express = require('express');
var app = express();
app.use(express.static(__dirname));
var server = app.listen(8085);
var io = require('socket.io').listen(server);
var mraa = require('mraa'); //require mraa
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console
var mcp = require('jsupm_mcp9808');
var temp = new mcp.MCP9808(1);
var temperature = {};
var status = {};
status.celsius = true;
status.manufacturer = temp.getManufacturer();
status.deviceid = temp.getDevicedId();
status.resolution = temp.getResolution();
status.hysteresis = temp.getHysteresis();
status.tcrit_set = temp.getMonitorReg(mcp.MCP9808.CRIT_TEMP);
status.tlower_set = temp.getMonitorReg(mcp.MCP9808.LOWER_TEMP);
status.tupper_set = temp.getMonitorReg(mcp.MCP9808.UPPER_TEMP);
status.alert_mode = '0';
setInterval(getTemp , 1000);
io.sockets.on('connection', function (socket) {
sendStatus();
sendTemp();
setInterval(function () {
sendTemp();
}, 1000);
socket.on('set_temp_mode', function(){
status.celsius = !temp.isCelsius();
temp.setMode(status.celsius);
status.hysteresis = temp.getHysteresis();
status.tcrit_set = temp.getMonitorReg(mcp.MCP9808.CRIT_TEMP);
status.tlower_set = temp.getMonitorReg(mcp.MCP9808.LOWER_TEMP);
status.tupper_set = temp.getMonitorReg(mcp.MCP9808.UPPER_TEMP);
sendStatus();
});
socket.on('set_temp_mon', function(data){
switch(data.alert_temp_set){
case "tcrit":
temp.setMonitorReg(mcp.MCP9808.CRIT_TEMP, data.value);
status.tcrit_set = temp.getMonitorReg(mcp.MCP9808.CRIT_TEMP);
break;
case "tlower":
temp.setMonitorReg(mcp.MCP9808.LOWER_TEMP, data.value);
status.tlower_set = temp.getMonitorReg(mcp.MCP9808.LOWER_TEMP);
break;
case "tupper":
temp.setMonitorReg(mcp.MCP9808.UPPER_TEMP, data.value);
status.tupper_set = temp.getMonitorReg(mcp.MCP9808.UPPER_TEMP);
break;
default:
//nothing;
}
sendStatus();
});
socket.on('set_param', function(data){
switch(data.command){
case 'resolution':
switch(data.value){
case '1':
temp.setResolution(mcp.MCP9808.RES_PRECISION);
break;
case '2':
temp.setResolution(mcp.MCP9808.RES_HIGH);
break;
case '3':
temp.setResolution(mcp.MCP9808.RES_MEDIUM);
break;
case '4':
temp.setResolution(mcp.MCP9808.RES_LOW);
break;
}
status.resolution = temp.getResolution();
break;
case 'hysteresis':
switch(data.value){
case '1':
temp.setHysteresis(mcp.MCP9808.HYST_0);
break;
case '2':
temp.setHysteresis(mcp.MCP9808.HYST_1_5);
break;
case '3':
temp.setHysteresis(mcp.MCP9808.HYST_3_0);
break;
case '4':
temp.setHysteresis(mcp.MCP9808.HYST_6_0);
break;
}
status.hysteresis = temp.getHysteresis();
break;
case 'alert_mode':
status.alert_mode = data.value;
switch(data.value){
case '0':
temp.clearAlertMode();
break;
case '1':
temp.setAlertMode(mcp.MCP9808.ALERTCTRL);
break;
case '2':
temp.setAlertMode(mcp.MCP9808.ALERTCTRL | mcp.MCP9808.ALERTMODE);
break;
case 'clear':
temp.clearInterrupt();
break;
}
break;
default:
//nothing
}
sendStatus();
});
function sendStatus(){
io.sockets.emit( 'status' , JSON.stringify(status)); //send temp every interval
}
function sendTemp(){
io.sockets.emit( 'temp' , JSON.stringify(temperature)); //send temp every interval
}
});
function getTemp(){
var new_temp = temp.getTemp();
temperature.temp = Math.round(new_temp * 10.0) / 10.0;
temperature.tcrit = temp.isTcrit();
temperature.tlower = temp.isTlower();
temperature.tupper = temp.isTupper();
}