Skip to content

Commit d704b3b

Browse files
committed
Improvements!
1 parent 3c39c16 commit d704b3b

File tree

5 files changed

+54
-33
lines changed

5 files changed

+54
-33
lines changed

mqtt/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"private": true,
66
"dependencies": {
77
"express": "3.x",
8+
"execSync": "0.0.4",
89
"socket.io": "*",
910
"smoothie": "*",
1011
"redis : "0.8.*",

mqtt/pages/app.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ p {
1111
table, th, td {
1212
border: 1px solid #CABFAF;
1313
border-spacing: 3px;
14+
table-layout:fixed;
1415
border-collapse: collapse;
1516
vertical-align: top;
16-
table-layout: fixed;
1717
}
1818

1919

mqtt/pages/sensors.html

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,48 @@
1111
<span id="sensors/power/0"> </span>W
1212
</big></big></big>
1313
<P>
14-
<span id="sensors/power/0/cumulative/today"></span> KWh used today
14+
<span id="sensors/power/0/cumulative/daily"></span> KWh used today
1515
<br>
1616
<span id="sensors/power/0/cumulative/hour"></span> KWh this hour
1717
<P>
1818
<canvas id="mycanvas" width="500" height="150"></canvas>
1919
<P>
20-
<table id="myTablePower"></table>
21-
<table id="myTableTemp"></table>
22-
<table id="myTableHumidity"></table>
23-
<table id="myTableNO2"></table>
24-
<table id="myTablePressure"></table>
25-
<table id="myTableCO"></table>
20+
<table style="width:60%">
21+
<tr>
22+
<td style="width:33%">
23+
<table id="myTablePower" style="width:100%"></table>
24+
</td>
25+
<td style="width:33%">
26+
</td>
27+
<td style="width:33%">
28+
</td>
29+
</tr>
30+
<tr>
31+
<td>
32+
<table id="myTableTemp" style="width:100%"></table>
33+
</td>
34+
<td>
35+
<table id="myTableHumidity" style="width:100%"></table>
36+
</td>
37+
<td>
38+
<table id="myTablePressure" style="width:100%"></table>
39+
</td>
40+
</tr>
41+
<tr>
42+
<td>
43+
<table id="myTableNO2" style="width:100%"></table>
44+
</td>
45+
<td>
46+
<table id="myTableCO" style="width:100%"></table>
47+
</td>
48+
<td>
49+
</td>
50+
</tr>
51+
</table>
2652
<P>
53+
2754
<div id="time" style="font-size:x-small"></div>
55+
2856
<script src="http://www.trease.eu:8500/socket.io/socket.io.js"></script>
2957
<script type="text/javascript" src="http://www.trease.eu:8500/jquery.min.js"></script>
3058
<script type="text/javascript" src="http://www.trease.eu:8500/smoothie.js"></script>
@@ -81,7 +109,10 @@
81109
var cell=row.insertCell(0);
82110
cell.id = data.topic + "name";
83111
document.getElementById(data.topic).style.textAlign="right"
112+
document.getElementById(data.topic).style.width="25%"
84113
document.getElementById(data.topic + "name").innerHTML= data.topic.slice(data.topic.lastIndexOf('/')+1);
114+
document.getElementById(data.topic + "name").style.textAlign="left"
115+
document.getElementById(data.topic + "name").style.width="75%"
85116
}
86117
// new we know there is a target, update it
87118
// console.log("Setting target " + data.topic + " to " + data.value);

mqtt/server-stats.js

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,16 @@ function log(type) {
1616
}
1717
}
1818

19-
// var powerlasttime = new Date(); // UNIX time in ms
20-
21-
// get last stored values from Redis
22-
// var execSync = require('execSync');
23-
// var result = execSync.exec('redis-cli get powercumulativeHour');
24-
// var powercumulativeHour = parseFloat(result.stdout);
25-
// var result = execSync.exec('redis-cli get powercumulativeToday');
26-
// var powercumulativeToday = parseFloat(result.stdout);
27-
28-
// var records_hourly = new Array();
29-
// var records_daily = new Array();
30-
// var records_lasttime = new Array();
31-
// var records_hourly = {};
19+
var savecount = 0;
3220
var records_hourly = {};
3321
var records_daily = {};
3422
var records_lasttime = {};
3523

24+
// get last stored values from Redis
3625
var execSync = require('execSync');
3726
var result = execSync.exec('redis-cli get records_hourly');
3827
records_hourly = JSON.parse(result.stdout);
3928
console.log("Loading hourly records from redis...");
40-
4129
result = execSync.exec('redis-cli get records_daily');
4230
records_daily = JSON.parse(result.stdout);
4331
console.log("Loading daily records from redis...");
@@ -74,25 +62,30 @@ mqttclient.on('connect', function() {
7462

7563
// different hour?
7664
if (records_lasttime[topic].getHours() != powercurrenttime.getHours()) {
77-
powercumulativeHour = 0;
65+
records_hourly[topic] = 0;
7866
}
7967
// different day?
8068
if (records_lasttime[topic].getDate() != powercurrenttime.getDate()) {
81-
powercumulativeDay = 0;
69+
records_daily[topic] = 0;
8270
}
8371
// calculate cumulative power used in KWh
8472
var duration = (powercurrenttime - records_lasttime[topic]) / 1000.0;
8573
var powerused = parseInt(message, 10) * (duration / 3600.0) / 1000.0; // convert to KWh
8674
records_lasttime[topic] = powercurrenttime;
8775
records_hourly[topic] += powerused;
8876
records_daily[topic] += powerused;
89-
console.log("topic:", topic, " duration ", duration, " period ", powerused, " hour ", records_hourly[topic], "daily ", records_daily[topic]);
77+
// console.log("topic:", topic, " duration ", duration, " period ", powerused, " hour ", records_hourly[topic], "daily ", records_daily[topic]);
9078

79+
// publish new data
9180
mqttclient.publish(topic + "/cumulative/hour", records_hourly[topic].toFixed(2));
9281
mqttclient.publish(topic + "/cumulative/daily", records_daily[topic].toFixed(2));
9382

94-
redisClient.set("records_hourly", JSON.stringify(records_hourly));
95-
redisClient.set("records_daily", JSON.stringify(records_daily));
96-
// console.log( JSON.stringify(records_hourly));
83+
// save to redis
84+
if (savecount++ > 24) {
85+
savecount = 0;
86+
redisClient.set("records_hourly", JSON.stringify(records_hourly));
87+
redisClient.set("records_daily", JSON.stringify(records_daily));
88+
// console.log("saving to redis...");
89+
}
9790
});
9891
});

mqtt/server.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@ app.use(function(req, res, next){
2525
});
2626

2727

28-
2928
server.listen(8500);
3029
console.log('listening on port 8500');
3130

3231

3332

34-
35-
36-
3733
io.of('/sensors').on('connection', function (socket) {
3834
// subscribe to MQTT
3935
var mqtt = require('mqtt');
@@ -42,7 +38,7 @@ io.of('/sensors').on('connection', function (socket) {
4238
});
4339
mqttclient.on('connect', function() {
4440
mqttclient.subscribe('sensors/+/+');
45-
mqttclient.subscribe('sensors/power/+/cumulative/+');
41+
mqttclient.subscribe('sensors/power/0/cumulative/+');
4642
// console.log('subscribing to sensors/+/+ on ' + config.mqtt.host + '(' + config.mqtt.port + ')');
4743

4844
mqttclient.on('message', function(topic, message) {

0 commit comments

Comments
 (0)