Created
May 26, 2016 07:09
-
-
Save yamaryu0508/7389aaa691da06ae54b5de04814d83b5 to your computer and use it in GitHub Desktop.
raspiTemp.py
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
var awsIot = require('aws-iot-device-sdk'); // AWS IoT SDK | |
var moment = require('moment'); // Date library // moment | |
var exec = require('child_process').exec; | |
// constant parameters | |
const DEVICE_NAME = 'dev1'; | |
const TOPIC = "raspi/temp"; // MQTT TOPIC | |
const REGION = 'ap-northeast-1'; | |
// device config. | |
var device = awsIot.device({ | |
keyPath: './cert/private.pem', | |
certPath: './cert/certificate.pem', | |
caPath: './cert/rootCA.pem', | |
clientId: DEVICE_NAME, | |
region: REGION | |
}); | |
// get CPU Temp. from Raspberry PI | |
function getCPUTemp(callback) { | |
exec('cat /sys/class/thermal/thermal_zone0/temp', function(error, stdout, stderr) { | |
if (stdout) { | |
var value = Number(stdout) / 1000.; | |
console.log('stdout: ' + value); | |
callback(value); | |
} | |
}); | |
} | |
// publish CPU temp | |
function publish_cpu_temp() { | |
getCPUTemp(function(value){ | |
var message = { | |
"device": DEVICE_NAME, | |
"sensor": 'raspi', | |
"time": moment().format(), | |
"temp": value | |
}; | |
message = JSON.stringify(message); | |
console.log("# Publish: " + message); | |
device.publish(TOPIC, message); // publish | |
}); | |
} | |
// loop for publish | |
setInterval(function() { | |
publish_cpu_temp(); | |
}, 10 * 1000); // per 10sec | |
// event for device "connect" | |
device | |
.on('connect', function() { | |
console.log('# Connected to Message Broker.'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment