コード
server.coffee
fs = require("fs") http = require("http") server = http.createServer() io = require("socket.io").listen(server) server.on "request", (req, res) -> fs.readFile "client.html", (err, data) -> if err res.writeHead 500 return res.end("Error loading client.html") res.writeHead 200, "Content-Type": "text/html;charset=UTF-8" res.end data server.listen 8080 ADC = require("adc-pi-spi") adc = new ADC("/dev/spidev0.0") io.sockets.on "connection", (socket) -> adc.on "change", (channel, value) -> socket.volatile.emit "event", message: "Detected value: " + value , (data) -> console.log "result: " + data process.on "SIGTERM", -> adc.close() process.on "SIGINT", -> adc.close() process.on "exit", -> adc.close()
client.html
<html> <head> <title>who's in kitanaka?</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></\ script> <script> $(function() { var socket = io.connect(); socket.on("event",function(data,fn){ $("#msg").text(data.message); }); }); </script> </head> <body> <h1>Socket.IO connection test</h1> <p><b>Received message: </b></p> <div id="msg"></div> </body> </html>
highcharts jsを加えてみた。
このjsFiddleを参照。
http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/line-basic/
http://jsfiddle.net/gh/get/jquery/1.9.1/highslide-software/highcharts.com/tree/master/samples/highcharts/demo/dynamic-update/
(まだうまく動いてない。イベント検知よりも1Hzでポーリングしたほうが良さそう)
client.html
<html> <head> <title>who's in kitanaka?</title> <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script> <script src="//cdnjs.cloudflare.com/ajax/libs/socket.io/0.9.16/socket.io.min.js"></\ script> <script src="http://code.highcharts.com/highcharts.js"></script> <script src="http://code.highcharts.com/modules/exporting.js"></script> <script> $(function() { var socket = io.connect(); socket.on("event",function(data,fn){ $("#msg").text(data.message); $(function () { $(document).ready(function() { Highcharts.setOptions({ global: { useUTC: false } }); var chart; $('#container').highcharts({ chart: { type: 'spline', animation: Highcharts.svg, // don't animate in old IE marginRight: 10, events: { load: function() { // set up the updating of the chart each second var series = this.series[0]; var x = (new Date()).getTime(), // current time y = data.message; series.addPoint([x, y], true, true); } } }, title: { text: 'PIR Sensor Data' }, xAxis: { type: 'datetime', tickPixelInterval: 150 }, yAxis: { title: { text: 'Value' }, min: 0, max: 600, plotLines: [{ value: 0, width: 1, color: '#808080' }] }, tooltip: { formatter: function() { return '<b>'+ this.series.name +'</b><br/>'+ Highcharts.dateFormat('%Y-%m-%d %H:%M:%S', this.x) +'<br/>'+ Highcharts.numberFormat(this.y, 2); } }, legend: { enabled: false }, exporting: { enabled: false }, series: [{ name: 'A/D converted sensor value', data: (function() { // generate an array of random data var data = [], time = (new Date()).getTime(), for (i = -19; i <= 0; i++) { data.push({ x: time + i * 1000, y: Math.random() }); } return data; })() }] }); }); }); }); }); </script> </head> <body> <h1>Socket.IO connection test</h1> <p><b>Received message: </b></p> <div id="msg"></div> <div id="container" style="min-width: 310px; height: 400px; margin: 0 auto"></div> </body> </html>