forked from meltaxa/solariot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolariot.py
executable file
·315 lines (277 loc) · 10.6 KB
/
solariot.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
#!/usr/bin/env python
# Copyright (c) 2017 Dennis Mellican
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian
from pymodbus.client.sync import ModbusTcpClient
from SungrowModbusTcpClient import SungrowModbusTcpClient
from influxdb import InfluxDBClient
import paho.mqtt.client as mqtt
import config
import dweepy
import json
import time
import datetime
import requests
from threading import Thread
MIN_SIGNED = -2147483648
MAX_UNSIGNED = 4294967295
requests.packages.urllib3.disable_warnings()
print ("Load config %s" % config.model)
# SMA datatypes and their register lengths
# S = Signed Number, U = Unsigned Number, STR = String
sma_moddatatype = {
'S16':1,
'U16':1,
'S32':2,
'U32':2,
'U64':4,
'STR16':8,
'STR32':16
}
# Load the modbus register map for the inverter
modmap_file = "modbus-" + config.model
modmap = __import__(modmap_file)
# This will try the Sungrow client otherwise will default to the standard library.
if 'sungrow-' in config.model:
print ("Load SungrowModbusTcpClient")
client = SungrowModbusTcpClient.SungrowModbusTcpClient(host=config.inverter_ip,
timeout=config.timeout,
RetryOnEmpty=True,
retries=3,
port=config.inverter_port)
else:
print ("Load ModbusTcpClient")
client = ModbusTcpClient(host=config.inverter_ip,
timeout=config.timeout,
RetryOnEmpty=True,
retries=3,
port=config.inverter_port)
print("Connect")
client.connect()
client.close()
if config.mqtt_enable:
try:
mqtt_client = mqtt.Client('pv_data')
if 'config.mqtt_username' in globals():
mqtt_client.username_pw_set(config.mqtt_username,config.mqtt_password)
mqtt_client.connect(config.mqtt_server, port=config.mqtt_port)
except:
mqtt_client = None
else:
mqtt_client = None
if config.influxdb_enable:
try:
flux_client = InfluxDBClient(config.influxdb_ip,
config.influxdb_port,
config.influxdb_user,
config.influxdb_password,
config.influxdb_database,
ssl=config.influxdb_ssl,
verify_ssl=config.influxdb_verify_ssl)
except:
flux_client = None
else:
flux_client = None
inverter = {}
bus = json.loads(modmap.scan)
def load_registers(type,start,COUNT=100):
try:
if type == "read":
rr = client.read_input_registers(int(start),
count=int(COUNT),
unit=config.slave)
elif type == "holding":
rr = client.read_holding_registers(int(start),
count=int(COUNT),
unit=config.slave)
if len(rr.registers) != int(COUNT):
print("[WARN] Mismatched number ({}) of registers read".format(len(rr.registers)))
return
for num in range(0, int(COUNT)):
run = int(start) + num + 1
if type == "read" and modmap.read_register.get(str(run)):
if '_10' in modmap.read_register.get(str(run)):
inverter[modmap.read_register.get(str(run))[:-3]] = float(rr.registers[num])/10
else:
inverter[modmap.read_register.get(str(run))] = rr.registers[num]
elif type == "holding" and modmap.holding_register.get(str(run)):
inverter[modmap.holding_register.get(str(run))] = rr.registers[num]
except Exception as err:
print("[WARN] No data. Try increasing the timeout or scan interval.")
## function for polling data from the target and triggering writing to log file if set
#
def load_sma_register(registers):
from pymodbus.payload import BinaryPayloadDecoder
from pymodbus.constants import Endian
import datetime
## request each register from datasets, omit first row which contains only column headers
for thisrow in registers:
name = thisrow[0]
startPos = thisrow[1]
type = thisrow[2]
format = thisrow[3]
## if the connection is somehow not possible (e.g. target not responding)
# show a error message instead of excepting and stopping
try:
received = client.read_input_registers(address=startPos,
count=sma_moddatatype[type],
unit=config.slave)
except:
thisdate = str(datetime.datetime.now()).partition('.')[0]
thiserrormessage = thisdate + ': Connection not possible. Check settings or connection.'
print( thiserrormessage)
return ## prevent further execution of this function
message = BinaryPayloadDecoder.fromRegisters(received.registers, endian=Endian.Big)
## provide the correct result depending on the defined datatype
if type == 'S32':
interpreted = message.decode_32bit_int()
elif type == 'U32':
interpreted = message.decode_32bit_uint()
elif type == 'U64':
interpreted = message.decode_64bit_uint()
elif type == 'STR16':
interpreted = message.decode_string(16)
elif type == 'STR32':
interpreted = message.decode_string(32)
elif type == 'S16':
interpreted = message.decode_16bit_int()
elif type == 'U16':
interpreted = message.decode_16bit_uint()
else: ## if no data type is defined do raw interpretation of the delivered data
interpreted = message.decode_16bit_uint()
## check for "None" data before doing anything else
if ((interpreted == MIN_SIGNED) or (interpreted == MAX_UNSIGNED)):
displaydata = None
else:
## put the data with correct formatting into the data table
if format == 'FIX3':
displaydata = float(interpreted) / 1000
elif format == 'FIX2':
displaydata = float(interpreted) / 100
elif format == 'FIX1':
displaydata = float(interpreted) / 10
else:
displaydata = interpreted
#print '************** %s = %s' % (name, str(displaydata))
inverter[name] = displaydata
# Add timestamp
inverter["00000 - Timestamp"] = str(datetime.datetime.now()).partition('.')[0]
def publish_influx(metrics):
target=flux_client.write_points([metrics])
print ("[INFO] Sent to InfluxDB")
def publish_dweepy(inverter):
try:
result = dweepy.dweet_for(config.dweepy_uuid,inverter)
print("[INFO] Sent to dweet.io")
except:
result = None
def publish_mqtt(inverter):
try:
result = mqtt_client.publish(config.mqtt_topic, json.dumps(inverter).replace('"', '\"'))
print("[INFO] Published to MQTT")
except:
result = None
def publish_pvoutput(inverter):
try:
# PVOutput headers
headers = {
'X-Pvoutput-Apikey': "{0}".format(config.pvo_api),
'X-Pvoutput-SystemId': "{0}".format(config.pvo_sid),
'Content-Type': "application/x-www-form-urlencoded",
'cache-control': "no-cache"
}
# see https://pvoutput.org/help.html#api
# Post the following values
# v2 - Power Generation
# v4 - Power Consumption
# v6 - Voltage (we post Grid voltage)
v2 = inverter['total_pv_power']
v4 = inverter['power_meter']
v6 = inverter['grid_voltage']
now = datetime.datetime.now()
# build the querystring
querystring = {
"d":"{0}".format(now.strftime("%Y%m%d")),
"t":"{0}".format(now.strftime("%H:%M")),
"v2":"{0}".format(v2),
"v4":"{0}".format(v4),
"v6":"{0}".format(v6)
}
# POST data
response = requests.request("POST", url=config.pvo_url, headers=headers, params=querystring)
if response.status_code != requests.codes.ok:
raise StandardError(response.text)
else:
print("Successfully posted to {0}".format(config.pvo_url))
except Exception as err:
#print ("[ERROR] pvoutput, %s" % err)
result = None
while True:
try:
client.connect()
inverter = {}
if 'sungrow-' in config.model:
for i in bus['read']:
load_registers("read",i['start'],i['range'])
for i in bus['holding']:
load_registers("holding",i['start'],i['range'])
# Sungrow inverter specifics:
# Work out if the grid power is being imported or exported
if config.model == "sungrow-sh5k" and \
inverter['grid_import_or_export'] == 65535:
export_power = (65535 - inverter['export_power']) * -1
inverter['export_power'] = export_power
inverter['timestamp'] = "%s/%s/%s %s:%02d:%02d" % (
inverter['day'],
inverter['month'],
inverter['year'],
inverter['hour'],
inverter['minute'],
inverter['second'])
if 'sma-' in config.model:
load_sma_register(modmap.sma_registers)
print (inverter)
if config.pvo_enable:
t = Thread(target=publish_pvoutput, args=(inverter,))
t.start()
if mqtt_client is not None:
t = Thread(target=publish_mqtt, args=(inverter,))
t.start()
if config.dweepy_enable:
t = Thread(target=publish_dweepy, args=(inverter,))
t.start()
if flux_client is not None:
metrics = {}
tags = {}
fields = {}
metrics['measurement'] = "Sungrow"
tags['location'] = "Gabba"
metrics['tags'] = tags
metrics['fields'] = inverter
t = Thread(target=publish_influx, args=(metrics,))
t.start()
client.close()
except Exception as err:
#Enable for debugging, otherwise it can be noisy and display false positives:
#print ("[ERROR] %s" % err)
client.close()
client.connect()
time.sleep(config.scan_interval)