forked from solmoller/eversolar-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eversolar.pl
executable file
·1342 lines (1103 loc) · 46.1 KB
/
eversolar.pl
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#! /usr/bin/perl
#
# bugs to fix : warnings, double entries to pvlog, max of the day
#
# Eversolar inverter data logger
# - Based on Steve Cliffe's <[email protected]> Eversolar PMU logger script (http://www.sjcnet.id.au/computers/eversolar-inverter-monitoring-with-linux)
# - Tested and known to work with the following inverters:
# -- TL1500AS Inverter connected to an ethernet to serial converter
# -- TL2000 (multiple) connected to a Raspberry Pi
# -- TL3000 (multiple) connected to a Raspberry Pi
# -- Volta TL3000
# - Supports upload of data to pvoutput.org. Please consider joining the "Eversolar perl loggers" team if you use this script - http://pvoutput.org/ladder.jsp?tid=485
#
# Kayne Richens <[email protected]>
#
# Version 0.1 - September 30 2011
# - first release
# Version 0.2 - October 03 2011
# - fixed bug that stopped script when inverter shut down at night
# - moved config options to a config file (eversolar.ini)
# - added support for direct serial comms (no eth2ser converter) - NOT TESTED!
# - added "-m 30" (max run time 30 seconds) to curl so script doesn't hang when pvoutput.org goes down
# Version 0.3 - October 12 2011
# - added support to upload generation to smartenergygroups.com (SEG)
# Version 0.4 - October 16 2011
# - added a http server, so the inverter can be accessed via the browser
# - experimental multiple inverter support started - not working yet
# Version 0.5 - October 26 2011
# - added paging to web server /log request
# Version 0.6 - November 18 2011
# - more fixes to inverter re-connect after overnight shutdown
# - added max daily PAC to web interface
# Version 0.7 - November 20 2011
# - added debugs to help with inverter re-connect after overnight shutdown
# - added the time that the max daily PAC occured to web interface
# Version 0.8 - November 21 2011
# - fixed bug in 0.7 that stopped inverter connecting
# Version 0.9 - November 23 2011
# - added a sleep after inverter wakes and powers up the ethernet to serial converter, to allow it time before initiating the connection
# - fixed the log display in the web interface when the log is not stored in the default location
# Version 0.10 - June 7 2012
# - added temp and volts (PV) to pvoutput.org upload
#
# Code move to http://code.google.com/p/eversolar-monitor/ - August 31 2012
#
#
# Version 0.11 - March 28 2013 by Claus Stenager
# - added summary of more inverters, requires change in index.html
# Version 0.12 - April 21 2013 by Henrik Jørgensen
# - added output to pv-log.com
# - fixed ripple on pvoutput with more inverters due to global min in pmu_log - hat tip leifnel
# - fixed temperature bug on negative centigrades - leifnel
# - faster connections from multiple inverters, as we are polling aggresively if no inverters are connected
# - It's now possible to specify an ini file from command line - Mads Lie Jensen
# - Moved web server back into mail pl file to encompass the ini file specified from command line
# - several minor changes, cleaning up code
# - fixed issue with max production today
# - stopped deleting the webfile when last inverter shut down, effectively enabling the webpage after inverter shut down
# - Added severity to logging, and only writes to logfile if debugging is set sufficiently high
# Severity 1 is high severity, severity 3 is an informal message, setting debug to 3 creates a very long logfile, and slows web interface with lots of unimportant messages
# Version 0.13 - April 21 2013 by Henrik Jørgensen
# - Now even faster connect, as it picks out serial numbers from database for immediate connection to inverters - Morten Friis
# - A few minor bugfixes
# Version 0.14 - Jan 31st 2015 by Henrik Jørgensen
# - Updated to handle total production over 6553,5 KWh by reading and adding two further bytes of production data
#
# Code move to https://github.com/solmoller/eversolar-monitor - August 20 2015
#
# Version 0.15 - Sep 5th 2015 by Henrik Jørgensen
# - Bugfix september 5th 2015 for serial numbers containing null
# Version 0.16 - January 21th 2017
# - added rolling 365 days production (not a year, as leap years will skew data)
# Version 0.17 - december 3rd 2017 by Henrik Jørgensen and Reneke43
# - updated rolling 365 days to update historical data more intelligently
# - added Domoticz integration supplied by Reneke43
# Version 0.171 - February 3rd 2017 by Henrik Jørgensen and Ctenberge
# - fixed comment bug in Domoticz integration
#
#
# Eversolar communications packet definition:
# 0xaa, 0x55, # header
# 0x00, 0x00, # source address
# 0x00, 0x00, # destination address
# 0x00, # control code
# 0x00, # function code
# 0x00, # data length
# 0x00...0x00 # data
# 0x00, 0x00 # checksum
#
use IO::Socket::INET;
use AppConfig;
use JSON;
use DBI;
use Net::FTP;
use File::Copy;
#use warnings;
our $config = AppConfig->new();
$config->define("eth2ser_ip_address=s");
$config->define("eth2ser_port=s");
$config->define("serial_port=s");
$config->define("pvoutput_enabled=s");
$config->define("pvoutput_api_key=s");
$config->define("pvoutput_system_id=s%");
$config->define("pvoutput_status_interval_mins=s");
$config->define("pvoutput_add_status_url=s");
$config->define("options_debug=s");
$config->define("options_query_inverter_secs=s");
$config->define("options_log_file=s");
$config->define("options_communication_method=s");
$config->define("options_strings=s");
$config->define("pvlog_enabled=s");
$config->define("pvlog_FTPServeradresse=s");
$config->define("pvlog_interval=s");
$config->define("pvlog_username=s");
$config->define("pvlog_Password=s");
$config->define("domoticz_enabled=s");
$config->define("domoticz_address=s");
$config->define("domoticz_port=s");
$config->define("domoticz_username=s");
$config->define("domoticz_password=s");
$config->define("domoticz_IDX=s");
$config->define("influxdb_enabled=s");
$config->define("influxdb_address=s");
$config->define("influxdb_port=s");
$config->define("influxdb_dbname=s");
$config->define("influxdb_panelname=s");
$config->define("seg_enabled=s");
$config->define("seg_upload_interval_mins=s");
$config->define("seg_site_id=s");
$config->define("seg_device=s");
$config->define("seg_power_stream=s");
$config->define("seg_energy_stream=s");
$config->define("seg_api_url=s");
$config->define("web_server_enabled=s");
$config->define("web_server_port=s");
$config->define("database_log=s");
#$config->file("eversolar.ini");
$config->define("configFile=s");
$config->args();
if ($config->configFile eq '') {
$config->configFile('eversolar.ini');
}
-e $config->configFile or die "Configfile '", $config->configFile, "' not found\n";
$config->file($config->configFile);
pmu_log("Severity 3, Configfile is: " . $config->configFile . "\n");
%CTRL_FUNC_CODES = (
"REGISTER" => { # CONTROL CODE 0x10
"OFFLINE_QUERY" => {
"REQUEST" => [0x10, 0x00],
"RESPONSE" => [0x10, 0x80]
},
"SEND_REGISTER_ADDRESS" => {
"REQUEST" => [0x10, 0x01],
"RESPONSE" => [0x10, 0x81]
},
"RE_REGISTER" => {
"REQUEST" => [0x10, 0x04],
"RESPONSE" => ""
}
},
"READ" => { # CONTROL CODE 0x11
"QUERY_INVERTER_ID" => {
"REQUEST" => [0x11, 0x03],
"RESPONSE" => [0x11, 0x83]
},
"QUERY_NORMAL_INFO" => {
"REQUEST" => [0x11, 0x02],
"RESPONSE" => [0x11, 0x82]
},
"QUERY_DESCRIPTION" => {
"REQUEST" => [0x11, 0x00],
"RESPONSE" => [0x11, 0x80]
},
},
"WRITE" => { # CONTROL CODE 0x12
},
"EXECUTE" => { # CONTROL CODE 0x13
}
);
if($config->options_strings == 1) {
%DATA_BYTES = (
"TEMP" => 0,
"E_TODAY" => 1,
"VPV" => 2,
"IPV" => 3,
"IAC" => 4,
"VAC" => 5,
"FREQUENCY" => 6,
"PAC" => 7,
"IMPEDANCE" => 8,
"NA_1" => 9,
"E_TOTAL" => 10,
"NA_2" => 11,
"HOURS_UP" => 12,
"OP_MODE" => 13
);
} elsif($config->options_strings == 2) {
%DATA_BYTES = (
"TEMP" => 0,
"E_TODAY" => 1,
"VPV" => 2,
"VPV2" => 3,
"IPV" => 4,
"IPV2" => 5,
"IAC" => 6,
"VAC" => 7,
"FREQUENCY" => 8,
"PAC" => 9,
"NA_0" => 10,
"NA_1" => 11, # NA_1 is actually upper bytes of total production
"E_TOTAL" => 12,
"NA_2" => 13,
"HOURS_UP" => 14,
"OP_MODE" => 15
);
} else {
die "Incorrect config option for 'strings'\n";
}
use constant START_INVERTER_ADDRESS => 0x10; # start at 10 - giving us 244 available addresses
use constant OUR_ADDRESS => 0x01;
our $sock = 0;
our $last_min = -1;
our $pvlog_last_min =-1;
our $e_last_wh = -1;
our $next_inverter_address = START_INVERTER_ADDRESS;
our %inverters;
our $dbh = DBI->connect("dbi:SQLite:db.sqlite");
##
## sub routines
##
##
## Web server package
##
if($config->web_server_enabled)
{
package SolarWebServer;
use HTTP::Server::Simple::CGI;
use base qw(HTTP::Server::Simple::CGI);
use JSON;
use AppConfig;
my %dispatch = (
'/' => \&index,
'/inverter-data' => \&inverter_data,
'/log' => \&log,
# ...
);
sub handle_request {
my $self = shift;
my $cgi = shift;
my $path = $cgi->path_info();
my $handler = $dispatch{$path};
if (ref($handler) eq "CODE") {
print "HTTP/1.0 200 OK\r\n";
$handler->($cgi);
} else {
print "HTTP/1.0 404 Not found\r\n";
print $cgi->header,
$cgi->start_html('Not found'),
$cgi->h1('Not found'),
$cgi->end_html;
}
}
sub index {
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
open my $fh, "<www/index.html";
my $data = do { local $/; <$fh> };
print $cgi->header,
$data;
}
sub inverter_data {
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
print $cgi->header('application/json');
open FILE, "</tmp/eversolar";
while(<FILE>) {
print $_;
}
}
sub log {
my $cgi = shift; # CGI.pm object
return if !ref $cgi;
my $line_limit = 100;
my $page = $cgi->param('page');
print $cgi->header;
open FILE, "<".$config->options_log_file;
@lines = reverse <FILE>;
my $count = 0;
foreach $line (@lines) {
if($count < (($page*$line_limit)+1)) {
$count++;
next;
}
print $line;
if($count == ($line_limit+($page*$line_limit))) {
last;
} else {
$count++;
}
}
}
}
##
## send packet to inverter
##
sub send_request {
# Don't send if not connected
if (!$sock) {
return FALSE;
};
my $destination_address = shift;
my ($ctrl_func_code, $data) = @_;
%ctrl_func_code = %$ctrl_func_code;
@data = @$data;
my $data_length = scalar(@data);
@tmp_packet = (0xAA, 0x55, OUR_ADDRESS, 0x00, 0x00, $destination_address, $ctrl_func_code{"REQUEST"}[0], $ctrl_func_code{"REQUEST"}[1], $data_length);
if($data_length) {
push(@tmp_packet, @data);
}
$tmp_packet_length = scalar(@tmp_packet);
# calculate checksum
my $checksum = 0;
for($i = 0; $i < $tmp_packet_length; $i++) {
$checksum += $tmp_packet[$i];
}
$packet = pack("C".$tmp_packet_length."n", (@tmp_packet, $checksum));
if($config->options_debug) {
print "sending packet to inverter... \n";
print_bytes($packet, length($packet));
}
if($config->options_communication_method eq "eth2ser") {
unless($sock->send($packet, scalar($packet))) {
pmu_log("Severity 1, Failed to write to socket: $!");
$sock = 0;
return 0;
}
} elsif($config->options_communication_method eq "serial") {
$sock->write($packet);
}
# allow time for inverter to respond
sleep 1;
if($ctrl_func_code{"RESPONSE"} ne "") {
if($config->options_communication_method eq "eth2ser") {
$sock->recv($response, 256);
#unless(defined $sock->recv($response, 256)) {
# pmu_log("Severity 1, Failed to read from socket: $!");
# $sock = 0;
# return 0;
#}
} elsif($config->options_communication_method eq "serial") {
($count_in, $response) = $sock->read(256);
}
if(length($response)) {
@recv_packet = unpack("C*", $response);
if($config->options_debug) {
print "received packet from inverter: \n";
print_bytes($response, length($response));
}
if(validate_checksum(@recv_packet)) {
my $len = length($response) - 11;
my ($ctrl_code, $func_code, @data) = unpack("xxxxxxCCxC".$len."xx", $response);
#use Data::Dumper;
#print Dumper(\@data);
if($ctrl_code == $ctrl_func_code{"RESPONSE"}[0] &&
$func_code == $ctrl_func_code{"RESPONSE"}[1]) {
return pack("C*", @data);
}
}
}
} else {
return 1;
}
# if we fall through to here, something isn't right!
return 0;
}
##
## Dump a buffer in hex for debugging purposes
##
sub print_bytes {
my $buf = shift;
my $len = shift;
my $line_count = 0;
if ($len <= 0) {
return;
}
my @bytes = unpack("C$len", $buf);
if ($len > 0) {
for (my $i=0; $i<$len; $i++) {
printf "%02x ", $bytes[$i];
if ($line_count++ > 15) {
$line_count = 0;
print "\n";
}
}
}
printf "\n";
}
##
## Connect to the ethernet-to-serial converter attached to the inverter
##
sub eth2ser_connect {
# make sure the sock is closed
shutdown($sock, 2);
# connect to ethernet to serial converter attached to inverter
# loop until it connects
do {
$sock = new IO::Socket::INET (
PeerAddr => $config->eth2ser_ip_address,
PeerPort => $config->eth2ser_port,
Proto => 'tcp',
);
if ($sock) {
pmu_log("Severity 1, Connected to the ethernet to serial converter");
} else {
pmu_log("Severity 2, Unable to connect to the ethernet to serial converter ... sleeping 60 seconds");
sleep 60;
}
} until ($sock);
my $timeo = pack("l!l!", 1, 0);
$sock->sockopt(SO_RCVTIMEO, $timeo);
$sock->sockopt(SO_SNDTIMEO, $timeo);
# sleep to allow inverter time to ready itself for connection
sleep 10;
}
##
## Connect directly to the inverter via serial
##
sub serial_connect {
if ($^O eq 'MSWin32') { # Win32 (ActivePerl)
eval "use Win32::SerialPort";
$sock = Win32::SerialPort->new ($config->serial_port, 0, '') || die "Can\'t open $port: $!";
} else { # Unix/Linux/other
eval "use Device::SerialPort";
$sock = Device::SerialPort->new ($config->serial_port, 0, '') || die "Can\'t open $port: $!";
}
$sock->baudrate(9600) || die 'fail setting baudrate, try -b option';
$sock->parity("none") || die 'fail setting parity';
$sock->databits(8) || die 'fail setting databits';
$sock->stopbits(1) || die 'fail setting stopbits';
$sock->handshake("none") || die 'fail setting handshake';
$sock->datatype("raw") || die 'fail setting datatype';
$sock->write_settings || die 'could not write settings';
$sock->read_char_time(0); # don't wait for each character
$sock->read_const_time(1000); # 1 second per unfulfilled "read" call
}
##
## send the re-register packet to inverters 8 times (happens after initial connect)
##
sub re_register_inverters {
# ask all inverters to drop their registration and respond to register requests - send packet 3 times as per protocol specs
for(my $i=0; $i<8; $i++) {
unless(send_request(0x00, $CTRL_FUNC_CODES{"REGISTER"}{"RE_REGISTER"})) {
pmu_log("Severity 2, Failed to send re-register message");
return 0;
}
}
return 1;
}
##
## send the register packet to any inverters out there, and register inverter that responds first
##
sub register_inverter {
# request serial numbers
$serial_num_response = send_request(0x00, $CTRL_FUNC_CODES{"REGISTER"}{"OFFLINE_QUERY"});
if($serial_num_response) { ##bugfix september 2nd 2015 for serial numbers containing null: Z* instead of A*:
my $serial_number = unpack("Z*", $serial_num_response);
pmu_log("Severity 1, Found serial number: $serial_number");
# send register address (serial number followed by 1 byte address we allocate to inverter)
my @register_address = unpack("C*", $serial_num_response);
push(@register_address, $next_inverter_address);
$address_response = send_request(0x00, $CTRL_FUNC_CODES{"REGISTER"}{"SEND_REGISTER_ADDRESS"}, \@register_address);
if($address_response) {
# check register acknowledgement
my $len = length($address_response);
my $register_acknowledgement = unpack("C", $address_response);
if($register_acknowledgement == 06) {
pmu_log("Severity 1, Inverter acknowledged registration");
# request inverter ID
$inverter_id_response = send_request($next_inverter_address, $CTRL_FUNC_CODES{"READ"}{"QUERY_INVERTER_ID"});
if($inverter_id_response) {
my $len = length($inverter_id_response);
my $inverter_id = unpack("A*", $inverter_id_response);
pmu_log("Severity 1, Connected to inverter: $inverter_id");
# remember this inverter
$timestamp = get_timestamp();
$inverters{$next_inverter_address}{"id_string"} = $inverter_id;
$inverters{$next_inverter_address}{"serial"} = $serial_number;
$inverters{$next_inverter_address}{"connected"} = $timestamp;
$inverters{$next_inverter_address}{"max"} = {
"pac" => {
"watts" => 0,
"timestamp" => $timestamp
}
};
$next_inverter_address++;
} else {
pmu_log("Severity 2, No response to 'query inverter id' request for inverter $serial_number");
}
} else {
pmu_log("Severity 1, Inverter register acknowledgement incorrect for inverter $serial_number. Expected 06, received $register_acknowledgement");
}
} else {
pmu_log("Severity 2, No response to 'send register address' request for inverter $serial_number");
}
} else {
pmu_log("Severity 3, No response to 'offline query' request - no offline inverters");
}
}
##
## Try to register known inverter
##
sub register_known_inverter {
my $serial_number = shift;
pmu_log("Severity 2: Try to register known serial number: $serial_number");
# send register address (serial number followed by 1 byte address we allocate to inverter)
$serial_num_response = pack("A*", $serial_number);
my @register_address = unpack("C*", $serial_num_response);
push(@register_address, $next_inverter_address);
$address_response = send_request(0x00, $CTRL_FUNC_CODES{"REGISTER"}{"SEND_REGISTER_ADDRESS"}, \@register_address);
if($address_response) {
# check register acknowledgement
my $len = length($address_response);
my $register_acknowledgement = unpack("C", $address_response);
if($register_acknowledgement == 06) {
pmu_log("Severity 1:Inverter $serial_number acknowledged registration");
# request inverter ID
$inverter_id_response = send_request($next_inverter_address, $CTRL_FUNC_CODES{"READ"}{"QUERY_INVERTER_ID"});
if($inverter_id_response) {
my $len = length($inverter_id_response);
my $inverter_id = unpack("A*", $inverter_id_response);
pmu_log("Severity 1: Connected to inverter: $inverter_id");
# remember this inverter
$timestamp = get_timestamp();
$inverters{$next_inverter_address}{"id_string"} = $inverter_id;
$inverters{$next_inverter_address}{"serial"} = $serial_number;
$inverters{$next_inverter_address}{"connected"} = $timestamp;
$inverters{$next_inverter_address}{"max"} = {
"pac" => {
"watts" => 0,
"timestamp" => $timestamp
}
};
$next_inverter_address++;
} else {
pmu_log("Severity 2: No response to 'query inverter id' request for inverter $serial_number");
}
} else {
pmu_log("Severity 1: Inverter register acknowledgement incorrect for inverter $serial_number. Expected 06, received $register_acknowledgement");
}
} else {
pmu_log("Severity 2: No response to 'send register address' request for inverter $serial_number");
}
}
##
## Write a log file entry
##
sub pmu_log {
my $msg = shift;
(my $sec,my $min,my $hour,my $mday,my $mon,my $year,my $wday,my $yday,my $isdst)=localtime(time);
my $timestamp = sprintf ("%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
# 9th letter is the severity print substr($msg,9,1);
if($config->options_debug >= substr($msg,9,1)) {
print sprintf("%s: %s\n", $timestamp, $msg);
open(OUT, ">>".$config->options_log_file) or die "Cannot open file ".$config->options_log_file." for writing\n";
printf(OUT "%s: %s\n", $timestamp, $msg);
close OUT;
}
}
##
## Validate data packet - calculate and verify checksum
##
sub validate_checksum {
my @packet = @_;
my $csum = 0;
my $len = scalar(@packet);
for($i = 0; $i < $len-2; $i++) {
$csum += $packet[$i];
}
# packets with all zeroes pass checksum test, but are invalid
return ($csum > 0) && ($csum == (($packet[$len-2] << 8) + $packet[$len-1]));
}
##
## Parse data packet - put two bytes together
##
sub parse_packet {
my @packet = @_;
my @data;
my $j = 0;
my $len = scalar(@packet);
for($i = 0; $i < $len; $i+=2) {
$data[$j++] = ($packet[$i] << 8) + $packet[($i+1)];
}
return @data;
}
##
## Write out a JSON file for web server
##
sub write_web_json {
$json_text = encode_json \%inverters;
open(OUT, ">/tmp/eversolar") or die "Cannot open file /tmp/eversolar for writing\n";
printf(OUT "%s", $json_text);
close OUT;
}
##
## Upload data to PV-log.com
##
sub upload_pvlog {
# pv-log here
# We've accumulated data, send data to PV-log, this could be done in more detail, specs here
# http://photonensammler.homedns.org/wiki/doku.php?id=solarlog_datenformat#datendatei_min_dayjs
print "uploading to pv-log\n";
}
##
## Establish socket connection to inverter
##
sub inverter_connect {
my $connected = 0;
while(!$connected) {
if($config->options_communication_method eq "eth2ser") {
pmu_log("Severity 1, Connecting to the ethernet to serial converter");
eth2ser_connect();
} elsif($config->options_communication_method eq "serial") {
pmu_log("Severity 2, Connecting to the serial port");
serial_connect();
}
$next_inverter_address = START_INVERTER_ADDRESS;
pmu_log("Severity 2, Asking all inverters to re-register");
$connected = re_register_inverters();
}
# reset some "last" vars
$last_min = -1;
$e_last_wh = -1;
}
##
## get current timestamp
##
sub get_timestamp {
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
return sprintf ("%04d-%02d-%02d %02d:%02d:%02d", $year+1900, $mon+1, $mday, $hour, $min, $sec);
}
##
## START OF PROGRAM
##
##
## Setup sqlite database
##
$dbh->do("CREATE TABLE IF NOT EXISTS inverter (
serial_number VARCHAR(128),
timestamp VARCHAR(64),
pac INT,
e_today FLOAT,
e_total FLOAT,
vpv FLOAT,
vpv2 FLOAT,
ipv FLOAT,
ipv2 FLOAT,
vac FLOAT,
iac FLOAT,
frequency FLOAT,
impedance FLOAT,
hours_up FLOAT,
op_mode INT,
temp FLOAT
)");
###############################################################################
##
##
##
## Database housekeeping
##
##
##
##
##
###############################################################################
# vpv2 and ipv2 were added in later versions - fix any existing databases that don't have the columns
print "Update old database version, or print 2 fail messages : \n";
$dbh->do("ALTER TABLE inverter ADD COLUMN vpv2 FLOAT") or 1;
$dbh->do("ALTER TABLE inverter ADD COLUMN ipv2 FLOAT") or 1;
print "Done updating old database version. \n";
# since version 0.16 we add a second table with daily production levels including a rolling 365 day production volume
# First we create the table, and then we populate it with historical data. This may take a little while first time around
#
pmu_log("Severity 3, Checking database");
my $stmt = " SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='daily'";
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $DBI::errstr;
my $count = $sth->fetchrow_array();
if($count < 1){
print $DBI::errstr;
# ok, old tadabase without the daily table.
# create table and fill in data
pmu_log("Severity 1, Updating database with daily table, making data export and stats easy");
my $stmt = " create table daily (
serial_number varchar(128) not null,
timestamp varchar(64) not null,
e_today float,
e_total float,
unique (serial_number, timestamp)
)";
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $DBI::errstr;
my $stmt = " insert into daily (serial_number, timestamp, e_today,e_total)
select serial_number, date(timestamp),e_today, max(e_total) from inverter group by serial_number,date(timestamp) ";
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $DBI::errstr;
}
###############################################################################
##
##
##
## Web server
##
##
##
##
##
###############################################################################
if($config->web_server_enabled) {
# require "web-server.pl";
# start the web server on port $config->web_server_port
our $web_server_pid = SolarWebServer->new($config->web_server_port)->background();
$SIG{INT} = sub {
syswrite(STDERR, "\nCaught INT signal...\n");
syswrite(STDERR, "Shutting down web server (pid $web_server_pid)...\n");
my $cmd = `kill -9 $web_server_pid`;
syswrite(STDERR, "Exiting.\n");
exit;
};
# tmp file for web server
$json_text = encode_json \%inverters;
open(OUT, ">/tmp/eversolar") or die "Cannot open file /tmp/eversolar for truncating\n";
printf(OUT "%s", $json_text);
close OUT;
}
###############################################################################
##
##
##
## Main Loop
##
##
##
##
##
###############################################################################
while(1) {
$timestamp = get_timestamp();
# connect to inverter if not connected (!$sock)
my $combined_power = 0;
my $combined_daykwh = 0;
my $d365 =0;
if(!$sock) {
inverter_connect();
sleep 10;
my $sth = $dbh->prepare("select distinct serial_number from inverter limit 10");
$sth->execute();
my $row;
while ($row = $sth->fetchrow_arrayref()) {
pmu_log("Severity 2: got serial @$row[0] from database");
register_known_inverter( @$row[0]);
sleep 10;
}
$sth->finish();
}
#aggresive polling when no inverters connected
while(keys(%inverters)==0) {
register_inverter();
}
$timestamp = get_timestamp();
# see if there are any new inverters every once in a while (every minute)
if($min != $last_min) {
pmu_log("Severity 2, Asking for any inverters to register");
register_inverter();
}
# request data from each connected inverter
$combined_power = 0;
$combined_daykwh = 0;
foreach $inverter (keys(%inverters)) {
$response = send_request($inverter, $CTRL_FUNC_CODES{"READ"}{"QUERY_NORMAL_INFO"});
if($response) {
# good response - reset response_timeout_count
$inverters{$inverter}{"response_timeout_count"} = 0;
my $len = length($response);
my @data = parse_packet(unpack("C$len", $response));
my $e_today_kwh = $data[$DATA_BYTES{'E_TODAY'}]/100;
my $e_today_wh = $e_today_kwh * 1000;
my $e_total = $data[$DATA_BYTES{'E_TOTAL'}]/10 +$data[$DATA_BYTES{'NA_1'}]*65535/10; # NA_1 is actually upper bytes of total production
my $pac = $data[$DATA_BYTES{'PAC'}];
if ($data[$DATA_BYTES{'TEMP'}]>=0x8000) {$data[$DATA_BYTES{'TEMP'}]-=0x10000;} # Temperature is signed, -0.1 = 0xFFFF
my $temp = $data[$DATA_BYTES{'TEMP'}]/10;
my $volts = $data[$DATA_BYTES{'VAC'}]/10;
my $dc_volt = $data[$DATA_BYTES{'VPV'}]/10,
my $dc_imp = $data[$DATA_BYTES{'IPV'}]/10,
my $ac_volt = $data[$DATA_BYTES{'VAC'}]/10,
my $ac_imp = $data[$DATA_BYTES{'IAC'}]/10,
$combined_power = $combined_power +$data[$DATA_BYTES{'PAC'}];
$combined_daykwh = $combined_daykwh +$data[$DATA_BYTES{'E_TODAY'}]/100;
$d365 = $e_total ;
# store in db
if($config->database_log) {
print "select min(e_total) as mins from inverter where timestamp >= date('now', '-1 year') and serial_number = ".$inverters{$inverter}{"serial"};
my $stmt = "select min(e_total) as mins from inverter where timestamp >= date('now', '-365 day') and serial_number = '".$inverters{$inverter}{"serial"}."'";
my $sth = $dbh->prepare( $stmt );
my $rv = $sth->execute() or die $DBI::errstr;
if($rv < 0){
print $DBI::errstr;
}
while(my @row = $sth->fetchrow_array()) {
print "etot = ". $row[0] . "\n";
$d365 = $e_total-$row[0] ;
}
print "Operation done successfully\n";
pmu_log("Severity 3, ".$inverters{$inverter}{"serial"}." output: $pac W, Total: $e_total kWh, Today: $e_today_kwh kWh, 365 days : $d365 ");
# store in db
$inverters{$inverter}{"data"} = {
"timestamp" => $timestamp,
"pac" => $data[$DATA_BYTES{'PAC'}],
"e_today" => $e_today_kwh,
"e_total" => $e_total,
"vpv" => $data[$DATA_BYTES{'VPV'}]/10,
"vpv2" => $data[$DATA_BYTES{'VPV2'}]/10,
"ipv" => $data[$DATA_BYTES{'IPV'}]/10,
"ipv2" => $data[$DATA_BYTES{'IPV2'}]/10,
"vac" => $data[$DATA_BYTES{'VAC'}]/10,
"iac" => $data[$DATA_BYTES{'IAC'}]/10,
"frequency" => $data[$DATA_BYTES{'FREQUENCY'}]/100,
"d365" => $d365,
"impedance" => $data[$DATA_BYTES{'IMPEDANCE'}],
"hours_up" => $data[$DATA_BYTES{'HOURS_UP'}],
"op_mode" => $data[$DATA_BYTES{'OP_MODE'}],
"temp" => $temp,
"total_power" => $combined_power,
"total_daykwh" => $combined_daykwh
};
# store in db
$dbh->do("
INSERT INTO inverter
(serial_number,