Skip to content

Commit 5ff675c

Browse files
committed
MQTT and Sockets.io example, pushing CurrentCost consumption via Mosquitto
1 parent d04a701 commit 5ff675c

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

mqtt/app.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
var app = require('http').createServer(handler),
2-
io = require('socket.io').listen(app),
3-
fs = require('fs')
1+
var app = require('http').createServer(handler);
2+
var io = require('socket.io').listen(app, {
3+
'log level': 1
4+
});
5+
var fs = require('fs');
46

57
app.listen(8500);
68

9+
10+
// server web pages
711
function handler (req, res) {
812
console.log('Connection from %j', req.connection.remoteAddress);
913
fs.readFile(__dirname + '/index.html',
@@ -18,19 +22,22 @@ function handler (req, res) {
1822
}
1923

2024

25+
// for each client connection, subscribe to mqtt and push updates
2126
io.sockets.on('connection', function (socket) {
27+
2228
var mqtt = require('mqtt');
2329
var client = mqtt.createClient(1883, 'localhost', function(err, client) {
2430
keepalive: 10000
2531
});
2632

2733
client.on('connect', function() {
28-
client.subscribe('sensors/power/0');
34+
client.subscribe('sensors/power/+');
2935

3036
client.on('message', function(topic, message) {
3137
console.log('topic: ' + topic + ' payload: ' + message);
32-
socket.emit('data', { channel: 0, value: message });
38+
socket.emit('data', { topic: topic, value: message });
3339
});
3440
});
41+
3542
});
3643

mqtt/index.html

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,39 @@
11
<html>
22
<head>
33
<title>
4-
Power used today
4+
Charlestown Power
55
</title>
66
</head>
77
<body style="font-family:Verdana;">
88

9+
<div id=time></div>
910
<div id=power></div>
1011

1112
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
1213
<script src="http://www.trease.eu:8500/socket.io/socket.io.js"></script>
1314
<script>
15+
ElementExists = function(id) {
16+
return !!document.getElementById(id);
17+
};
18+
19+
1420
var socket = io.connect("http://192.168.1.103:8500");
1521
socket.on('data', function(data){
16-
console.log("channel : %s, value : %s", data.channel, data.value);
17-
document.getElementById("power").innerHTML= data.value;
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+
}
31+
document.getElementById("power" + data.topic).innerHTML= data.value;
32+
33+
var dt = new Date();
34+
document.getElementById("time").innerHTML= dt.toLocaleTimeString();
1835
});
1936
</script>
2037
</body>
2138
</html>
39+

0 commit comments

Comments
 (0)