-
Notifications
You must be signed in to change notification settings - Fork 1
/
mppt.py
executable file
·201 lines (165 loc) · 4.77 KB
/
mppt.py
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
#!/usr/bin/env python
"""
BlueSolar MPPT Communication Library
Copyright (C) 2015 Ace Internet Services Pty Ltd
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Usage:
./mppt.py <host> <port> [--no-checksum]
See also:
cacti.py (for insertion into Cacti)
Note that BlueSolar MPPT devices are not network-enabled. To
network-enable a device, use a MikroTik router to access the serial
port and expose as a TCP port.
One way to do this is to use a TTL-to-USB serial cable. Note that the
cable supplied by Victron is not supported by the MikroTik. However,
other chipsets such as FTDI work with the BlueSolar controller as well as
MikroTik RouterOS.
"""
import sys
import socket
class MPPT(object):
""" BlueSolar MPPT Communications Class
usage:
m = MPPT(('192.168.88.1', 26001))
s = m.status()
print s
# output:
LOAD ON
H19 94
VPV 19.59
ERR 0
FW 116
I 3.12
H21 114
IL 0.5
PID 41026
H20 8
H23 70
H22 22
HSDS 7
SER# XXXXXXXXXXX
V 14.39
CS 4
PPV 53
"""
def __init__(self, target, timeout=5, validate_checksum=True):
""" Constructor: opens connection to BlueSolar MPPT controller.
usage for TCP socket: MPPT(('192.168.88.1', 26001))
usage for serial: MPPT('/dev/ttyS0')
"""
self.validate_checksum = validate_checksum
if isinstance(target, (list, tuple)): # socket connection
for res in socket.getaddrinfo(target[0], target[1], socket.AF_UNSPEC, socket.SOCK_STREAM):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error, msg:
s = None
continue
try:
s.connect(sa)
except socket.error, msg:
s.close()
s = None
continue
break
if s is None:
raise Exception('could not open socket')
s.settimeout(timeout)
self.comm = s.makefile() # returns file-like object
elif isinstance(target, basestring): # file I/O based (tty) connection
s = open(target, 'r+b')
if s is None:
raise Exception('could not open serial port')
self.comm = s
else:
raise Exception('could not understand what you want to connect to')
def status(self):
""" Reads the MPPT status in ASCII mode
Because it's a serial connection, corrupted lines
are tolerated by skipping them and retrying until
a checksum-passing series of lines is found.
"""
failures_remaining = 30
while failures_remaining > 0:
failures_remaining -= 1
# rolling checksum
checksum = 0
status = {}
while True:
line = self.comm.readline().strip()
# handling a corrupted line, in case we start listening to the stream mid-line
if '\t' not in line:
break
# if it doesn't split cleanly into exactly two fields, skip
try:
key, value = line.split('\t')
except ValueError:
break
if key == 'PID':
status[key] = int(value, 16) # convert from hex string
elif key in (
'V',
'VPV',
'I',
'IL',):
status[key] = float(value) / 1000 # convert from milli
elif key in (
'FW',
'PPV',
'CS',
'ERR',
'H19',
'H20',
'H21',
'H22',
'H23',
'HSDS',):
status[key] = int(value) # convert from decimal str to int
elif key == 'Checksum':
pass
else:
status[key] = value
checksum += ord('\r') + ord('\n')
for char in line:
checksum += ord(char)
if key == 'Checksum':
# adding custom calculated fields not from controller:
if 'IPV' not in status.keys() \
and 'VPV' in status.keys() \
and 'PPV' in status.keys():
status['IPV'] = status['PPV'] / status['VPV']
if checksum % 256 == 0:
return status
else:
if self.validate_checksum:
break
else:
if failures_remaining < 2:
return status
raise Exception('ran out of parse attempts')
if __name__ == '__main__':
sys.argv.pop(0) # argv[0] is the script name
host = sys.argv.pop(0)
port = sys.argv.pop(0)
validate_checksum = True
try:
while True:
arg = sys.argv.pop(0)
if arg == '--no-checksum':
validate_checksum = False
except IndexError:
pass
m = MPPT((host, port), validate_checksum=validate_checksum)
status = m.status()
for key, value in status.iteritems():
print('%s\t%s' % (key, value))