Created
September 5, 2015 06:47
-
-
Save chintanp/5b79255d60c8fe03a8f0 to your computer and use it in GitHub Desktop.
Live Streaming temperature and humidity data from a DHT11 though Arduino UNO to plotly
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
// Code inspired from http://adilmoujahid.com/posts/2015/07/practical-introduction-iot-arduino-nodejs-plotly/ | |
// and modified to use multiple traces, using : https://github.com/plotly/plotly-nodejs/blob/master/examples/streaming-multiple-traces.js | |
var serialport = require('serialport'), | |
plotly = require('plotly')('chintanp','vu32637k4y'), | |
tokens = ['xg5vkjdoct', 'qsc0j3pqks']; | |
var portName = 'COM5'; | |
// serial port part works fine, and was checked independently | |
var sp = new serialport.SerialPort(portName,{ | |
baudRate: 9600, | |
dataBits: 8, | |
parity: 'none', | |
stopBits: 1, | |
flowControl: false, | |
parser: serialport.parsers.readline("\n") | |
}); | |
// helper function to get a nicely formatted date string | |
function getDateString() { | |
var time = new Date().getTime(); | |
// 32400000 is (GMT+9 Japan) | |
// for your timezone just multiply +/-GMT by 3600000 | |
var datestr = new Date(time + 19800000).toISOString().replace(/T/, ' ').replace(/Z/, ''); | |
return datestr; | |
} | |
// wanting to create sub-plots with two data-streams | |
var initdata = [ | |
{name: "Temperature", x:[], y:[], stream:{token:tokens[0], maxpoints: 1500}}, | |
{name: "Humidity", x:[], y:[], stream:{token:tokens[1], maxpoints: 1500}} | |
]; | |
var initlayout = {fileopt : "new", filename : "Live Streaming Temperature and Humidity"}; | |
plotly.plot(initdata, initlayout, function (err, msg) { | |
if (err) | |
return console.log(err) | |
console.log(msg); | |
// creating two streams | |
var streams = { | |
"temperature" : plotly.stream(tokens[0], function (err, res) { | |
if (err) console.log(err); | |
console.log(err, res); | |
}), | |
"humidity" : plotly.stream(tokens[1], function (err, res) { | |
if (err) console.log(err); | |
console.log(err, res); | |
}) | |
}; | |
sp.on('data', function(input) { | |
if(isNaN(input) || input > 1023) return; | |
var values = input.split('\t'); | |
// writing the temperature stream | |
var tempStreamObject = JSON.stringify({ x : getDateString(), y : values[0] }); | |
console.log("temperatureObject: " + tempStreamObject); | |
streams["temperature"].write(tempStreamObject + '\n'); | |
// writing the humidiity stream | |
var humStreamObject = JSON.stringify({ x : getDateString(), y : values[1] }); | |
console.log("humidityObject: " + humStreamObject); | |
streams["humidity"].write(humStreamObject + '\n'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment