Advertisement
Guest User

Untitled

a guest
Jun 9th, 2018
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.31 KB | None | 0 0
  1. <?php
  2. error_reporting(E_ERROR | E_WARNING | E_PARSE);
  3.  
  4. /************************************************************************************************/
  5. /* This program can be freely copied and modified                                              * /
  6. /*   The program can be used under its own responsibility without any liability                * /
  7. /*   The authors Franz Spreitz and Lukas Pawek are pleased about a hint in the program code:   * /
  8. /*   Code by Franz Spreitz and Lukas Pawek https://energieautark.wordpress.com                 * /
  9. /*   Translated and updated by Stian B. Barmen https://www.sbarmen.no                          * /
  10. /************************************************************************************************/
  11.  
  12.  
  13.  
  14. /*****************************************************************************************/
  15. /*********** Function clean is used to clean the variables of special characters *********/
  16. /*****************************************************************************************/
  17. function sauber($uebergabe) {
  18.         $uebergabe=str_replace("\n", "", $uebergabe);
  19.         $uebergabe=str_replace("\r", "", $uebergabe);
  20.         $uebergabe=str_replace("   ", "", $uebergabe);
  21.         return $uebergabe;
  22. }
  23.  
  24. /*****************************************************************************************/
  25. /*********** Function readdata is used to extract the variables from the record        ***/
  26. /*****************************************************************************************/
  27. function readdata($befehl) {
  28.         $str="";
  29.         $einzel="";
  30.         $varname="";
  31.  
  32.         $str = shell_exec ($befehl);
  33.         //echo $str;
  34.         $arr = explode ("\n", $str);
  35.         for($x=0;$x<count($arr);$x++) {
  36.                 $einzel=explode("\t", $arr[$x]);
  37.                 $varname=sauber($einzel[0]);
  38.                 $arrneuret[$varname]=sauber($einzel[1]);
  39.         }
  40.         return $arrneuret;
  41. }
  42.  
  43. /******************************************************************************************
  44.  
  45.  
  46. By means of the grep command becomes an entire record of a device
  47.  (eg from the charge controller) from the USB port. This command only has to be done later
  48.  be added to the unique ID of the USB cable. The unique IDs can with
  49.  be determined by this command:
  50.  ls / dev / serial / by-id / usb-VictronEnergy_BV_VE_Direct_cable_VE *
  51.  Write down the name of the device without the directories, eg.
  52.  usb VictronEnergy_BV_VE_Direct_cable_VE12345X-if00-port0
  53.  
  54. ******************************************************************************************/
  55. $grepbefehl="timeout 5 grep -a -m 1 PID -A 50 ";
  56.  
  57.  
  58. // $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
  59.  
  60. /******************************************************************************************
  61. Important variables returned by the Victron devices:
  62.   CS ... Charge State
  63.   H20 .. accumulated daily production of the charge controller
  64.   P .... Power on the battery monitor. Caution: Direct "used up" performance
  65.                   must be added by the charge controller
  66.   PPV .. module performance
  67.   SOC .. State of charge - Battery state of charge in percent with one decimal place
  68.   V .... battery voltage (supplied by the battery monitor BMV)
  69.   VPV .. module voltage in millivolts
  70.  
  71.   V = Main (battery) voltage (mV)
  72.   VS = Auxiliary (starter) voltage (mV)
  73.   I = Battery current (mA)
  74.   T = Battery Temperature
  75.   P = Instantaneous power (W)
  76.   CE = Consumed Amp Hours (mAh6)
  77.   TTG = Time To Go (Minutes6)
  78.   H2 = Depth of deepest discharge
  79.   H2 = Depth of last discharge
  80.   H4 = Number of charge cycles
  81.   H5 = Number of full discharges
  82.  
  83. See more information here: https://www.victronenergy.no/download-document/2036/ve.direct-protocol-3.24.pdf
  84.  
  85. ******************************************************************************************/
  86.  
  87. /* Get USB device name and move to variable $usbname */
  88.   $usbdevices = glob("/dev/serial/by-id/usb-VictronEnergy_BV_VE_Direct_cable_*");
  89.   $usbname = array_values($usbdevices)[0]; // Get devicename from Array and move to simple variable
  90. // echo $usbname;  // Test to see if the device is detected successfully
  91.  
  92. /***************************************************************************/
  93. /* Comment the following lines if you do not want BMV-Data extraction ******/
  94. /***************************************************************************/
  95.  
  96. $befehl=$grepbefehl.$usbname;
  97. //When booting, the baud rate in the crontab must be set to 19200 and the echo switched off - for all Victron USB ports:
  98. //@reboot stty -F /dev/serial/by-id/usb-VictronEnergy_BV_VE_Direct_cable_ENTER-SERIENUMBER-HERE-if00-port0 speed 19200 raw -echo
  99. $arrneu=readdata($befehl);
  100.  
  101. // Change the decimal point to make it more readable
  102. $arrneu['V']=$arrneu['V']/1000; // Change to Volt from millivolt
  103. $arrneu['VS']=$arrneu['VS']/1000; // Change to Volt from millivolt
  104. $arrneu['I']=$arrneu['I']/1000; // Change to Amp from milliamp
  105. $arrneu['SOC']=$arrneu['SOC']/10; // Change to whole %
  106.  
  107.  
  108. //Encode the array into a JSON string.
  109. $encodedString = json_encode($arrneu);
  110. //Save the JSON string to a text file.
  111. $dir="/home/homeassistant/.homeassistant/"; // Enter the directory for storing the CSV file, /var/www/html/ enables local webserver to serve the file publicly
  112. $filename="victron_json.txt"; // Change this to the preferred filename
  113. file_put_contents($dir.$filename, $encodedString);
  114.  
  115.  
  116.  
  117. /****************************************************************/
  118. /*  Uncomment the following lines for MPPT-Data 100/30 module   */
  119. /****************************************************************/
  120.  
  121. //  $usbnamemppt10030=$usbname;
  122. //  $befehlmppt=$grepbefehl.$usbnamemppt10030;
  123. //  $arrneumppt=readdata($befehlmppt);
  124. //  $VPV=$arrneumppt['VPV']/1000; // Voltage = VPV - in millivolts so we deivide by 1.000
  125. //  $csvstring.=$VPV.";".$arrneumppt['PPV'].";".$arrneumppt['CS'].";".$arrneumppt['H20'].";";
  126.  
  127. /****************************************************************/
  128. /* Uncomment the following lines for the MPPT-Data 75/15 module */
  129. /****************************************************************/
  130.  
  131. // $usbnamemppt7515=$usbname;
  132. // $befehlmppt7515=$grepbefehl.$usbnamemppt7515;
  133. // $arrneumppt7515=readdata($befehlmppt7515);
  134. // $VPV7515=$arrneumppt7515['VPV']/1000; // Voltage = VPV - in millivolts so we deivide by 1.000
  135. // $csvstring.=$VPV7515.";".$arrneumppt7515['PPV'].";".$arrneumppt7515['CS'].";".$arrneumppt7515['H20'].";";
  136.  
  137. /*******************************************************/
  138. /* Save data to local CSV file                         */
  139. /*******************************************************/
  140.  
  141. /*
  142. 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 ...
  143.         //echo $csvstring;
  144.         $csvstring.="\n";
  145.         $dir="/home/homeassistant/.homeassistant/"; // Enter the directory for storing the CSV file, /var/www/html/ enables local webserver to serve the file publicly
  146.         $filename="victron_output.csv"; // Change this to the preferred filename
  147.         $out = fopen($dir.$filename, "a+");
  148.         fwrite($out, $csvstring);
  149.         fclose($out);
  150. }
  151.  
  152. */
  153. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement