Created
May 3, 2016 08:00
-
-
Save robertpyke/58a03bcb6f5064f2fa857601684aa224 to your computer and use it in GitHub Desktop.
Intel Edison with grove sensors
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
/*jslint node:true, vars:true, bitwise:true, unparam:true */ | |
/*jshint unused:true */ | |
// Leave the above lines for propper jshinting | |
//Type Node.js Here :) | |
var path = require('path'); | |
var mraa = require('mraa'); // require mraa | |
var awsIot = require('aws-iot-device-sdk'); // require aws-iot | |
console.log('MRAA Version: ' + mraa.getVersion()); //write the mraa version to the Intel XDK console | |
var thingShadows = awsIot.thingShadow({ | |
keyPath: path.resolve(__dirname, "certs/05461b06b7-private.pem.key"), | |
certPath: path.resolve(__dirname, "certs/05461b06b7-certificate.pem.crt"), | |
caPath: path.resolve(__dirname, "certs/root-CA.crt"), | |
debug: false, | |
host: 'XXXXXXXXXXXXXXX.iot.us-east-1.amazonaws.com' | |
}); | |
thingShadows.on('connect', function() { | |
console.log('connected to things instance, registering thing name'); | |
thingShadows.register( 'edison', { | |
persistentSubscribe: true, | |
ignoreDeltas: true | |
} ); | |
console.log('registered thing shadows...'); | |
}); | |
/* | |
UVSensor setup | |
*/ | |
var UVSensor = require('jsupm_guvas12d'); | |
// Instantiate a UV sensor on analog pin A3 | |
var myUVSensor = new UVSensor.GUVAS12D(3); | |
// analog voltage, usually 3.3 or 5.0 | |
var g_GUVAS12D_AREF = 5.0; | |
var g_SAMPLES_PER_QUERY = 1024; | |
var grove = require('jsupm_grove'); | |
var i2clcd = require('jsupm_i2clcd'); | |
var groveMotion = require('jsupm_biss0001'); | |
var jsupm_th02_module = require('jsupm_th02'); | |
// Create the button object using UART | |
var button = new grove.GroveButton(0); | |
var th02 = new jsupm_th02_module.TH02(1); | |
// Create motionSensor using D7 | |
var motionSensor = new groveMotion.BISS0001(7); | |
// Create the light sensor object using AIO pin 2 | |
var light = new grove.GroveLight(2); | |
var lcdPanel = new i2clcd.Jhd1313m1(0); | |
lcdPanel.clear(); | |
var upmBuzzer = require("jsupm_buzzer"); | |
var pastButtonState = 0; | |
// Initialize on GPIO 3 | |
var myBuzzer = new upmBuzzer.Buzzer(3); | |
myBuzzer.stopSound(); | |
myBuzzer.setVolume(0.05); | |
//var myOnboardLed = new mraa.Gpio(3, false, true); //LED hooked up to digital pin (or built in pin on Galileo Gen1) | |
var myOnboardLed = new mraa.Gpio(13); //LED hooked up to digital pin 13 (or built in pin on Intel Galileo Gen2 as well as Intel Edison) | |
myOnboardLed.dir(mraa.DIR_OUT); //set the gpio direction to output | |
var ledState = true; //Boolean to hold the state of Led | |
var motionCaptured = false; // track whether motion was captured in the period | |
lcdPanel.setColor(random(0, 255),random(0, 255),random(0, 255)); | |
periodicActivity(); //call the periodicActivity function | |
fastPeriodActivity(); | |
/* | |
* Slow loop - executed every 30s | |
*/ | |
function periodicActivity() | |
{ | |
processLEDState(); | |
updateAWS(); | |
setTimeout(periodicActivity,30000); //call the indicated function after 30 seconds | |
motionCaptured = false; // reset that motion was captured. | |
} | |
/* | |
* Fast loop - executed every 100 ms | |
*/ | |
function fastPeriodActivity() { | |
myBuzzer.stopSound(); | |
processMotionSensorState(); | |
processButtonState(); | |
processLightSensorState(); | |
updateScreen(); | |
setTimeout(fastPeriodActivity,100); //call the indicated function after 0.1 second (100 milliseconds) | |
} | |
function updateAWS() { | |
console.info("Updating AWS"); | |
thingShadows.update('edison', { | |
state: { | |
reported: | |
{ | |
button_name: button.name(), | |
button_value: button.value(), | |
light_raw_value: light.raw_value(), | |
light_value: light.value(), | |
motion: motionCaptured, | |
temperature: th02.getTemperature(), | |
humidity: th02.getHumidity(), | |
temperature_humidity_sensor_name: th02.name(), | |
uv_value: roundNum(myUVSensor.value(g_GUVAS12D_AREF, g_SAMPLES_PER_QUERY), 9) | |
} | |
} | |
}); | |
} | |
/* | |
* Process the motion sensor state | |
*/ | |
function processMotionSensorState(){ | |
if(motionSensor.value()) { | |
motionCaptured = true; | |
myBuzzer.playSound(upmBuzzer.SI, 100000); | |
} | |
} | |
/* | |
* Process the light sensor state | |
*/ | |
function processLightSensorState() { | |
// var longMessage = light.name() + "; raw: " + light.raw_value() + ", val: " + light.value(); | |
// console.info(longMessage); | |
} | |
/* | |
* Process the current ledState | |
*/ | |
function processLEDState() { | |
myOnboardLed.write(ledState?1:0); // if ledState is true then write a '1' (high) otherwise write a '0' (low) | |
ledState = !ledState; //invert the ledState | |
} | |
/* | |
* Update the LCD display | |
*/ | |
function updateScreen() { | |
var shortMessage = "L:" + light.raw_value() + | |
";B:" + button.value() + | |
";M:" + (motionSensor.value()?"1":"0") + | |
" "; // Buffer to handle changing size of L (XX to XXX) | |
if (button.value() == 1 && (pastButtonState == 0)) { | |
lcdPanel.setColor(random(0, 255),random(0, 255),random(0, 255)); | |
} | |
lcdPanel.home(); | |
lcdPanel.write(shortMessage); | |
pastButtonState = button.value() | |
} | |
/* | |
* Handle the button state | |
*/ | |
function processButtonState() { | |
button.value(); | |
} | |
/* | |
* Generate a random number between low and high. | |
*/ | |
function random (low, high) { | |
return Math.random() * (high - low) + low; | |
} | |
function roundNum(num, decimalPlaces) | |
{ | |
var extraNum = (1 / (Math.pow(10, decimalPlaces) * 1000)); | |
return (Math.round((num + extraNum) * (Math.pow(10, decimalPlaces))) / Math.pow(10, decimalPlaces)); | |
} | |
// -------------- | |
var device = awsIot.device({ | |
keyPath: path.resolve(__dirname, "certs/05461b06b7-private.pem.key"), | |
certPath: path.resolve(__dirname, "certs/05461b06b7-certificate.pem.crt"), | |
caPath: path.resolve(__dirname, "certs/root-CA.crt"), | |
clientId: "edison", | |
region: "us-east-1", | |
debug: false | |
}); | |
// | |
// Device is an instance returned by mqtt.Client(), see mqtt.js for full | |
// documentation. | |
// | |
device | |
.on('connect', function() { | |
console.info('connect'); | |
device.subscribe('topic_1'); | |
device.publish('topic_2', JSON.stringify({ test_data: 1})); | |
console.info('published to AWS IOT'); | |
}); | |
device | |
.on('message', function(topic, payload) { | |
console.info('message', topic, payload.toString()); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment