Skip to content

Commit e9dc3cc

Browse files
committed
Pushing data and building tables clientside.
1 parent d470eca commit e9dc3cc

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

mqtt/app.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,17 +27,18 @@ io.sockets.on('connection', function (socket) {
2727

2828
var mqtt = require('mqtt');
2929
var client = mqtt.createClient(1883, 'localhost', function(err, client) {
30-
keepalive: 10000
30+
keepalive: 1000
3131
});
3232

3333
client.on('connect', function() {
34-
client.subscribe('sensors/power/+');
34+
client.subscribe('sensors/+/+');
3535

3636
client.on('message', function(topic, message) {
3737
console.log('topic: ' + topic + ' payload: ' + message);
38-
socket.emit('data', { topic: topic, value: message });
38+
socket.broadcast.emit('data', { topic: topic, value: message });
3939
});
4040
});
4141

4242
});
4343

44+

mqtt/index.html

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@
66
</head>
77
<body style="font-family:Verdana;">
88

9-
<table id="myTable" border="1"></table>
9+
<table><tr>
10+
<td>
11+
<table id="myTablePower" border="1"><tr>
12+
<td id="myTablePowerTopicsensors/power/0">Total power</td>
13+
<td id="myTablePowerPowersensors/power/0"></td>
14+
</tr>
15+
</table>
16+
</td>
17+
<td>
18+
<table id="myTableTemp" border="1"></table>
19+
</td>
20+
</tr></table>
1021
<canvas id="mycanvas" width="500" height="100"></canvas>
1122
<P>
1223
<div id="time"></div>
@@ -18,6 +29,9 @@
1829
ElementExists = function(id) {
1930
return !!document.getElementById(id);
2031
};
32+
BeginsWith = function(needle, haystack){
33+
return (haystack.substr(0, needle.length) == needle);
34+
}
2135

2236

2337
var smoothie = new SmoothieChart({millisPerPixel: 500});
@@ -26,27 +40,55 @@
2640
smoothie.addTimeSeries(line1);
2741

2842
var socket = io.connect("http://192.168.1.103:8500");
29-
socket.on('data', function(data){
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;
38-
}
39-
document.getElementById("myTablePower" + data.topic).innerHTML= data.value;
40-
document.getElementById("myTableTopic" + data.topic).innerHTML= data.topic;
43+
socket.on('data', function(data) {
44+
45+
var topictag = "sensors/power/";
46+
if (data.topic.substring(0,topictag.length) == topictag) {
47+
console.log("Power match - " + data.topic + " " + typeof(data.topic));
48+
// check if the table cells exists, if not create it and then set value
49+
if (!ElementExists ("myTablePowerPower" + data.topic)) {
50+
var table=document.getElementById("myTablePower");
51+
var row=table.insertRow(1);
52+
var cell=row.insertCell(0);
53+
cell.id = "myTablePowerPower" + data.topic;
54+
document.getElementById("myTablePowerPower" + data.topic).style.textAlign="right";
55+
var cell=row.insertCell(0);
56+
cell.id = "myTablePowerTopic" + data.topic;
57+
document.getElementById("myTablePowerTopic" + data.topic).innerHTML= data.topic;
58+
}
59+
document.getElementById("myTablePowerPower" + data.topic).innerHTML= data.value;
60+
61+
// if data topic is 0 it is the overall power consumption, so update the graph
62+
if (data.topic == "sensors/power/0") {
63+
line1.append (new Date().getTime(), data.value);
64+
}
65+
}
66+
67+
var topictag = "sensors/temperature/";
68+
if (data.topic.substring(0,topictag.length) == topictag) {
69+
console.log("Temperature match - " + data.topic + " " + typeof(data.topic));
70+
// check if the table cells exists, if not create it and then set value
71+
if (!ElementExists ("myTableTempTemp" + data.topic)) {
72+
var table=document.getElementById("myTableTemp");
73+
var row=table.insertRow(0);
74+
var cell=row.insertCell(0);
75+
cell.id = "myTableTempTemp" + data.topic;
76+
document.getElementById("myTableTempTemp" + data.topic).style.textAlign="right";
77+
var cell=row.insertCell(0);
78+
cell.id = "myTableTempTopic" + data.topic;
79+
document.getElementById("myTableTempTopic" + data.topic).innerHTML= data.topic;
80+
}
81+
document.getElementById("myTableTempTemp" + data.topic).innerHTML= data.value;
82+
}
83+
84+
85+
86+
4187

4288
// print the time the refresh happened
4389
var dt = new Date();
4490
document.getElementById("time").innerHTML= dt.toLocaleTimeString();
4591

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-
}
5092
});
5193
</script>
5294
</body>

0 commit comments

Comments
 (0)