vrm_portal:vrm_juice_php_example
This is an old revision of the document!
VRM Juice PHP Example
For more info about VRM Juice, see vrm_juice_json_api
Many thanks to Steven from http://lackancottage.co.uk/blog/, who wrote this code for his blog and providing it to us.
<?php $url = 'https://juice.victronenergy.com/'; $username="YOUR VRM USERNAME"; $password="YOUR VRM PASSWORD"; $version="200.i"; $verification_token="1"; $instance="0"; $fields = array( 'username' => urlencode($username), 'password' => urlencode($password), 'version' => urlencode($version), 'verification_token' => urlencode($verification_token), ); foreach($fields as $key=>$value) $fields_string .= $key.'='.$value.'&'; rtrim($fields_string, '&'); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url."user/login"); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); $result = curl_exec($ch); $result=json_decode($result); $sessionid=$result->data->user->sessionid; $fields = array( 'sessionid' => urlencode($sessionid), 'version' => urlencode($version), 'verification_token' => urlencode($verification_token), ); foreach($fields as $key=>$value) $fields_string .= $key.'='.$value.'&'; rtrim($fields_string, '&'); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url."sites/get"); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); $result = json_decode(curl_exec($ch)); if($result->status->code==200){ $i=0; $str=""; $ajax_values=array(); foreach($result->data->sites as $site){ $i++; $fields = array( 'sessionid' => urlencode($sessionid), 'version' => urlencode($version), 'verification_token' => urlencode($verification_token), 'siteid' => urlencode($site->idSite), #ADD THE ATTRIBUTES YOU NEED TO RETURN HERE 'attributes'=> '[113, 131, 144, 143 ]', 'instance' => urlencode($instance), ); foreach($fields as $key=>$value) $fields_string .= $key.'='.$value.'&'; rtrim($fields_string, '&'); $ch = curl_init(); curl_setopt($ch,CURLOPT_URL, $url."sites/attributes"); curl_setopt($ch,CURLOPT_POST, count($fields)); curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1); $attributes = json_decode(curl_exec($ch)); if($attributes->status->code==200){ $attributes=$attributes->data->attributes; } else{ $str.="Error in API Call. Try again<br /><h3>Error Description:</h3><p>".$attributes->status->message."</p>"; } $ajax_values[$i-1]=array( "site_id"=>$site->idSite, "site_name"=>$site->name, "attributes"=>array() ); $str.='<div class="title" id="site_'.$site->siteId.'">'.$site->name.'</div>'; if(isset($attributes) && is_array($attributes)){ foreach($attributes as $attribute){ $str.='<div class="row" id="site_'.$site->idSite.'">'; $value="value".ucfirst($attribute->dataType); switch($attribute->idDataAttribute){ case 131: $str.="<div class='consumption'><div id='customlabel'>Consumption</div><div id='value'>We are using <span id='attribute_".$attribute->idDataAttribute."'>".round($attribute->$value, 2)."</span> W</div></div>"; $ajax_values[$i-1]["attributes"][]=array($attribute->idDataAttribute,round($attribute->$value, 2)); break; case 113: $str.="<div class='yield'><div id='customlabel'>Solar Yield:</div><div id='value'>Generating <span id='attribute_".$attribute->idDataAttribute."'>".round($attribute->$value, 1)."</span> W</div></div>"; $ajax_values[$i-1]["attributes"][]=array($attribute->idDataAttribute,round($attribute->$value, 1)); break; case 143: $str.="<div class='voltage'><div id='customlabel'>Battery:</div><div id='value'>Voltage <span id='attribute_".$attribute->idDataAttribute."'>".$attribute->$value."</span> V</div></div>"; $ajax_values[$i-1]["attributes"][]=array($attribute->idDataAttribute,$attribute->$value); break; case 144: $str.="<div class='soc".($attribute->$value<=25?"1":($attribute->$value>25 && $attribute->$value<=50)?"2":($attribute->$value>50 && $attribute->$value<=75)?"3":"4")."'><div id='customlabel'>Battery:</div><div id='value'>SOC <span id='attribute_".$attribute->idDataAttribute."'>".$attribute->$value."</span>%</div></div>"; $ajax_values[$i-1]["attributes"][]=array($attribute->idDataAttribute,$attribute->$value); break; } $str.='</div>'; } } else{ $str.='<div class="row"> </div>'; } } $ajax_values[1]=$ajax_values[0]; if(!isset($_GET["ajax"])){ ?> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <script> $(window).load(function(){ setInterval(function(){ $(".status_msg").show(); $.get("<?php echo $_SERVER['PHP_SELF']?>", {ajax: 1}, function(data){ data=data.split("##JSON##"); if(data[1]){ data = JSON.parse(data[1]); for($i=0; $i<data.length; $i++){ for($j=0; $j<data[$i].attributes.length; $j++){ $("#site_"+data[$i].site_id+" #attribute_"+data[$i].attributes[$j][0]).html(data[$i].attributes[$j][1]); } } } $(".status_msg").hide(); }); }, 10000); // change this value to whatever interval you need for update }); </script> <style> .status_msg{ display:none;} .list{ border-collapse: collapse; font-size: 11px; margin: 0 auto; width: 1000px; } .list .th, .list .td { border: 1px solid #333; padding: 5px; vertical-align:top; } .list .th { background: none repeat scroll 0 0 #333; border: 1px solid #fff; color: #fff; } .list .tr.odd { background: none repeat scroll 0 0 #ccc; } .list .tr.even { background: none repeat scroll 0 0 #ff1; } .title { font-family: arial, sans-serif; font-size: 15px; font-weight:bold: } .row {display: block; width: 300px;padding:10px;padding-left: 80px; margin:5px; border: 1px solid #666; } #customlabel { font-family: arial, sans-serif; font-size: 13px; font-weight:normal; display:block;} #value { font-family: arial, sans-serif; font-size: 13px; font-weight:normal; display:block;} #consumption {background-image url('images/thumb_kwh_metre.png'); background-repeat:no-repeat; background-position: left, middle; } #yield {background-image url('images/thumb_weather_00.png'); background-repeat:no-repeat; background-position: left, middle; } #voltage {background-image url('images/thumb_lightning.png'); background-repeat:no-repeat; background-position: left, middle; } #soc {background-image url('images/thumb_battery_4.png'); background-repeat:no-repeat; background-position: left, middle; } </style> <div class="list"> <?php echo $str;?> <div class="status_msg">Updating...</div> </div> <p> <?php } else echo "##JSON##".json_encode($ajax_values)."##JSON##"; } else echo "Error in API Call. Try again<br /><h3>Error Description:</h3><p>".$result->status->message."</p>"; curl_close($ch); ?> <br /> <strong>Sample Values for Attribute ID's.</strong><br /> #description=Battery Power (System) #Attribute ID 243 Value(valueFloat): -141.54%F%.0F W <br /> #description=Grid L1 #Attribute ID 134 Value(valueFloat): 0%.0F%.0F W <br /> #description=AC Consumption L1 #Attribute ID 131 Value(valueFloat): 116%.0F%.0F W <br /> #description=Solar charger serial number #Attribute ID 118 Value(valueString): HQ143299WX7%s%s <br /> #description=PV - DC-coupled #Attribute ID 113 Value(valueFloat): 0%.0F%.0F W <br /> #description=Battery watts #Attribute ID 107 Value(valueFloat): 0%.0F%.0F W <br /> #description=Solarcharger model #Attribute ID 106 Value(valueString): 108%s%s <br /> #description=Battery to consumers #Attribute ID 105 Value(valueFloat): 0.0599995%.2F%.2F kWh<br /> #description=Error code #Attribute ID 98 Value(valueEnum): 0%s%s <br /> #description=Maximum charge power yesterday #Attribute ID 97 Value(valueFloat): 1118%d%d <br /> #description=Yield yesterday #Attribute ID 96 Value(valueFloat): 1.83%.2F%.2F <br /> #description=Maximum charge power today #Attribute ID 95 Value(valueFloat): 157%d%d <br /> #description=Yield today #Attribute ID 94 Value(valueFloat): 0.4%.2F%.2F <br /> #description=High batt. voltage alarm #Attribute ID 93 Value(valueEnum): 0%s%s <br /> #description=Low batt. voltage alarm #Attribute ID 92 Value(valueEnum): 0%s%s <br /> #description=Battery Voltage (System) #Attribute ID 143 Value(valueFloat): 23.59%.2F%.2F V <br /> #description=Battery State of Charge (System) #Attribute ID 144 Value(valueFloat): 94%.1F%.1F %% <br /> #description=Battery Consumed Amphours (System) #Attribute ID 145 Value(valueFloat): -20.7%.2F%.2F Ah <br /> #description=VE.Bus hidden SOC #Attribute ID 240 Value(valueFloat): 84.5%.1F%.1F %% <br /> #description=Network traffic rx #Attribute ID 239 Value(valueFloat): 89506%d%d B <br /> #description=Network traffic tx #Attribute ID 238 Value(valueFloat): 8997%d%d B <br /> #description=Boot type #Attribute ID 237 Value(valueEnum): 1%s%s <br /> #description=log time offset #Attribute ID 216 Value(valueFloat): 119%s%s s <br /> #description=Battery state #Attribute ID 215 Value(valueEnum): 2%s%s <br /> #description=Remote support #Attribute ID 154 Value(valueEnum): 0%s%s <br /> #description=VE.Bus charge current (System) #Attribute ID 153 Value(valueFloat): -5.7%.1F%.1F A <br /> #description=Update to #Attribute ID 151 Value(valueEnum): 0%d%d <br /> #description=Auto update #Attribute ID 150 Value(valueEnum): 1%d%d <br /> #description=Local ip address #Attribute ID 149 Value(valueString): 192.168.2.2%s%s #<br /> #description=Remote ip address #Attribute ID 148 Value(valueString): 88.145.32.15%s%s <br /> #description=Battery Current (System) #Attribute ID 147 Value(valueFloat): -6%.2F%.2F A <br /> #description=Battery Time to Go (System) #Attribute ID 146 Value(valueFloat): 23.5%.2F%.2F h <br /> #description=Relay on the charger #Attribute ID 90 Value(valueEnum): 0%s%s <br /> #description=PV current #Attribute ID 87 Value(valueFloat): 0%.1F%.1F <br /> #description=PV voltage #Attribute ID 86 Value(valueFloat): 10.98%.2F%.2F <br /> #description=Active input current limit #Attribute ID 27 Value(valueFloat): 3.1%.2F%.2F A <br /> #description=Output frequency #Attribute ID 26 Value(valueFloat): 49.8982%.2F%.2F Hz <br /> #description=Output current phase 1 #Attribute ID 23 Value(valueFloat): 0.64%.1F%.1F A <br /> #description=Output voltage phase 1 #Attribute ID 20 Value(valueFloat): 245.02%.1F%.1F VAC <br /> #description=Input power 1 #Attribute ID 17 Value(valueFloat): 0%.0F%.0F VA <br /> #description=Input frequency 1 #Attribute ID 14 Value(valueFloat): 50.1026%.2F%.2F Hz <br /> #description=Input current phase 1 #Attribute ID 11 Value(valueFloat): 0%.1F%.1F A <br /> #description=Input voltage phase 1 #Attribute ID 8 Value(valueFloat): 247.06%.1F%.1F VAC <br /> #description=VE.Bus device version #Attribute ID 7 Value(valueString): 2610304%s%s <br /> #description=mk2 version #Attribute ID 6 Value(valueString): 1130133%s%s <br /> #description=Longitude #Attribute ID 5 Value(valueFloat): -6.08909%.5F%.5F LNG <br /> #description=Latitude #Attribute ID 4 Value(valueFloat): 54.2621%.5F%.5F LAT <br /> #description=gatewayBuildDate #Attribute ID 3 Value(valueString): 201412221640%s%s <br /> #description=gatewayVersion #Attribute ID 2 Value(valueString): v1.17%s%s <br /> #description=Output power 1 #Attribute ID 29 Value(valueFloat): 116%.0F%.0F VA <br /> #description=Battery voltage #Attribute ID 32 Value(valueFloat): 23.6%.2F%.2F V <br /> #description=Battery current #Attribute ID 33 Value(valueFloat): -5.7%.1F%.1F A <br /> #description=Charge state #Attribute ID 85 Value(valueEnum): 0%s%s <br /> #description=Charger on/off #Attribute ID 84 Value(valueEnum): 4%s%s <br /> #description=Battery temperature #Attribute ID 83 Value(valueFloat): 4.75%.1F%.1F C <br /> #description=Battery current #Attribute ID 82 Value(valueFloat): 0%.1F%.1F A <br /> #description=Battery voltage #Attribute ID 81 Value(valueFloat): 23.61%.2F%.2F V <br /> #description=Solar charger version #Attribute ID 80 Value(valueString): v2.02%s%s <br /> #description=Switch Position #Attribute ID 79 Value(valueEnum): 3%s%s <br /> #description=Overload alarm #Attribute ID 44 Value(valueEnum): 0%s%s <br /> #description=Low battery alarm #Attribute ID 43 Value(valueEnum): 0%s%s <br /> #description=Temperature alarm #Attribute ID 42 Value(valueEnum): 0%s%s <br /> #description=VE.Bus Error #Attribute ID 41 Value(valueEnum): 0%s%s <br /> #description=VE.Bus state #Attribute ID 40 Value(valueEnum): 9%s%s <br /> #description=Active input #Attribute ID 35 Value(valueFloat): 0%d%d <br /> #description=Phase count #Attribute ID 34 Value(valueFloat): 1%d%d <br /> #description=gatewayID #Attribute ID 1 Value(valueEnum): 1%s%s </p>
DISQUS
~~DISQUS~~
vrm_portal/vrm_juice_php_example.1422267700.txt.gz · Last modified: 2015-01-26 11:21 by mvader