It has been a while since I have posted anything. So I thought I would go through the setup of my Solar monitoring.

When I got my solar system I noticed that the Inverter had a RS232 connector on it. While it was being installed I was talking with the installers about it and they mentioned that you could monitor the system with it so I set out to learn how to. I had to end up getting in touch with the manufacturer about it. They were very helpful which was nice.

I have just recently moved this from a full Linux install to a Raspberry Pi so hang on while we go through all that is required to set it up.

The script uses Perl to query the serial adaptor (USB in this case) then writes the values to a CSV file, sends it to pvoutput.org and writes it to a RRD file.

First up was to install all of the Perl requirements, I was having some trouble with CPAN so I ended up installing CPanMinus

curl -L https://cpanmin.us | perl - --sudo App::cpanminus

Once that was installed I needed to install a few dependencies for the script to run correctly.

#AppConfig (To allow it to read settings from a file
sudo cpanm AppConfig

#SerialPort to allow the script to interact with the serial port
sudo cpanm Device::SerialPort

#HTTPCommon to allow the script to send the data to pvoutput.org
sudo cpanm HTTP::Request::Common

#UserAgent to allow the script to send the data to pvoutput.org
sudo cpanm LWP::UserAgent

Now that all of the scripts dependencies have been installed the next step was to find out where the USB serial adaptor was

pi@raspberrypi:~/solar $ dmesg | grep tty
[    0.000000] Kernel command line: dma.dmachans=0x7f35 bcm2708_fb.fbwidth=640 bcm2708_fb.fbheight=480 bcm2708.boardrev=0xe bcm2708.serial=0xf4fd3327 smsc95xx.macaddr=B8:27:EB:FD:33:27 bcm2708_fb.fbswap=1 bcm2708.uart_clock=3000000 vc_mem.mem_base=0x1ec00000 vc_mem.mem_size=0x20000000  dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p7 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait
[    0.001318] console [tty1] enabled
[    0.135197] 20201000.uart: ttyAMA0 at MMIO 0x20201000 (irq = 83, base_baud = 0) is a PL011 rev2
[    0.503269] console [ttyAMA0] enabled
[    3.880036] systemd[1]: Expecting device dev-ttyAMA0.device...
[    4.258519] systemd[1]: Starting system-serial\x2dgetty.slice.
[    4.271345] systemd[1]: Created slice system-serial\x2dgetty.slice.
[    8.585714] usb 1-1.2: pl2303 converter now attached to ttyUSB0

Great, it is attached to /dev/ttyUSB0

Now the script. I cant take credit for this script. I found it years ago on a forum that was all about solar systems. (Solarfreaks) However after some searching I have found that the script has been added to GitHub and can be found here – https://github.com/trollkarlen/inverter_monitor

The readme.txt file has a great run through of how to setup the scripts – https://github.com/trollkarlen/inverter_monitor/blob/master/README.txt

Once the script is setup we need to test it.

pi@raspberrypi:~/solar $ perl inverter.pl "/dev/ttyUSB0"
Starting up at 16/07/2016 07:30:54 running on linux ...
Initialise Serial Port... port = /dev/ttyUSB0
Send -> req init inverter: 435a00007f00000300011f
Send -> req serial: 435a00007f00000000011c
Recv <- aaaaff007f0000800a4132464c393239303337059f
Send -> confirm serial: 435a00007f0100010b4132464c39323930333701036d
Recv <- aaaaff017f0000810106035b
Send -> req version: 435a0000010101030000a3
Recv <- aaaaff0101000183303130303230303041342e313520204d6f64656c3a20322e30204b57202020496e766572746572203230303020333530300ebb
* Version info:
asciiVers=▒▒▒▒01002000A4.15  Model: 2.0 KW   Inverter 2000 3500▒
CAPACITY : 002000
FIRMWARE : A4.15
MANUF    : Inverter 2000
MODEL    : Model: 2.0 KW
OTHER    : 3500
SERIAL   : A2FL929037
Send -> req data as at 16/07/2016 07:33:08 : 435a0000010101020000a2
Recv <- aaaaff01010001822800630a60000009cc1385000000000001894800004df93100000000000000000000000000000000000783
* Data:
TEMP    :      9.9 deg C = Internal Temperature
VPV     :    265.6 V     = Panel Voltage
IAC     :        0 A     = Grid Current
VAC     :    250.8 V     = Grid Voltage
FAC     :    49.97 Hz    = Grid Frequency
PAC     :        0 W     = Output Power
ETODAY  :        0 kWh   = Accumulated Energy Today
ETOTALH :      256 kWh   = Accumulated Energy (high bit)
ETOTALL :   3514.4 kWh   = Accumulated Energy (low bit)
HTOTALH :        0 hrs   = Working Hours (high bit)
HTOTALL :    19961 hrs   = Working Hours (low bit)
MODE    :    12544       = Operating Mode?
ERR_GV  :        0       = Error message: GV fault value?
ERR_GF  :        0       = Error message: GF fault value?
ERR_GZ  :        0       = Error message: GZ fault value?
ERR_TEMP:        0       = Error message: Tmp fault value?
ERR_PV1 :        0       = Error message: PV1 fault value?
ERR_GFC1:        0       = Error message: GFC1 fault value?
ERR_MODE:        0       = Error mode?
Logging to: /home/pi/solar/Outputs/inverter_A2FL929037_20160716.csv
PVOUTPUT as at 16/07/2016 07:36:11 ...
  ran: perl pvoutput.pl 0 0 20160716 07:36 A2FL929037 0 9.9
Sleeping: 58 secs as at 16/07/2016 07:33:10 ...

Excellent, that is all working. The next (and last) thing to do is to get it to run automatically. You could just run a cron job which calls the perl script, however the issue is that what was found was that it would just start heaps and heaps of the same script which would eventually bring the system to a halt.

So to get around that I found the following script which creates a solar.lock file when it is running. The script checks for this file and if it exists, exits.

#!/bin/bash

# Some variable
#
BASEDIR="/home/pi/solar"
DATA=$(date +%Y%m%d)

# crude locking...
#
if [ -f "$BASEDIR/solar.lock" ]; then
spid=$(cat "$BASEDIR/solar.lock")
if [ -d "/proc/$spid" ]; then
exit 0
else
rm -f "$BASEDIR/solar.lock"
fi
fi
echo $$ > "$BASEDIR/solar.lock"


# call the script... we need to cd to the dir to correctly read the .ini files.
#
cd $BASEDIR
perl $BASEDIR/inverter.pl "/dev/ttyUSB0" >> $BASEDIR/Outputs/inverter-$DATA.log 2>&1

# Unlock
#
rm -f "$BASEDIR/solar.lock"
exit 0

Give that file +x rights and then setup a cron job to call it between certain hours of the day (in my case 4am and 8pm)

*/5 4-20 * * * /home/pi/solar/solar >/dev/null 2>&1

That’s it.

If you would like to see my outputs on pvoutput.org check out my system at http://pvoutput.org/list.jsp?userid=2401