Python Test Script with PV Module

Prerequisites:
1.  Python. It is installed by default on all Raspbian distributions
2. Python module pyserial. It is installed already on Jessie and Jessie Lite Raspbian distributions. Otherwise, install with
sudo pip install pyserial
or, if pip isn’t installed or doesn’t work,
sudo apt-get install pyserial
3. PV module, originally  from https://pv.codeplex.com/
Download the package from the “Downloads” tab, unzip it into your home folder (preferably into a subdirectory of your home folder). When unzipped, there will be a subdirectory “pv“. Unfortunately the package does not contain the setup.py file, so we have to get it from the tab “Source Code“. As it cannot be downloaded directly into “setup.py”, I generated a new document in a text editor, called it setup.py and pasted the content into that document.
Alternatively, get the complete package from GIT: https://github.com/blebo/pv

The package contains setup.py and additionally a test script, but the actual python module is identical to the original.

#!/usr/bin/env python

import pv
import serial
import time
import sys
import os

pv.debug()


#Read Raspberry Pi Temperature
raspi_temp = os.popen('vcgencmd measure_temp').readline()
print "Raspberry CPU Temperatur: " + raspi_temp[5:]


#Open Serial Port
port = serial.Serial('/dev/ttyAMA0',9600, timeout=0.2)

# timeout?
if not port.isOpen():
    print "cannot connect to Sunezy."
    sys.exit(1)

#Create Inverter object
from pv import cms

ezy= cms.Inverter(port)

#Reset Inverter
ezy.reset()

#Discover (get serial number)
sn = ezy.discover()
if sn is None:
    print "Sunezy not connected"
    sys.exit(1)
if pv._DEBUG:
    print "Seriennummer: ", sn

# Register Inverter
ok = ezy.register(sn)
if not ok:
    print "Sunezy registration failed"
    sys.exit(1)

# Get Version
version = ezy.version()
print "Sunezy Version: ",version

# Get Layout of parameters message
param_layout=ezy.param_layout()
if pv._DEBUG:
    print "Parameter Layout: ", param_layout

# Get parameters
parameters = ezy.parameters(param_layout)
if not parameters:
    print "Kann Parameter nicht interpretieren"
else:
    for field in parameters:
        print "%-10s: %s" % field

# Get Layout of Status message
status_layout = ezy.status_layout()
if pv._DEBUG:
    print "Status Layout: ", status_layout

# Get Status
status = ezy.status(status_layout)

for field in status:
    print "%-10s: %s" % field

dstat = dict (status)
print "Power to Grid: %s W" %dstat['Pac']

os.environ['TZ'] = 'Europe/Paris'
time.tzset()

t = time.strftime('%W %a %d.%m.%Y %H:%M:%S', time.localtime(time.time()))
Pac = str(dstat['Pac'])
Vac = str(dstat['Vac'])
Fac = str(dstat['Fac'])
Vpv = str(dstat['Vpv'])
hTotal = str(dstat['h-Total'])
ETotal = str(dstat['E-Total'])
Mode = str(dstat['Mode'])
Err = str(dstat['Error'])
TempInv = str(dstat['Temp-inv'])

#Statuszeile komponieren
hdr = "%2s %3s %-10s %-8s %4s %6s %6s %6s %7s %7s %4s %4s %8s %8s" % ("WW","Tag","Datum","Zeit","Pac","Vac","Fac","Vpv","h-Total","E-Total","Mode","Err","Temp-Inv","Temp-RPi")
print hdr
sts = "%s %4s %6s %6s %6s %7s %7s %4s %4s %8s %8s\n" % (t,Pac,Vac,Fac,Vpv,hTotal,ETotal,Mode,Err,TempInv,raspi_temp[5:-3])
print sts
Advertisements