- Temperature sensor, to A0
- Light sensor, to A1
With the following code, we read values from sensors and send every 2 secs. (in the config) a msg to AWS IoT, containing value read for temperature (C) and light (lux).
This file contains hidden or 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 configurationFile = 'config.js'; | |
var fs = require('fs'); | |
var awsIot = require('aws-iot-device-sdk'); | |
var groveSensor = require('jsupm_grove'); | |
var connected = false; | |
// read configuration from file | |
// and store in configuration object | |
var configuration = JSON.parse( | |
fs.readFileSync(configurationFile) | |
); | |
console.log('Started Test with AWS !!!'); | |
console.log('....'); | |
console.log('Configuration:'); | |
console.log('CertPath: ' + configuration.certPath); | |
console.log('KeyPath: ' + configuration.keyPath); | |
console.log('CaPath: ' + configuration.caPath); | |
console.log('ClientId: ' + configuration.clientId); | |
console.log('Region: ' + configuration.region); | |
// create the device and connect | |
var device = awsIot.device({ | |
keyPath: configuration.keyPath, | |
certPath: configuration.certPath, | |
caPath: configuration.caPath, | |
clientId: configuration.clientId, | |
region: configuration.region | |
}); | |
device.on('connect', function() | |
{ | |
console.log('connect'); | |
connected = true; | |
}); | |
const PIN_TEMP = 0; | |
const PIN_LIGHT = 1; | |
const PIN_GAS = 2; | |
// Create the sensor object using AIO pin 0,1,2 | |
var temp = new groveSensor.GroveTemp(PIN_TEMP); | |
var light = new groveSensor.GroveLight(PIN_LIGHT); | |
var itNumber = 0; | |
function readAndSend() | |
{ | |
var msg = {}; | |
var TOPIC = configuration.clientId + '/msg'; | |
msg.clientId = configuration.clientId; | |
msg.temp = temp.value(); | |
// simulate reading of pressure | |
msg.pressure = 1024; | |
msg.light = light.value(); | |
if (connected) | |
{ | |
itNumber++; | |
device.publish(TOPIC, JSON.stringify(msg)); | |
console.log('Sent msg n. ' + itNumber); | |
} | |
// re-schedule execution of publish | |
setTimeout(readAndSend, configuration.sleepTime); | |
} | |
// first execution | |
readAndSend(); | |
{
"keyPath" : "/node_app_slot/certs/thunder10-private.pem.key",}
"certPath" : "/node_app_slot/certs/thunder10-certificate.pem.crt",
"caPath" : "/node_app_slot/certs/root-ca.crt",
"region" : "eu-west-1",
"clientId" : "thunder10",
"sleepTime" : 2000
The last piece (config.js) shows you what must be considered a best practice: Keep separated the logic from the configuration, that probably needs to be different from device to device and maybe can change more frequently. This way you need only to replace the config.js file.
The nice thing with Edison is that, if you want to change the configuration, you can simply upload the config.js file Over The Air.
No comments:
Post a Comment