forked from JackKelly/CurrentCostLogger
-
Notifications
You must be signed in to change notification settings - Fork 2
/
current-cost
executable file
·115 lines (90 loc) · 2.98 KB
/
current-cost
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#!/usr/bin/perl -w
# Reads data from a Current Cost device via USB port and logs it to a file
# and RRDtool round-robin database.
# Forked from Jack Kelly's Perl script: https://github.com/JackKelly/CurrentCostLogger which itself was…
# Based on Paul Mutton's excellent Perl script and tutorial: http://www.jibble.org/currentcost/
# with modifications from Jamie Bennett: http://www.linuxuk.org/2008/12/currentcost-and-ubuntu/
# This module requires the Device::SerialPort Perl module. Install this using Perl's package manager:
# At the Linux command line, type:
# sudo perl -MCPAN -e shell
# And then type:
# install Device::SerialPort
# Alternatively, in Debian/Ubuntu:
# apt-get install libdevice-serialport-perl
# Also requires RRDtool: http://oss.oetiker.ch/rrdtool/
# apt-get install rrdtool
# and Proc::Daemon
# apt-get install libproc-daemon-perl
use strict;
use Device::SerialPort qw( :PARAM :STAT 0.07 );
use POSIX qw(strftime);
use Proc::Daemon;
use sigtrap 'handler', \&clean_exit, 'normal-signals';
if ($ARGV[0] eq "-d") {
Proc::Daemon::Init;
open(PIDFILE, ">/var/run/current-cost.pid");
print PIDFILE "$$\n";
close PIDFILE;
}
my $DEBUG = $ENV{'DEBUG'};
$DEBUG = 0 unless $DEBUG;
my $SERIALPORT = $ENV{'SERIALPORT'};
$SERIALPORT = "/dev/ttyUSB0" unless $SERIALPORT;
my $LOGFILE = $ENV{'LOGFILE'};
$LOGFILE = "data.txt" unless $LOGFILE;
my $DUMP = $ENV{'DUMP'};
$DUMP = "input.xml" unless $DUMP;
my $DB = $ENV{'DB'};
$DB = "powertemp.rrd" unless $DB;
print "Current Cost logger\n";
print "SERIALPORT -> $SERIALPORT\n";
print "LOGFILE -> $LOGFILE\n";
print "DUMP -> $DUMP\n";
print "DB -> $DB\n";
my $ob = Device::SerialPort->new($SERIALPORT);
$ob->baudrate(57600);
$ob->write_settings;
open(SERIAL, "+>$SERIALPORT");
print "Opened serial port.\n";
print "Unix time\tPower\tDD/MM/YEAR HH:MM:SS\tt °C\n";
open(MYFILE, ">>$LOGFILE") if $DEBUG;
open(DUMP, ">>$DUMP") if $DEBUG;
$| = 1;
my $continue = 1;
# Check at most once per second
while ($continue) {
sleep 1;
while (my $line = <SERIAL>) {
print DUMP $line if $DEBUG;
if ($line =~ m!<time>(\d+):(\d+):(\d+)</time><tmpr> ?([0-9.-]+)</tmpr>.*<ch1><watts>(\d+)</watts></ch1>!) {
# For Perl regex help, see http://www.cs.tut.fi/~jkorpela/perl/regexp.html
# For Current Cost XML details, see http://www.currentcost.com/cc128/xml.htm
# Data from Current Cost EnviR device:
my $hours = $1;
my $mins = $2;
my $secs = $3;
my $temp = $4;
my $watts = $5;
# Get today's date from local Linux host computer
my $datetime = strftime('%d/%m/%Y %H:%M:%S', localtime());
my $unixtime = strftime('%s', localtime());
my $output = "$unixtime\t$watts\t$datetime\t$temp\n";
print $output if $DEBUG;
# Update RRDtool database
qx(rrdtool update $DB N:$watts:$temp);
# Log the output to file
print MYFILE $output if $DEBUG;
}
}
}
sub clean_exit {
$continue = 0;
close(MYFILE) if $DEBUG;
close(SERIAL);
close(DUMP) if $DEBUG;
print "Exiting\n";
exit 0;
}
# 25 months
# 90 days
# 746 hours