Fronius API Bash Script Logging to MariaDB
From KlavoWiki
I have created a BASH script to save inverter data and history. I am running a Raspberry Pi and a local MariaDB server.
BASH Script
#/bin/bash rows=20 #Lines to Display count=1 #Predefine rows db_Name=solar #Database Name db_User=username #Database Username db_Password=password #Database Password db_Table=5kw #Table Name db_Server=localhost #Server Address, IP or FQDN timeloop=$(date '+%H%M') display_output () { if [ $count -eq 1 ]; then echo -e 'Date\t\tWatts\tVolts\tAMPs\tToday\tGross Total\tYear Total' fi echo -e $strDateTime '\t\t' $PAC '\t' $VAC '\t' $IAC '\t' $ETODAY '\t' $ETOTAL '\t' $YTOTAL if [ $count -eq $rows ]; then count=0 echo echo fi ((count++)) } write_sql () { mysql -u $db-User -p$db_Password -D "$db_Name" -e "insert into $db_Table (date, etotal, etoday, ytotal, pac, vac, iac, fac) values ('$strDateTime', '$ETOTAL', '$ETODAY', '$YTOTAL', '$PAC', '$VAC', '$IAC', '$FAC');" } while [ $timeloop -lt 1930 ]; do strDateTime=$(date '+%Y-%m-%d %H:%M:%S') strVal=$(curl -sG http://192.168.1.101/solar_api/v1/GetInverterRealtimeData.cgi?'Scope=Device&DataCollection=CommonInverterData&DeviceId=1') result=$? if [ $result -ne 0 ]; then echo echo Unable to access URL of inverter. echo Exiting. echo exit fi ETODAY=$(jq '.Body.Data.DAY_ENERGY.Value' <<< $strVal) ETOTAL=$(jq '.Body.Data.TOTAL_ENERGY.Value' <<< $strVal) YTOTAL=$(jq '.Body.Data.YEAR_ENERGY.Value' <<< $strVal) PAC=$(jq '.Body.Data.PAC.Value' <<< $strVal) VAC=$(jq '.Body.Data.UAC.Value' <<< $strVal) IAC=$(jq '.Body.Data.IAC.Value' <<< $strVal) FAC=$(jq '.Body.Data.FAC.Value' <<< $strVal) if [ $PAC -lt 7 ] && [ $timeloop -gt 1600 ]; then echo Time to exit exit 0 fi write_sql #display_output sleep 19 done
SQL Database
mysql
create database solar
CREATE TABLE `5kw` ( `DATE` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', `ETODAY` int(5) NOT NULL DEFAULT 0.00, `PAC` int(4) NOT NULL DEFAULT 0, `IAC` float(4,2) DEFAULT NULL, `VAC` float(4,1) DEFAULT NULL, `FAC` float(4,2) DEFAULT NULL, `YTOTAL` int(9) DEFAULT NULL, `ETOTAL` float(10) DEFAULT NULL, PRIMARY KEY (`DATE`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1;