Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- error_reporting(E_ERROR | E_WARNING | E_PARSE);
- /************************************************************************************************/
- /* This program can be freely copied and modified * /
- /* The program can be used under its own responsibility without any liability * /
- /* The authors Franz Spreitz and Lukas Pawek are pleased about a hint in the program code: * /
- /* Code by Franz Spreitz and Lukas Pawek https://energieautark.wordpress.com * /
- /* Translated and updated by Stian B. Barmen https://www.sbarmen.no * /
- /************************************************************************************************/
- /*****************************************************************************************/
- /*********** Function clean is used to clean the variables of special characters *********/
- /*****************************************************************************************/
- function sauber($uebergabe) {
- $uebergabe=str_replace("\n", "", $uebergabe);
- $uebergabe=str_replace("\r", "", $uebergabe);
- $uebergabe=str_replace(" ", "", $uebergabe);
- return $uebergabe;
- }
- /*****************************************************************************************/
- /*********** Function readdata is used to extract the variables from the record ***/
- /*****************************************************************************************/
- function readdata($befehl) {
- $str="";
- $einzel="";
- $varname="";
- $str = shell_exec ($befehl);
- //echo $str;
- $arr = explode ("\n", $str);
- for($x=0;$x<count($arr);$x++) {
- $einzel=explode("\t", $arr[$x]);
- $varname=sauber($einzel[0]);
- $arrneuret[$varname]=sauber($einzel[1]);
- }
- return $arrneuret;
- }
- /******************************************************************************************
- By means of the grep command becomes an entire record of a device
- (eg from the charge controller) from the USB port. This command only has to be done later
- be added to the unique ID of the USB cable. The unique IDs can with
- be determined by this command:
- ls / dev / serial / by-id / usb-VictronEnergy_BV_VE_Direct_cable_VE *
- Write down the name of the device without the directories, eg.
- usb VictronEnergy_BV_VE_Direct_cable_VE12345X-if00-port0
- ******************************************************************************************/
- $grepbefehl="timeout 5 grep -a -m 1 PID -A 50 ";
- // $csvstring=date("d.m.Y H:i").";"; // Variable, die einen neuen Eintrag (also einen neue Zeile) in der .CSV Datei - mittels Semikolon (;) getrennt - schreibt. Der erste Eintrag ist immer das aktuelle Datum samt Uhrzeit
- /******************************************************************************************
- Important variables returned by the Victron devices:
- CS ... Charge State
- H20 .. accumulated daily production of the charge controller
- P .... Power on the battery monitor. Caution: Direct "used up" performance
- must be added by the charge controller
- PPV .. module performance
- SOC .. State of charge - Battery state of charge in percent with one decimal place
- V .... battery voltage (supplied by the battery monitor BMV)
- VPV .. module voltage in millivolts
- V = Main (battery) voltage (mV)
- VS = Auxiliary (starter) voltage (mV)
- I = Battery current (mA)
- T = Battery Temperature
- P = Instantaneous power (W)
- CE = Consumed Amp Hours (mAh6)
- TTG = Time To Go (Minutes6)
- H2 = Depth of deepest discharge
- H2 = Depth of last discharge
- H4 = Number of charge cycles
- H5 = Number of full discharges
- See more information here: https://www.victronenergy.no/download-document/2036/ve.direct-protocol-3.24.pdf
- ******************************************************************************************/
- /* Get USB device name and move to variable $usbname */
- $usbdevices = glob("/dev/serial/by-id/usb-VictronEnergy_BV_VE_Direct_cable_*");
- $usbname = array_values($usbdevices)[0]; // Get devicename from Array and move to simple variable
- // echo $usbname; // Test to see if the device is detected successfully
- /***************************************************************************/
- /* Comment the following lines if you do not want BMV-Data extraction ******/
- /***************************************************************************/
- $befehl=$grepbefehl.$usbname;
- //When booting, the baud rate in the crontab must be set to 19200 and the echo switched off - for all Victron USB ports:
- //@reboot stty -F /dev/serial/by-id/usb-VictronEnergy_BV_VE_Direct_cable_ENTER-SERIENUMBER-HERE-if00-port0 speed 19200 raw -echo
- $arrneu=readdata($befehl);
- // Change the decimal point to make it more readable
- $arrneu['V']=$arrneu['V']/1000; // Change to Volt from millivolt
- $arrneu['VS']=$arrneu['VS']/1000; // Change to Volt from millivolt
- $arrneu['I']=$arrneu['I']/1000; // Change to Amp from milliamp
- $arrneu['SOC']=$arrneu['SOC']/10; // Change to whole %
- //Encode the array into a JSON string.
- $encodedString = json_encode($arrneu);
- //Save the JSON string to a text file.
- $dir="/home/homeassistant/.homeassistant/"; // Enter the directory for storing the CSV file, /var/www/html/ enables local webserver to serve the file publicly
- $filename="victron_json.txt"; // Change this to the preferred filename
- file_put_contents($dir.$filename, $encodedString);
- /****************************************************************/
- /* Uncomment the following lines for MPPT-Data 100/30 module */
- /****************************************************************/
- // $usbnamemppt10030=$usbname;
- // $befehlmppt=$grepbefehl.$usbnamemppt10030;
- // $arrneumppt=readdata($befehlmppt);
- // $VPV=$arrneumppt['VPV']/1000; // Voltage = VPV - in millivolts so we deivide by 1.000
- // $csvstring.=$VPV.";".$arrneumppt['PPV'].";".$arrneumppt['CS'].";".$arrneumppt['H20'].";";
- /****************************************************************/
- /* Uncomment the following lines for the MPPT-Data 75/15 module */
- /****************************************************************/
- // $usbnamemppt7515=$usbname;
- // $befehlmppt7515=$grepbefehl.$usbnamemppt7515;
- // $arrneumppt7515=readdata($befehlmppt7515);
- // $VPV7515=$arrneumppt7515['VPV']/1000; // Voltage = VPV - in millivolts so we deivide by 1.000
- // $csvstring.=$VPV7515.";".$arrneumppt7515['PPV'].";".$arrneumppt7515['CS'].";".$arrneumppt7515['H20'].";";
- /*******************************************************/
- /* Save data to local CSV file */
- /*******************************************************/
- /*
- if(is_numeric($volt)) { // Query whether the voltage is a numerical value. If not, it is erroneous data and then no data should be written ...
- //echo $csvstring;
- $csvstring.="\n";
- $dir="/home/homeassistant/.homeassistant/"; // Enter the directory for storing the CSV file, /var/www/html/ enables local webserver to serve the file publicly
- $filename="victron_output.csv"; // Change this to the preferred filename
- $out = fopen($dir.$filename, "a+");
- fwrite($out, $csvstring);
- fclose($out);
- }
- */
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement