Skip to content

Instantly share code, notes, and snippets.

@chriddyp
Last active August 29, 2015 13:57
Show Gist options
  • Save chriddyp/9882627 to your computer and use it in GitHub Desktop.
Save chriddyp/9882627 to your computer and use it in GitHub Desktop.
var token1 = "g9ngw7qyyy";
var token2 = "begbqoc59p";
var plotly = require('plotly')('gmon01','u6jlwbypiq');
tz = require("timezone");
timezone = "America/Montreal";
timezoneObj = require("timezone/"+timezone);
var initdata = [{x:[], y:[], stream:{token:token1, maxpoints:200}},
{x:[], y:[], stream:{token:token2, maxpoints:200}}];
var initlayout = {fileopt : "overwrite", filename : "nodenodenode"};
var exec = require('child_process').exec;
var tempId = '28-00000436cecd';
plotly.plot(initdata, initlayout, function (err, msg) {
if (err) return console.log(err)
console.log(msg);
var stream1 = plotly.stream(token1, function (err, res) {
console.log(err, res);
clearInterval(loop); // once stream is closed, stop writing
});
var stream2 = plotly.stream(token2, function (err, res) {
console.log(err, res);
clearInterval(loop); // once stream is closed, stop writing
});
var i = 0;
var loop = setInterval(function () {
var timeNow = tz(new Date().getTime(), timezone, timezoneObj, "%Y-%m-%d %H:%M:%S.%N");
exec( "cat /sys/bus/w1/devices/" + tempId + "/w1_slave | grep t= | cut -f2 -d= | awk '{print $1/1000}'", function( error, stdout, stderr ){
var tempC = parseFloat(stdout);
var tempF = ((1.8)*tempC + 32).toFixed(2);
var data1 = JSON.stringify({x: timeNow, y: tempF})+ "\n";
console.log(data1);
stream1.write(data1);
});
exec( "./sensors/humi2c/humi2c", function( error, stdout, stderr ){
var report = stdout.trim();
console.log(report);
if ( report.length ) {
var values = stdout.trim().split(" ");
var humC = values[0];
var humF = ((9.0/5.0)*humC + 32.0).toFixed(2);
var data2 = JSON.stringify({x: timeNow, y: humF})+ "\n";
console.log(data2);
stream2.write(data2);
}
});
}, 2000);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment