Skip to content

Instantly share code, notes, and snippets.

@karlp
Last active December 22, 2015 16:58
Show Gist options
  • Save karlp/6502796 to your computer and use it in GitHub Desktop.
Save karlp/6502796 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# script to poll growatt PV inverter and spit out values
# Andrew Elwell <[email protected]> 2013-09-01
import ConfigParser
from pymodbus.client.sync import ModbusSerialClient as ModbusClient
import time
import mosquitto
config = "/home/andrew/solarmon.cfg"
settings = ConfigParser.RawConfigParser()
settings.read(config)
port = settings.get('inverter', 'port')
result = {}
result['timestamp'] = time.time()
client = ModbusClient(method='rtu', port=port, baudrate=9600, stopbits=1, parity='N', bytesize=8, timeout=1)
client.connect()
broker = settings.get('mosquitto', 'broker')
mqtt = mosquitto.Mosquitto("PV_Monitor")
mqtt.connect(broker)
mqtt.loop_start()
# can't read the whole lot in one pass, so grab each chunk
# addr / descriptions lifted from http://code.google.com/p/pvbeancounter/source/browse/trunk_v2/PVSettings/Device_Growatt.xml
rr = client.read_input_registers(2,3)
mqtt.publish("PV_W", rr.registers[0])
mqtt.publish("PV_V", rr.registers[1])
mqtt.publish("PV_A", rr.registers[2])
rr = client.read_input_registers(12,3)
mqtt.publish("Out_W", rr.registers[0])
mqtt.publish("AC_Hz", rr.registers[1])
mqtt.publish("AC_V", rr.registers[2])
rr = client.read_input_registers(27, 1)
mqtt.publish("kWh_today", rr.registers[0])
rr = []
rr = client.read_input_registers(29,1)
mqtt.publish("kWh_total", rr.registers[0])
client.close()
mqtt.loop_stop()
mqtt.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment