forked from evertonsims/canadianSolar-pvoutput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcanadian_reads.py
331 lines (286 loc) · 12.5 KB
/
canadian_reads.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
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import requests
from datetime import datetime
from pytz import timezone
from time import sleep, time
from configobj import ConfigObj
from pyowm import OWM
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
# read settings from config file
config = ConfigObj("pvoutput.txt")
SYSTEMID = config['SYSTEMID']
APIKEY = config['APIKEY']
OWMKey = config['OWMKEY']
OWMLon = float(config['Longitude'])
OWMLat = float(config['Latitude'])
LocalTZ = timezone(config['TimeZone'])
# Local time with timezone
def localnow():
return datetime.now(tz=LocalTZ)
class Inverter(object):
def __init__(self, address, port):
"""Return a Inverter object with port set to *port* and
values set to their initial state."""
self._inv = ModbusClient(method='rtu', port=port, baudrate=9600, stopbits=1,
parity='N', bytesize=8, timeout=1)
self._unit = address
# Inverter properties
self.date = timezone('UTC').localize(datetime(1970, 1, 1, 0, 0, 0))
self.status = -1
self.pv_power = 0.0
self.pv_volts = 0.0
self.ac_volts = 0.0
self.ac_power = 0.0
self.wh_today = 0
self.wh_total = 0
self.temp = 0.0
self.firmware = ''
self.control_fw = ''
self.model_no = ''
self.serial_no = ''
self.dtc = -1
self.cmo_str = ''
def read_inputs(self):
"""Try read input properties from inverter, return true if succeed"""
ret = False
if self._inv.connect():
# by default read first 45 registers (from 0 to 44)
# they contain all basic information needed to report
rr = self._inv.read_input_registers(0, 45, unit=self._unit)
if not rr.isError():
ret = True
self.date = localnow()
self.status = rr.registers[0]
if self.status != -1:
self.cmo_str = 'Status: '+str(self.status)
# my setup will never use high nibble but I will code it anyway
self.pv_power = float((rr.registers[1] << 16)+rr.registers[2])/10
self.pv_volts = float(rr.registers[3])/10
self.ac_power = float((rr.registers[11] << 16)+rr.registers[12])/10
self.ac_volts = float(rr.registers[14])/10
self.wh_today = float((rr.registers[26] << 16)+rr.registers[27])*100
self.wh_total = float((rr.registers[28] << 16)+rr.registers[29])*100
self.temp = float(rr.registers[32])/10
else:
self.status = -1
ret = False
self._inv.close()
else:
print 'Error connecting to port'
ret = False
return ret
def version(self):
"""Read firmware version"""
ret = False
if self._inv.connect():
# by default read first 45 holding registers (from 0 to 44)
# they contain more than needed data
rr = self._inv.read_holding_registers(0, 45, unit=self._unit)
if not rr.isError():
ret = True
# returns G.1.8 on my unit
self.firmware = \
str(chr(rr.registers[9] >> 8) + chr(rr.registers[9] & 0x000000FF) +
chr(rr.registers[10] >> 8) + chr(rr.registers[10] & 0x000000FF) +
chr(rr.registers[11] >> 8) + chr(rr.registers[11] & 0x000000FF))
# does not return any interesting thing on my model
self.control_fw = \
str(chr(rr.registers[12] >> 8) + chr(rr.registers[12] & 0x000000FF) +
chr(rr.registers[13] >> 8) + chr(rr.registers[13] & 0x000000FF) +
chr(rr.registers[14] >> 8) + chr(rr.registers[14] & 0x000000FF))
# does match the label in the unit
self.serial_no = \
str(chr(rr.registers[23] >> 8) + chr(rr.registers[23] & 0x000000FF) +
chr(rr.registers[24] >> 8) + chr(rr.registers[24] & 0x000000FF) +
chr(rr.registers[25] >> 8) + chr(rr.registers[25] & 0x000000FF) +
chr(rr.registers[26] >> 8) + chr(rr.registers[26] & 0x000000FF) +
chr(rr.registers[27] >> 8) + chr(rr.registers[27] & 0x000000FF))
# as per Growatt protocol
mo = (rr.registers[28] << 16) + rr.registers[29]
self.model_no = (
'T' + str((mo & 0XF00000) >> 20) + ' Q' + str((mo & 0X0F0000) >> 16) +
' P' + str((mo & 0X00F000) >> 12) + ' U' + str((mo & 0X000F00) >> 8) +
' M' + str((mo & 0X0000F0) >> 4) + ' S' + str((mo & 0X00000F))
)
# 134 for my unit meaning single phase/single tracker inverter
self.dtc = rr.registers[43]
else:
self.firmware = ''
self.control_fw = ''
self.model_no = ''
self.serial_no = ''
self.dtc = -1
ret = False
self._inv.close()
else:
print 'Error connecting to port'
ret = False
return ret
class Weather(object):
def __init__(self, API, lat, lon):
self._API = API
self._lat = float(lat)
self._lon = float(lon)
self._owm = OWM(self._API)
self.temperature = 0.0
self.cloud_pct = 0
self.cmo_str = ''
def get(self):
obs = self._owm.weather_at_coords(self._lat, self._lon)
w = obs.get_weather()
status = w.get_detailed_status()
self.temperature = w.get_temperature(unit='celsius')['temp']
self.cloud_pct = w.get_clouds()
self.cmo_str = ('%s with cloud coverage of %s percent' % (status, self.cloud_pct))
class PVOutputAPI(object):
def __init__(self, API, system_id=None):
self._API = API
self._systemID = system_id
self._wh_today_last = 0
def add_status(self, payload, system_id=None):
"""Add live output data. Data should contain the parameters as described
here: http://pvoutput.org/help.html#api-addstatus ."""
sys_id = system_id if system_id is not None else self._systemID
self.__call("https://pvoutput.org/service/r2/addstatus.jsp", payload, sys_id)
def add_output(self, payload, system_id=None):
"""Add end of day output information. Data should be a dictionary with
parameters as described here: http://pvoutput.org/help.html#api-addoutput ."""
sys_id = system_id if system_id is not None else self._systemID
self.__call("http://pvoutput.org/service/r2/addoutput.jsp", payload, sys_id)
def __call(self, url, payload, system_id=None):
headers = {
'X-Pvoutput-Apikey': self._API,
'X-Pvoutput-SystemId': system_id,
'X-Rate-Limit': '1'
}
# Make tree attempts
for i in range(3):
try:
r = requests.post(url, headers=headers, data=payload, timeout=10)
reset = round(float(r.headers['X-Rate-Limit-Reset']) - time())
if int(r.headers['X-Rate-Limit-Remaining']) < 10:
print("Only {} requests left, reset after {} seconds".format(
r.headers['X-Rate-Limit-Remaining'],
reset))
if r.status_code == 403:
print("Forbidden: " + r.reason)
sleep(reset + 1)
else:
r.raise_for_status()
break
except requests.exceptions.HTTPError as errh:
print(localnow().strftime('%Y-%m-%d %H:%M'), " Http Error:", errh)
except requests.exceptions.ConnectionError as errc:
print(localnow().strftime('%Y-%m-%d %H:%M'), "Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
print(localnow().strftime('%Y-%m-%d %H:%M'), "Timeout Error:", errt)
except requests.exceptions.RequestException as err:
print(localnow().strftime('%Y-%m-%d %H:%M'), "OOps: Something Else", err)
sleep(5)
else:
print(localnow().strftime('%Y-%m-%d %H:%M'),
"Failed to call PVOutput API after {} attempts.".format(i))
def send_status(self, date, energy_gen=None, power_gen=None, energy_imp=None,
power_imp=None, temp=None, vdc=None, cumulative=False, vac=None,
temp_inv=None, energy_life=None, comments=None, power_vdc=None,
system_id=None):
# format status payload
payload = {
'd': date.strftime('%Y%m%d'),
't': date.strftime('%H:%M'),
}
# Only report total energy if it has changed since last upload
# this trick avoids avg power to zero with inverter that reports
# generation in 100 watts increments (Growatt and Canadian solar)
if ((energy_gen is not None) and (self._wh_today_last != energy_gen)):
self._wh_today_last = int(energy_gen)
payload['v1'] = int(energy_gen)
if power_gen is not None:
payload['v2'] = float(power_gen)
if energy_imp is not None:
payload['v3'] = int(energy_imp)
if power_imp is not None:
payload['v4'] = float(power_imp)
if temp is not None:
payload['v5'] = float(temp)
if vdc is not None:
payload['v6'] = float(vdc)
if cumulative is not None:
payload['c1'] = 1
if vac is not None:
payload['v8'] = float(vac)
if temp_inv is not None:
payload['v9'] = float(temp_inv)
if energy_life is not None:
payload['v10'] = int(energy_life)
if comments is not None:
payload['m1'] = str(comments)[:30]
# calculate efficiency
if ((power_vdc is not None) and (power_vdc > 0) and (power_gen is not None)):
payload['v12'] = float(power_gen) / float(power_vdc)
# Send status
self.add_status(payload, system_id)
def main_loop():
# init
inv = Inverter(0x1, '/dev/ttyUSB0')
inv.version()
if OWMKey:
owm = Weather(OWMKey, OWMLat, OWMLon)
owm.fresh = False
else:
owm = False
pvo = PVOutputAPI(APIKEY, SYSTEMID)
# start and stop monitoring (hour of the day)
shStart = 5
shStop = 21
# Loop until end of universe
while True:
if shStart <= localnow().hour < shStop:
# get fresh temperature from OWM
if owm:
try:
owm.get()
owm.fresh = True
except Exception as e:
print 'Error getting weather: {}'.format(e)
owm.fresh = False
# get readings from inverter, if success send to pvoutput
inv.read_inputs()
if inv.status != -1:
# pvoutput(inv, owm)
# temperature report only if available
temp = owm.temperature if owm and owm.fresh else None
pvo.send_status(date=inv.date, energy_gen=inv.wh_today,
power_gen=inv.ac_power, vdc=inv.pv_volts,
vac=inv.ac_volts, temp=temp,
temp_inv=inv.temp, energy_life=inv.wh_total,
power_vdc=inv.pv_power)
# sleep until next multiple of 5 minutes
min = 5 - localnow().minute % 5
sleep(min*60 - localnow().second)
else:
# some error
sleep(60) # 1 minute before try again
else:
# it is too late or too early, let's sleep until next shift
hour = localnow().hour
minute = localnow().minute
if 24 > hour >= shStop:
# before midnight
snooze = (((shStart - hour) + 24) * 60) - minute
elif shStart > hour <= 0:
# after midnight
snooze = ((shStart - hour) * 60) - minute
print localnow().strftime('%Y-%m-%d %H:%M') + ' - Next shift starts in ' + \
str(snooze) + ' minutes'
sys.stdout.flush()
snooze = snooze * 60 # seconds
sleep(snooze)
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print >> sys.stderr, '\nExiting by user request.\n'
sys.exit(0)