Skip to content

Commit de9d57e

Browse files
committed
Sunday
1 parent 5379e21 commit de9d57e

File tree

3 files changed

+28
-82
lines changed

3 files changed

+28
-82
lines changed

mqtt/power.html

Lines changed: 11 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -7,71 +7,27 @@
77
<link type="text/css" rel="stylesheet" href="/app.css" media="all">
88
</head>
99
<body>
10-
Current total current power consumption <span id="sensors/power/0"></span>
11-
<table id="myTablePower"><tr>
12-
</tr>
13-
</table>
14-
<P>
15-
Total power today <span id="sensors/power/0powercumulativeToday"></span> KWh
16-
<P>
17-
Total power this hour <span id="sensors/power/0powercumulativeHour"></span> KWh
18-
<P>
19-
<canvas id="mycanvas" width="500" height="100"></canvas>
20-
<P>
21-
<div id="time" style="font-size:x-small"></div>
10+
<div id="sensors"></div>
11+
2212

2313
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
2414
<script src="http://www.trease.eu:8500/socket.io/socket.io.js"></script>
25-
<script type="text/javascript" src="http://github.com/joewalnes/smoothie/raw/master/smoothie.js"></script>
2615
<script>
27-
ElementExists = function(id) {
28-
return !!document.getElementById(id);
29-
};
30-
BeginsWith = function(needle, haystack){
31-
return (haystack.substr(0, needle.length) == needle);
32-
}
33-
3416

35-
var smoothie = new SmoothieChart({millisPerPixel: 500});
36-
var line1 = new TimeSeries();
37-
smoothie.streamTo(document.getElementById("mycanvas"));
38-
smoothie.addTimeSeries(line1);
3917

4018
var socket = io.connect("http://192.168.1.103:8500");
4119
socket.on('data', function(data) {
42-
// console.log("Message received " + data.topic + " of " + data.value);
43-
44-
// check the target topic exisits & if not create a target table entry
45-
if (!ElementExists (data.topic)) {
46-
// console.log("Creating target " + data.topic);
47-
var topictag = "sensors/power/";
48-
if (data.topic.substring(0,topictag.length) == topictag) {
49-
var table=document.getElementById("myTablePower");
50-
var row=table.insertRow(0);
51-
var cell=row.insertCell(0);
52-
cell.id = data.topic;
53-
var cell=row.insertCell(0);
54-
cell.id = data.topic + "name";
55-
document.getElementById(data.topic).style.textAlign="right";
56-
document.getElementById(data.topic + "name").innerHTML= data.topic;
57-
}
58-
}
59-
// now we know there is a target, update it (unless it is one we don't want one this page)
60-
// console.log("Setting target " + data.topic + " to " + data.value);
61-
if (ElementExists(data.topic)) {
62-
document.getElementById(data.topic).innerHTML= data.value;
63-
} else {
64-
// console.log("ignoring " + data.topic);
65-
}
66-
67-
// if data topic is 0 it is the overall power consumption, so update the graph
68-
if (data.topic == "sensors/power/0") {
69-
line1.append (new Date().getTime(), data.value);
20+
console.log("Message received " + data.topic + " of " + data.value);
21+
22+
if($("#" + data.topic().replace('/', '')).length == 0) {
23+
//it doesn't exist
24+
console.log(data.topic);
25+
var $div = $("#sensors");
26+
var $myNewElement = $("<p>New element</p>");
27+
$myNewElement.appendTo($div);
7028
}
7129

72-
// print the time the refresh happened
73-
var dt = new Date();
74-
document.getElementById("time").innerHTML= dt.toLocaleTimeString();
30+
7531
});
7632
</script>
7733
</body>

mqtt/server-stats.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ mqttclient.on('connect', function() {
6060
redisClient.set("powercumulativeToday", powercumulativeToday);
6161
redisClient.set("powercumulativeHour", powercumulativeHour);
6262
// console.log("duration ", duration, "period ", powerused, "today ", powercumulativeToday, "hour ", powercumulativeHour);
63-
mqttclient.publish("sensors/power/0powercumulativeToday", powercumulativeToday.toString());
64-
mqttclient.publish("sensors/power/0powercumulativeHour", powercumulativeHour.toString());
63+
mqttclient.publish("sensors/power/0powercumulativeToday", powercumulativeToday.toFixed(2));
64+
mqttclient.publish("sensors/power/0powercumulativeHour", powercumulativeHour.toFixed(2));
6565
powerlasttime = powercurrenttime;
6666
}
6767
});

mqtt/server.js

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,12 @@ var fs = require('fs');
99
var config = require('./config.json');
1010

1111

12-
var connectionCount = 0;
13-
14-
1512

1613
app.get('/app.css', function (req, res) {
1714
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
1815
res.sendfile(__dirname + '/app.css');
1916
});
2017
app.get('/stats', function(req, res){
21-
res.send(connectionCount + " clients connected");
2218
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
2319
console.log('This process is pid ' + process.pid + " with an uptime of " + process.uptime());
2420
console.log('Running on ' + process.platform + ' (' + process.arch + ')');
@@ -57,27 +53,21 @@ server.listen(8500);
5753
console.log('listening on port 8500');
5854

5955

60-
io.sockets.on('connection', function (socket) {
61-
connectionCount++;
62-
});
63-
io.sockets.on('disconnet', function (socket) {
64-
connectionCount--;
65-
});
66-
67-
68-
// subscribe to MQTT
69-
var mqtt = require('mqtt');
70-
var client = mqtt.createClient(1883, config.mqtt.host, function(err, client) {
71-
keepalive: 1000
72-
});
73-
console.log('connecting to mqtt on ' + config.mqtt.host + '(' + config.mqtt.port + ')');
7456

75-
client.on('connect', function() {
76-
client.subscribe('sensors/+/+');
77-
console.log('subscribing to sensors/+/+ on ' + config.mqtt.host + '(' + config.mqtt.port + ')');
78-
79-
client.on('message', function(topic, message) {
80-
// console.log('topic: ' + topic + ' payload: ' + message);
81-
io.sockets.emit('data', { topic: topic, value: message });
57+
io.sockets.on('connection', function (socket) {
58+
// subscribe to MQTT
59+
var mqtt = require('mqtt');
60+
var mqttclient = mqtt.createClient(1883, config.mqtt.host, function(err, client) {
61+
keepalive: 1000
62+
});
63+
64+
mqttclient.on('connect', function() {
65+
mqttclient.subscribe('sensors/+/+');
66+
// console.log('subscribing to sensors/+/+ on ' + config.mqtt.host + '(' + config.mqtt.port + ')');
67+
68+
mqttclient.on('message', function(topic, message) {
69+
// console.log('emitting topic: ' + topic + ' payload: ' + message);
70+
socket.emit('data', { topic: topic, value: message });
71+
});
8272
});
8373
});

0 commit comments

Comments
 (0)