|
| 1 | +<head> |
| 2 | +<script src="js/smoothie.js"></script> |
| 3 | + |
| 4 | +<script> |
| 5 | +// parse arguments from URL |
| 6 | +// www.mysite.com/my_app.html?x=1234 |
| 7 | +var GET = {}; |
| 8 | +var query = window.location.search.substring(1).split("&"); |
| 9 | +for (var i = 0, max = query.length; i < max; i++) { |
| 10 | + if (query[i] === "") // check for trailing & with no param |
| 11 | + continue; |
| 12 | + var param = query[i].split("="); |
| 13 | + GET[decodeURIComponent(param[0])] = decodeURIComponent(param[1] || ""); |
| 14 | +} |
| 15 | + |
| 16 | +var ws = new WebSocket('ws://169.229.198.105:8801/'); |
| 17 | +var MSPERPOINT = 50; |
| 18 | + |
| 19 | +var NUMLINES = 300; |
| 20 | +if ( Number.isInteger(parseInt(GET.lookback)) ) { |
| 21 | + NUMLINES = parseInt(parseFloat(GET.lookback)*60000/MSPERPOINT); |
| 22 | +} |
| 23 | + |
| 24 | +var PLOTMAX = 1000; |
| 25 | +if ( Number.isInteger(parseInt(GET.ymax)) ) { |
| 26 | + PLOTMAX = parseFloat(GET.ymax); |
| 27 | +} |
| 28 | + |
| 29 | +var PLOTMIN = 0; |
| 30 | +if ( Number.isInteger(parseInt(GET.ymin)) ) { |
| 31 | + PLOTMIN = parseFloat(GET.ymin); |
| 32 | +} |
| 33 | + |
| 34 | +var PLOTWIDTH = 600; |
| 35 | +var PLOTHEIGHT = 300; |
| 36 | +var MILLISPERLINE = NUMLINES * MSPERPOINT / 5; |
| 37 | + |
| 38 | +// create the chart stuff |
| 39 | +var mytimeline = new TimeSeries(); |
| 40 | +var mytimeline2 = new TimeSeries(); |
| 41 | + |
| 42 | +var chart = new SmoothieChart({millisPerPixel:NUMLINES*MSPERPOINT/PLOTWIDTH, |
| 43 | + grid:{fillStyle:'#ffffff', millisPerLine:MILLISPERLINE,verticalSections: (PLOTMAX-PLOTMIN)/100}, |
| 44 | + labels:{fillStyle:'#000000', fontSize:16, precision:0}, |
| 45 | + timestampFormatter: function(date) { |
| 46 | + function pad2(number) { return (number < 10 ? '0' : '') + number } |
| 47 | + return pad2(date.getHours()) + ':' + pad2(date.getMinutes()) + ':' + pad2(date.getSeconds()); |
| 48 | + }, |
| 49 | + yMinFormatter: function(min, precision) { |
| 50 | + return parseFloat(min).toFixed(precision).toString(); |
| 51 | + }, |
| 52 | + yMaxFormatter: function(max, precision) { |
| 53 | + return parseFloat(max).toFixed(precision).toString(); |
| 54 | + }, |
| 55 | + interpolation:'step',maxDataSetLength: NUMLINES,maxValue:PLOTMAX,minValue:PLOTMIN}); |
| 56 | + |
| 57 | +chart.addTimeSeries(mytimeline, { strokeStyle: 'rgba(0, 255, 0, 1)', |
| 58 | + fillStyle: 'rgba(0, 255, 0, 0.3)', lineWidth: 4 }); |
| 59 | +chart.addTimeSeries(mytimeline2, { strokeStyle: 'rgba(0, 0, 255, 1)', |
| 60 | + fillStyle: 'rgba(0, 0, 255, 0.15)', lineWidth: 1 }); |
| 61 | + |
| 62 | +var firstRun = true; |
| 63 | + |
| 64 | +function myLoading() { |
| 65 | + if ( Number.isInteger(parseInt(GET.lookback)) ) { |
| 66 | + document.getElementById("in_lookback").value = parseFloat(GET.lookback); |
| 67 | + } |
| 68 | + if ( Number.isInteger(parseInt(GET.ymin)) ) { |
| 69 | + document.getElementById("in_ymin").value = parseInt(GET.ymin); |
| 70 | + } |
| 71 | + if ( Number.isInteger(parseInt(GET.ymax)) ) { |
| 72 | + document.getElementById("in_ymax").value = parseInt(GET.ymax); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +// put in the data for the chart |
| 77 | +ws.onmessage = function(e){ |
| 78 | + var line = e.data.split(','); |
| 79 | + if (line.length == 2) { |
| 80 | + var ptTime = Date.now(); |
| 81 | + var ptVal = parseFloat(line[1]); |
| 82 | + var ptVal2 = parseFloat(line[0]); |
| 83 | + mytimeline.append(ptTime, ptVal); |
| 84 | + mytimeline2.append(ptTime, ptVal2); |
| 85 | + |
| 86 | + document.getElementById("curVal").innerHTML = ptVal.toString(); |
| 87 | + |
| 88 | + if (firstRun) { |
| 89 | + chart.streamTo(document.getElementById("chart")); |
| 90 | + console.log("plotting started!"); |
| 91 | + firstRun = false; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | +</script> |
| 96 | +</head> |
| 97 | + |
| 98 | +<body onload="myLoading()"> |
| 99 | +<h1>Fresh Data, Served Every Five Seconds</h1> |
| 100 | +<div> |
| 101 | + <p>Wonder what the 555 IC at work is doing? With Arduino, Websockets, and JS, wonder no more. |
| 102 | + <form style="width:270px;line-height: 1.5;border:4px solid black;padding:5px;"> |
| 103 | + Minutes to look back: |
| 104 | + <input id="in_lookback" name="lookback" value="0.25" type="text" |
| 105 | + size=5 style="float:right;"><br> |
| 106 | + Minimum y-value: |
| 107 | + <input id="in_ymin" name="ymin" value="0" type="text" |
| 108 | + size=5 style="float:right;"><br> |
| 109 | + Maximum y-value: |
| 110 | + <input id="in_ymax" name="ymax" value="1000" type="text" |
| 111 | + size=5 style="float:right;"><br> |
| 112 | + <div style="text-align:center;"> |
| 113 | + <input type="submit" value="Adjust View" |
| 114 | + style="margin: 5px auto;font-weight:bold;"> |
| 115 | + </div> |
| 116 | + </form> |
| 117 | + </div> |
| 118 | + |
| 119 | +<div id="divchart" style="position:relative;"> |
| 120 | + <div style="position:absolute; left:5px; top:5px; z-index:1; |
| 121 | + color:white; font-weight:bold; font-family:sans-serif; |
| 122 | + background-color:black;"> |
| 123 | + Current value: <span id="curVal"></span> |
| 124 | + </div> |
| 125 | + <canvas id="chart" width="600" height="300" |
| 126 | + style="position:absolute; z-index:0; border: 4px solid black;"> |
| 127 | + </canvas> |
| 128 | + </div> |
| 129 | + |
| 130 | +</body> |
0 commit comments