Skip to content

Commit 5031a28

Browse files
committed
Single MQTT subscribe pushing to all clients
1 parent 8c56b01 commit 5031a28

File tree

4 files changed

+47
-31
lines changed

4 files changed

+47
-31
lines changed

mqtt/app.js

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,36 @@ function handler (req, res) {
2222
}
2323

2424

25-
// for each client connection, subscribe to mqtt and push updates
25+
2626
io.sockets.on('connection', function (socket) {
27-
var mqtt = require('mqtt');
28-
var client = mqtt.createClient(1883, 'localhost', function(err, client) {
29-
keepalive: 1000
30-
});
31-
// set of per client local variables
32-
var powercumulative = 0;
33-
var powerlasttime = new Date().getTime(); // UNIX time in ms
34-
35-
client.on('connect', function() {
36-
client.subscribe('sensors/+/+');
37-
38-
client.on('message', function(topic, message) {
39-
// console.log('topic: ' + topic + ' payload: ' + message);
40-
socket.emit('data', { topic: topic, value: message });
41-
if (topic == "sensors/power/0") {
42-
var powercurrenttime = new Date().getTime();
43-
var duration = (powercurrenttime - powerlasttime) / 1000.0;
44-
var powerused = parseInt(message, 10) * (duration / 3600.0);
45-
powercumulative += powerused;
46-
// console.log("duration ", duration, "powerused ", powerused, "cumulative ", powercumulative);
47-
socket.emit('data', { topic: "powercumulative", value: powercumulative.toFixed(3) });
48-
powerlasttime = powercurrenttime;
49-
}
50-
});
51-
});
27+
});
28+
29+
30+
// subscribe to MQTT
31+
var mqtt = require('mqtt');
32+
var client = mqtt.createClient(1883, 'localhost', function(err, client) {
33+
keepalive: 1000
34+
});
35+
// global variables for tracking cumulative power usage
36+
var powercumulative = 0;
37+
var powerlasttime = new Date().getTime(); // UNIX time in ms
38+
39+
client.on('connect', function() {
40+
client.subscribe('sensors/+/+');
41+
42+
client.on('message', function(topic, message) {
43+
// console.log('topic: ' + topic + ' payload: ' + message);
44+
io.sockets.emit('data', { topic: topic, value: message });
45+
if (topic == "sensors/power/0") {
46+
var powercurrenttime = new Date().getTime();
47+
var duration = (powercurrenttime - powerlasttime) / 1000.0;
48+
var powerused = parseInt(message, 10) * (duration / 3600.0) / 1000.0; // convert to KWh
49+
powercumulative += powerused;
50+
console.log("duration ", duration, "powerused ", powerused, "cumulative ", powercumulative);
51+
io.sockets.emit('data', { topic: "powercumulative", value: powercumulative.toFixed(3) });
52+
powerlasttime = powercurrenttime;
53+
}
54+
});
5255
});
5356

5457

mqtt/index.html

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747

4848
var topictag = "sensors/power/";
4949
if (data.topic.substring(0,topictag.length) == topictag) {
50-
console.log("Power match - " + data.topic + " " + typeof(data.topic));
50+
// console.log("Power match - " + data.topic + " " + typeof(data.topic));
5151
// check if the table cells exists, if not create it and then set value
5252
if (!ElementExists ("myTablePowerPower" + data.topic)) {
5353
var table=document.getElementById("myTablePower");
@@ -69,14 +69,14 @@
6969

7070
var topictag = "powercumulative";
7171
if (data.topic.substring(0,topictag.length) == topictag) {
72-
console.log("Powercumulative - " + data.value);
72+
// console.log("Powercumulative - " + data.value);
7373
document.getElementById("powercumulative").innerHTML= data.value;
7474

7575
}
7676

7777
var topictag = "sensors/temperature/";
7878
if (data.topic.substring(0,topictag.length) == topictag) {
79-
console.log("Temperature match - " + data.topic + " " + typeof(data.topic));
79+
// console.log("Temperature match - " + data.topic + " " + typeof(data.topic));
8080
// check if the table cells exists, if not create it and then set value
8181
if (!ElementExists ("myTableTempTemp" + data.topic)) {
8282
var table=document.getElementById("myTableTemp");
@@ -93,8 +93,6 @@
9393

9494

9595

96-
97-
9896
// print the time the refresh happened
9997
var dt = new Date();
10098
document.getElementById("time").innerHTML= dt.toLocaleTimeString();

mqtt/package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "hello-world",
3+
"description": "hello world test app",
4+
"version": "0.0.1",
5+
"private": true,
6+
"dependencies": {
7+
"express": "3.x",
8+
"socket.io": "*",
9+
"smoothie": "*",
10+
"mqtt": "*"
11+
}
12+
}

mqtt/run.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
cd /home/download/mqtt
3+
nodemon app.js

0 commit comments

Comments
 (0)