Skip to content

Instantly share code, notes, and snippets.

@w4ilun
Last active August 29, 2015 14:22
Show Gist options
  • Save w4ilun/69c1ba84a74a8d97d4d7 to your computer and use it in GitHub Desktop.
Save w4ilun/69c1ba84a74a8d97d4d7 to your computer and use it in GitHub Desktop.
EMC Test
//var exec = require('child_process').exec;
//var dgram = require('dgram');
//var client = dgram.createSocket('udp4');
var groveSensor = require('jsupm_grove');
var loudnessSensor = require('jsupm_groveloudness');
var touchModule = require('jsupm_ttp223');
var vibrationModule = require('jsupm_ldt0028');
var LCD = require('jsupm_i2clcd');
var digitalAccelerometer = require('jsupm_mma7660');
//init
// var child = exec("systemctl start iotkit-agent", function (error, stdout, stderr) {
// if (error !== null) {
// console.log('exec error: ' + error);
// }
// });
var temp = new groveSensor.GroveTemp(2);
var light = new groveSensor.GroveLight(1);
var sound = new loudnessSensor.GroveLoudness(0);
var touch = new touchModule.TTP223(4);
var vibration = new vibrationModule.LDT0028(3);
var myLcd = new LCD.Jhd1313m1 (0, 0x3E, 0x62);
var myDigitalAccelerometer = new digitalAccelerometer.MMA7660(
digitalAccelerometer.MMA7660_I2C_BUS,
digitalAccelerometer.MMA7660_DEFAULT_I2C_ADDR);
myLcd.setColor(0,0,255);
myDigitalAccelerometer.setModeStandby();
myDigitalAccelerometer.setSampleRate(digitalAccelerometer.MMA7660.AUTOSLEEP_64);
myDigitalAccelerometer.setModeActive();
var x, y, z;
x = digitalAccelerometer.new_intp();
y = digitalAccelerometer.new_intp();
z = digitalAccelerometer.new_intp();
// var udpOptions = {
// host : '127.0.0.1',
// port : 41234
// };
// function sendObservation(name, value, on){
// var msg = JSON.stringify({
// n: name,
// v: value,
// on: on
// });
// var sentMsg = new Buffer(msg);
// console.log("Sending observation: " + sentMsg);
// client.send(sentMsg, 0, sentMsg.length, udpOptions.port, udpOptions.host);
// };
//poll
setInterval(function(){
var date = new Date();
var time = date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
var celsius = temp.value();
var lux = light.value();
var volume = sound.value();
var isPressed = touch.isPressed();
var vibrationValue = vibration.getSample();
myDigitalAccelerometer.getRawValues(x, y, z);
console.log("#####");
console.log("Current Time:" + time);
console.log("Celsius:" + celsius);
console.log("Sound:" + volume);
console.log("Lux:" + lux);
console.log("Button Pressed:" + isPressed);
console.log("Vibration:" + vibrationValue);
console.log("Accelerometer Raw Values: x = " + digitalAccelerometer.intp_value(x) +
" y = " + digitalAccelerometer.intp_value(y) +
" z = " + digitalAccelerometer.intp_value(z));
console.log("#####");
myLcd.setCursor(0,0);
myLcd.write(celsius+"C "+volume+"dB "+lux+"LX "+(isPressed?"T":"F"));
myLcd.setCursor(1,0);
myLcd.write("X:"+digitalAccelerometer.intp_value(x)+"Y:"+digitalAccelerometer.intp_value(y)+"Z:"+digitalAccelerometer.intp_value(z));
// var data = [{
// sensorName : "temp",
// sensorType: "temperature.v1.0",
// observations: [{
// on: new Date().getTime(),
// value: celsius
// }]
// },{
// sensorName : "sound",
// sensorType: "sound.v1.0",
// observations: [{
// on: new Date().getTime(),
// value: volume
// }]
// },{
// sensorName : "light",
// sensorType: "light.v1.0",
// observations: [{
// on: new Date().getTime(),
// value: lux
// }]
// },{
// sensorName : "touch",
// sensorType: "touch.v1.0",
// observations: [{
// on: new Date().getTime(),
// value: isPressed
// }]
// },{
// sensorName : "vibe",
// sensorType: "vibration.v1.0",
// observations: [{
// on: new Date().getTime(),
// value: vibrationValue
// }]
// },{
// sensorName : "accel",
// sensorType: "accelerometer.v1.0",
// observations: [{
// on: new Date().getTime(),
// value: "x = " + digitalAccelerometer.intp_value(x) +
// " y = " + digitalAccelerometer.intp_value(y) +
// " z = " + digitalAccelerometer.intp_value(z)
// }]
// }];
// data.forEach(function(item) {
// item.observations.forEach(function (observation) {
// sendObservation(item.sensorName, observation.value, observation.on);
// });
// });
},1000);
process.on('SIGINT', function()
{
// clean up memory
digitalAccelerometer.delete_intp(x);
digitalAccelerometer.delete_intp(y);
digitalAccelerometer.delete_intp(z);
myDigitalAccelerometer.setModeStandby();
console.log("Exiting...");
process.exit(0);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment