Thursday, September 14, 2017

Simple Fronius logger

If you can install python (which you can pretty much anywhere) here's a little script to get started with logging from Fronius inverter.

The latest source code is here.
import requests
import json
import datetime
import time

# Set this to the IP address of your inverter
host = "192.168.0.112"
# number of seconds between samples, set to zero to run once and exit
sample_seconds = 60 * 5


def main():
    print("started")
    while True:
        try:
            watts = watts_generated()
            now = time.strftime("%H:%M:%S")
            line = "%s\t%s\n" % (now, watts)
            # print(line)
            write_to_logfile(line)
        except requests.exceptions.ConnectTimeout:
            print("Connect timeout at %s" % time.strftime("%H:%M:%S"))
        if sample_seconds > 0:
            time.sleep(sample_seconds)
        else:
            return


def write_to_logfile(line):
    today = time.strftime("%Y_%m_%d")
    file_name = today + ".csv"
    out_file = open(file_name, "a")
    out_file.write(line)
    out_file.close()


def watts_generated():
    url = "http://" + host + "/solar_api/v1/GetInverterRealtimeData.cgi?Scope=System"
    r = requests.get(url, timeout=2)
    json_data = r.json()
    result = json_data["Body"]["Data"]["PAC"]["Values"]["1"]
    return result


if __name__ == "__main__":
    main()



Note that I'm assuming Python 3 and you'll probably need to install the requests module. The program outputs a single file for each day.

Here's some code to draw a pretty graph.
"""
Simple code to draw a graph of a day of power.
Requires matplotlib

On Fedora Linux: sudo dnf install python3-matplotlib

"""

import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import csv
import time
import datetime

today = time.strftime("%Y_%m_%d")

INFILENAME = today + '.csv'


def main():
    data_reader = csv.reader(open(INFILENAME,'r'), delimiter='\t')
    y = []
    x = []
    for row in data_reader:
        if len(row) > 1:
            timeval = row[0]

            dt = datetime.datetime.strptime(timeval, "%H:%M:%S")
            print("timeval = %s -> %s" % (timeval, dt))
            x.append(mdates.date2num(dt))
            watts = float(row[1])
            y.append(watts)

    fig, ax = plt.subplots()
    ax.plot(x,y)
    ax.xaxis_date()
    my_fmt = mdates.DateFormatter('%H:%M')
    ax.xaxis.set_major_formatter(my_fmt)
    plt.ylabel("Watts")
    plt.xlabel("Time")
    plt.show()
    plt.savefig('%s_graph' % today)


if __name__ == "__main__":
    main()




Finding the IP address of your inverter is pretty easy if you have a network scanner such as nmap. I use LanScan on macOS and the Fronius comes up as having a network interface from "u-blox AG".

No comments: