Smappee
Posted: Tuesday 03 April 2018 10:28
is it possible to introduce smappee in domoticz natively?
Code: Select all
<?php
$smappee=json_decode(file_get_contents('http://192.168.2.19/gateway/apipublic/reportInstantaneousValues'),true);
if(!empty($smappee['report'])){
$zon=apcu_fetch('zon');
preg_match_all("/ activePower=(\\d*.\\d*)/",$smappee['report'],$matches);
if(!empty($matches[1][1])){
$newzon=round($matches[1][1],0);
if($newzon<0)$newzon=0;
if($zon!=$newzon)apcu_store('zon',$newzon);
if(!empty($matches[1][2])){
$consumption=round($matches[1][2],0);
if($consumption!=apcu_fetch('verbruik'))apcu_store('verbruik',$consumption);
$timestamp=strftime("%Y-%m-%d %H:%M:%S",time());
$query="INSERT INTO `smappee` (`timestamp`,`consumption`) VALUES ('$timestamp','$consumption');";
$db=new mysqli('127.0.0.1','domotica','domotica','domotica');if($db->connect_errno>0)die('Unable to connect to database [' . $db->connect_error . ']');if(!$result=$db->query($query))die('There was an error running the query ['.$query .' - ' . $db->error . ']');$db->close();
if($consumption>8000){
if(past('notify_power')>3600){
apcu_store('Tnotify_power',time());
telegram('Power usage: '.$consumption.' W!',false);
}
}
}
}
}else{
if(shell_exec('curl -H "Content-Type: application/json" -X POST -d "" http://192.168.2.19/gateway/apipublic/logon')!='{"success":"Logon successful!","header":"Logon to the monitor portal successful..."}')exit;
}
function past($name){return time()-apcu_fetch('T'.$name);}
function telegram($msg,$silent=true,$to=1){
$msg=str_replace('__',PHP_EOL,$msg);
shell_exec('/var/www/html/secure/telegram.sh "'.$msg.'" "'.$silent.'" "'.$to.'" > /dev/null 2>/dev/null &');
}
?>
Code: Select all
$timefrom=time-86400;
$chauth = curl_init('https://app1pub.smappee.net/dev/v1/oauth2/token?grant_type=password&client_id='.$smappeeclient_id.'&client_secret='.$smappeeclient_secret.'&username='.$smappeeusername.'&password='.$smappeepassword.'');
curl_setopt($chauth,CURLOPT_AUTOREFERER,true);
curl_setopt($chauth,CURLOPT_RETURNTRANSFER,1);
curl_setopt($chauth,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($chauth,CURLOPT_VERBOSE,1);
curl_setopt($chauth,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($chauth,CURLOPT_SSL_VERIFYPEER,false);
$objauth=json_decode(curl_exec($chauth));
if(!empty($objauth)){
$access=$objauth->{'access_token'};
curl_close($chauth);
$chconsumption=curl_init('');
curl_setopt($chconsumption,CURLOPT_HEADER,0);
$headers=array('Authorization: Bearer '.$access);
curl_setopt($chconsumption,CURLOPT_HTTPHEADER,$headers);
curl_setopt($chconsumption,CURLOPT_AUTOREFERER,true);
curl_setopt($chconsumption,CURLOPT_URL,'https://app1pub.smappee.net/dev/v1/servicelocation/'.$smappeeserviceLocationId.'/consumption?aggregation=3&from='.$timefrom.'000&to='.time.'000');
curl_setopt($chconsumption,CURLOPT_RETURNTRANSFER,1);
curl_setopt($chconsumption,CURLOPT_FOLLOWLOCATION,1);
curl_setopt($chconsumption,CURLOPT_VERBOSE,1);
curl_setopt($chconsumption,CURLOPT_SSL_VERIFYHOST,false);
curl_setopt($chconsumption,CURLOPT_SSL_VERIFYPEER,false);
$objconsumption=json_decode(curl_exec($chconsumption),true);
if(!empty($objconsumption['consumptions'])){
$verbruikvandaag=$objconsumption['consumptions'][0]['consumption']/1000;
apcu_store('verbruikvandaag',round($verbruikvandaag,1));
$zonvandaag=$objconsumption['consumptions'][0]['solar']/1000;
apcu_store('zonvandaag',round($zonvandaag,1));
$gas=apcu_fetch('gasvandaag')/100;
$water=apcu_fetch('watervandaag')/1000;
@file_get_contents("https://xxx.xxx.xxx/secure/insertdata.php?user=xxx&verbruik=$verbruikvandaag&gas=$gas&water=$water&zon=$zonvandaag");
lg("verbruik => gas = $gas | verbruik = $verbruikvandaag | zon = $zonvandaag | water = $water");
}
curl_close($chconsumption);
}
Thanks, I meant to use:
Code: Select all
[url]http://www.domoticz.com/wiki[/url]