Skip to content

Commit 2283bd2

Browse files
committed
CSS for index.html and daily/hourly cumulative power consumpion
1 parent b051e32 commit 2283bd2

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

mqtt/app.css

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
body {
2+
font-family: Verdana,Tahoma,"DejaVu Sans",sans-serif;
3+
color: #201E1C;
4+
background-color: AntiqueWhite;
5+
}
6+
7+
p {
8+
width: 100%;
9+
}
10+
11+
table, th, td {
12+
border: 1px solid #CABFAF;
13+
}
14+
15+
16+
.clear {
17+
clear: both;
18+
}

mqtt/app.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ app.get('/hello.txt', function(req, res){
5656
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
5757
res.send('Hello World');
5858
});
59+
app.get('/app.css', function (req, res) {
60+
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
61+
res.sendfile(__dirname + '/app.css');
62+
});
5963
app.get('/stats', function(req, res){
6064
console.log('connection %j %s %s', req.connection.remoteAddress, req.method, req.url);
6165
res.send(timeSince(scriptStartDate));
@@ -74,6 +78,8 @@ app.get('/', function(req, res) {
7478

7579
});
7680

81+
82+
7783
// start the server
7884
server.listen(8500);
7985
console.log('listening on port 8500');
@@ -89,7 +95,8 @@ var client = mqtt.createClient(1883, 'localhost', function(err, client) {
8995
keepalive: 1000
9096
});
9197
// global variables for tracking cumulative power usage
92-
var powercumulative = 0;
98+
var powercumulativeToday = 0;
99+
var powercumulativeHour = 0;
93100
var powerlasttime = new Date(); // UNIX time in ms
94101

95102
client.on('connect', function() {
@@ -102,14 +109,19 @@ client.on('connect', function() {
102109
var powercurrenttime = new Date();
103110
// Is it now a different day from the last time this block ran?
104111
if (powerlasttime.getDate() != powercurrenttime.getDate()) {
105-
powerlasttime = 0;
112+
powercumulativeToday = 0;
113+
}
114+
if (powerlasttime.getHours() != powercurrenttime.getHours()) {
115+
powercumulativeHour = 0;
106116
}
107117
// caluclate cumlative power used in KWh
108118
var duration = (powercurrenttime - powerlasttime) / 1000.0;
109119
var powerused = parseInt(message, 10) * (duration / 3600.0) / 1000.0; // convert to KWh
110-
powercumulative += powerused;
120+
powercumulativeToday += powerused;
121+
powercumulativeHour += powerused;
111122
// console.log("duration ", duration, "powerused ", powerused, "cumulative ", powercumulative);
112-
io.sockets.emit('data', { topic: "powercumulative", value: powercumulative.toFixed(3) });
123+
io.sockets.emit('data', { topic: "powercumulativeToday", value: powercumulativeToday.toFixed(3) });
124+
io.sockets.emit('data', { topic: "powercumulativeHour", value: powercumulativeHour.toFixed(3) });
113125
powerlasttime = powercurrenttime;
114126
}
115127
});

mqtt/index.html

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,7 @@
44
<title>
55
Charlestown Power
66
</title>
7-
<style>
8-
body
9-
{
10-
background-color:AntiqueWhite;
11-
}
12-
table, th, td
13-
{
14-
border: 1px solid #CABFAF;
15-
}
16-
</style>
7+
<link type="text/css" rel="stylesheet" href="/app.css" media="all">
178
</head>
189
<body>
1910
Current total current power consumption <span id="sensors/power/0"></span>
@@ -34,7 +25,9 @@
3425
<table id="myTablePressure"></table>
3526
<table id="myTableCO"></table>
3627
<P>
37-
Total power consumed since start <span id="powercumulative"></span>
28+
Total power today <span id="powercumulativeToday"></span> KWh
29+
<P>
30+
Total power this hour <span id="powercumulativeHour"></span> KWh
3831
<P>
3932
<canvas id="mycanvas" width="500" height="100"></canvas>
4033
<P>

0 commit comments

Comments
 (0)