Skip to content

Commit d470eca

Browse files
committed
Smootie chart.
1 parent a0d5446 commit d470eca

File tree

1 file changed

+28
-13
lines changed

1 file changed

+28
-13
lines changed

mqtt/index.html

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,47 @@
66
</head>
77
<body style="font-family:Verdana;">
88

9-
<div id=time></div>
10-
<div id=power></div>
9+
<table id="myTable" border="1"></table>
10+
<canvas id="mycanvas" width="500" height="100"></canvas>
11+
<P>
12+
<div id="time"></div>
1113

1214
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
1315
<script src="http://www.trease.eu:8500/socket.io/socket.io.js"></script>
16+
<script type="text/javascript" src="http://github.com/joewalnes/smoothie/raw/master/smoothie.js"></script>
1417
<script>
1518
ElementExists = function(id) {
1619
return !!document.getElementById(id);
1720
};
18-
21+
22+
23+
var smoothie = new SmoothieChart({millisPerPixel: 500});
24+
var line1 = new TimeSeries();
25+
smoothie.streamTo(document.getElementById("mycanvas"));
26+
smoothie.addTimeSeries(line1);
1927

2028
var socket = io.connect("http://192.168.1.103:8500");
2129
socket.on('data', function(data){
22-
console.log("topic : %s, value : %s", data.topic, data.value);
23-
24-
// check if the element exists, if not create it and then set value
25-
if (!ElementExists ("power" + data.topic)) {
26-
var ni = document.getElementById('power');
27-
var newdiv = document.createElement('div');
28-
newdiv.setAttribute('id',"power" + data.topic);
29-
ni.appendChild(newdiv);
30+
// check if the table cells exists, if not create it and then set value
31+
if (!ElementExists ("myTablePower" + data.topic)) {
32+
var table=document.getElementById("myTable");
33+
var row=table.insertRow(0);
34+
var cell=row.insertCell(0);
35+
cell.id = "myTablePower" + data.topic;
36+
var cell=row.insertCell(0);
37+
cell.id = "myTableTopic" + data.topic;
3038
}
31-
document.getElementById("power" + data.topic).innerHTML= data.value;
39+
document.getElementById("myTablePower" + data.topic).innerHTML= data.value;
40+
document.getElementById("myTableTopic" + data.topic).innerHTML= data.topic;
3241

33-
var dt = new Date();
42+
// print the time the refresh happened
43+
var dt = new Date();
3444
document.getElementById("time").innerHTML= dt.toLocaleTimeString();
45+
46+
// if data topic is 0 it is the over power consumption
47+
if (data.topic == "sensors/power/0") {
48+
line1.append (new Date().getTime(), data.value);
49+
}
3550
});
3651
</script>
3752
</body>

0 commit comments

Comments
 (0)