-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjunctek.pl
More file actions
executable file
·303 lines (269 loc) · 9.62 KB
/
junctek.pl
File metadata and controls
executable file
·303 lines (269 loc) · 9.62 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
# uses Net::MQTT::Simple if MQTT is enabled
$ENV{'PATH'} = "/usr/bin:/usr/sbin";
my $DEBUG = $ENV{'DEBUG'};
# BT MAC address of device to query
my $BT = "00:00:00:00:00:00";
# Friendly name for this device
# (used in the MQTT topic)
my $BT_friendly = "KG140F";
# set to 0 to disable MQTT completely
my $mqtt_enabled = 1;
my $mqtt_server = "localhost";
my $mqtt_retain = 0;
# basename for the MQTT topics,
# topics are ${mqtt_topic_base}/${BT_friendly}/key name
my $mqtt_topic_base = "junctek";
# leave empty if your mqtt server doesn't require auth
my $mqtt_username = "";
my $mqtt_passwd = "";
# set to 1 to skip over all the values for the
# battery protection settings.
# These are device configuration settings, not measurements
my $skip_protection_stats = 1;
my $help = 0;
my $quiet;
GetOptions(
'help|?' => \$help,
'quiet' => \$quiet,
'device=s' => \$BT,
'mqtt' => \$mqtt_enabled,
'server=s' => \$mqtt_server,
'username=s' => \$mqtt_username,
'password=s' => \$mqtt_passwd,
'name=s' => \$BT_friendly,
'retain' => \$mqtt_retain
);
&help if $help;
sub help {
print "\nUsage: $0 [OPTION?]\n\n";
print "Options:\n";
print "\t-d, --device=MAC\t\tSpecify remote Bluetooth address\n";
print "\t-q, --quiet\t\t\tDon't print output\n";
print "\t-m, --mqtt\t\t\tEnable (1) or disable (0) publishing to MQTT\n";
print "\t-s, --server\t\t\tHostname of MQTT server\n";
print "\t-u --username\t\t\tMQTT username\n";
print "\t-p --password\t\t\tMQTT password\n";
print
"\t-n, --name\t\t\t'Friendly' name of device, MQTT topic becomes junctek/[name]/... \n";
print "\t-r, --retain\t\t\tPublish MQTT messages with retain flag\n";
print "\t-h, --help\t\t\t<-- You are here\n";
print "\n\n";
exit(0);
}
my $mqtt;
if ($mqtt_enabled) {
use Net::MQTT::Simple;
$mqtt = Net::MQTT::Simple->new($mqtt_server);
if ($mqtt_username) {
$ENV{'MQTT_SIMPLE_ALLOW_INSECURE_LOGIN'} = 1;
$mqtt->login( $mqtt_username, $mqtt_passwd )
or warn "MQTT login failed\n";
}
}
my $val;
my %stats;
my $i = 0;
# how many packets to listen for, 40 appears to be one complete parameter set
my $max_pkts = 40;
# These are all the data types I've been able to identify,
# there's a bunch more still but I'm mainly interested in V/A/SoC/state
my %key = (
"b0", "Battery capacity",
"b1", "Over-temp protection",
"b7", "Relay mode",
"c0", "Volts",
"c1", "Amps",
"c2", "Protection delay sec",
"c3", "Protection recovery sec",
"c5", "Over-voltage protection",
"c6", "Under-voltage protection",
"c7", "Over-current protection",
"c8", "Over-current charge protection",
"c9", "Over-power protection",
"d0", "Relay state",
"d1", "Charge state",
"d2", "Ah remaining",
"d3", "KWh discharged",
"d4", "KWh charged",
"d5", "Total run time",
"d6", "Time remaining",
"d7", "Impedance",
"d8", "Watts",
"d9", "Temperature",
"e3", "Under-temp protection"
);
# First, request the device to send all values once,
# otherwise it only sends values as they change so some
# will never be sent, e.g. battery capacity etc.
# Note: this won't actually send the values, just
# trigger them to be sent once something starts listening to
# the datastream on handle 0x22.
my $rv = system(
"gatttool -b $BT --char-write-req --handle=0x0025 --value=0xbb9aa90cee >/dev/null"
);
if ( $rv > 0 ) { die "Connection failed\n"; }
# Next, request the datastream to start.
# The device will initially send all ~40 values once,
# then send values as they change.
open my $gatt,
"gatttool -b $BT --char-write-req --handle=0x0022 --value=0100 --listen|"
or die "failed to read from gatttool\n";
while (<$gatt>) {
chomp;
if ( $_ =~ m/value: (.*)$/ ) {
my $data = $1;
unless ( crc_check($data) ) {
# checkum failure
$DEBUG && print "(c/s failure)\n";
next;
}
my @d = split /\s/, $data;
foreach my $v (@d) {
next if ( $v eq 'ee' ); # end of record byte
if ( $v eq 'bb' ) {
# found a start of record byte, flush the buffer
$val = '';
next;
}
if ( $v =~ m/^\d+$/ ) {
$val .= $v;
}
else { # Parameter ID byte, calculate the value
if ( $v eq 'c0' ) { $val = $val / 100; }
elsif ( $v eq 'c1' ) {
$val = $val / 100;
no warnings;
if ( $stats{ $key{'d1'} } eq 'Discharging' ) {
$val = $val * -1;
}
use warnings;
}
elsif ( $v eq 'c5' ) { $val = $val / 100; }
elsif ( $v eq 'c6' ) { $val = $val / 100; }
elsif ( $v eq 'c7' ) { $val = $val / 100; }
elsif ( $v eq 'c8' ) { $val = $val / 100; }
elsif ( $v eq 'c9' ) { $val = $val / 100; }
elsif ( $v eq 'd0' ) {
if ( $val > 0 ) { $val = "off"; }
else { $val = "on"; }
}
elsif ( $v eq 'd1' ) {
if ( $val > 0 ) { $val = "Charging"; }
else {
$val = "Discharging";
if ( $stats{ $key{'c1'} } > 0 ) {
$stats{ $key{'c1'} } *= -1;
}
}
}
elsif ( $v eq 'b7' ) {
if ( $val > 0 ) { $val = "N/C"; }
else { $val = "N/O"; }
}
elsif ( $v eq 'd2' ) { $val = $val / 1000; }
elsif ( $v eq 'd3' ) { $val = $val / 100000; }
elsif ( $v eq 'd4' ) { $val = $val / 100000; }
elsif ( $v eq 'd5' ) {
$val = formatSeconds( { seconds => $val } );
}
elsif ( $v eq 'd6' ) {
$val = formatSeconds( { seconds => ( $val * 60 ) } );
}
elsif ( $v eq 'd7' ) { $val = $val / 100; }
elsif ( $v eq 'd8' ) {
$val = $val / 100;
no warnings;
if ( $stats{ $key{'d1'} } eq 'Discharging' ) {
$val = $val * -1;
}
use warnings;
}
elsif ( $v eq 'd9' ) { $val = $val - 100; }
elsif ( $v eq 'b0' ) { $val = $val / 10; }
elsif ( $v eq 'b1' ) { $val = $val - 100; }
elsif ( $v eq 'e3' ) { $val = $val - 100; }
if ( $stats{ $key{'b0'} } && $stats{ $key{'d2'} } ) {
# calculate state of charge if we have values
# for both battery capacity and Ah remaining
my $SoC =
( $stats{ $key{'d2'} } / $stats{ $key{'b0'} } ) * 100;
$stats{'SoC'} = $SoC;
}
no warnings; # expect lots of empty vars in debug output
$DEBUG && print $key{$v} . " [$v]" . ": " . $val . "\n";
use warnings;
if ( $key{$v} ) {
$stats{ $key{$v} } = $val;
}
if ( $i < $max_pkts ) {
$i++;
}
else {
foreach my $k_ ( sort keys %stats ) {
next unless ($k_);
if ($skip_protection_stats) {
next if ( $k_ =~ m/rotection/ );
}
unless ($quiet) { print "$k_: $stats{$k_}\n"; }
if ($mqtt_enabled) {
my $topic = $k_;
$topic =~ s/\s+/_/g;
if ($mqtt_retain) {
$mqtt->retain(
"$mqtt_topic_base/$BT_friendly/$topic" =>
$stats{$k_} );
}
else {
$mqtt->publish(
"$mqtt_topic_base/$BT_friendly/$topic" =>
$stats{$k_} );
}
}
}
exit;
}
$val = '';
}
}
}
}
# Convert seconds into hours:minutes:seconds
sub formatSeconds {
my ($args) = @_;
my $secs = $args->{seconds};
my ( $hours, $hourremainder ) =
( ( $secs / ( 60 * 60 ) ), $secs % ( 60 * 60 ) );
my ( $minutes, $seconds ) =
( int $hourremainder / 60, $hourremainder % 60 );
( $hours, $minutes, $seconds ) = (
sprintf( "%02d", $hours ),
sprintf( "%02d", $minutes ),
sprintf( "%02d", $seconds )
);
return $hours . ':' . $minutes . ':' . $seconds;
}
sub crc_check {
# checksum is the last byte before the end-of-packet byte
# and is stored as BCD :/
# Checksum is the modulo 100 of the sum of all bytes in the
# packet up to the checksum byte (including the start-of-packet byte).
my $packet = shift;
my @bytes = unpack( "(A2)*", $packet );
my $EoP = pop(@bytes);
my $cs = pop(@bytes);
my $sum = 0;
foreach my $byte (@bytes) {
$sum += hex($byte);
}
my $fsum = $sum % 100;
if ( $cs == $fsum ) {
return (1);
}
else {
return (0);
}
}