Pass2PHP

User avatar
sincze
Posts: 962
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Saturday 18 August 2018 9:39

@Egregius: I created a new php class (my first one indeed) to read Omink Inverters from friends via the Omnik Web Portal. (so no direct access to their Inverters). It log in to the portal, grabs a token, retrieves the data from the portal and returns the data.

Code: Select all

function OmnikInverterUpdate()
{
	include(rootdir.'pass2php_include/omnikwebinverter_class.php');
    
       $inverter = new \OmnikWebInverter\OmnikWebInverter(omnik_user_1,omnik_pwd_1,omnik_id_1);
	$result=$inverter->omnikdata();
	lg('Omnik: 1 Result: '.round($result[0]).' and: '.$result[1]); 

	$inverter = new \OmnikWebInverter\OmnikWebInverter(omnik_user_2,omnik_pwd_2,omnik_id_2);
	$result=$inverter->omnikdata();
	lg('Omnik: 2 Result: '.round($result[0]).' and: '.$result[1]);

	$inverter = new \OmnikWebInverter\OmnikWebInverter(omnik_user_3,omnik_pwd_3,omnik_id_3);
	$result=$inverter->omnikdata();
	lg('Omnik: 3 Result: '.round($result[0]).' and: '.$result[1]);
}
It just returns current W and Currenlt Daily KwH.

As this process takes around 6-8 seconds to complete, wouldn't it be best not to call this function from _Cronn300.php but move the action to a separate file like:

Code: Select all

shell_exec(domoticz_location.'scripts/pass2php/OmnikWebInverter.sh "'.$details.'" > /dev/null 2>/dev/null &');      // 
Like with telegram :lol: and grabbing screenshots from an ip camera ?

tnx for your thought.
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2715
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Saturday 18 August 2018 13:05

I would indeed put it in cron120 or cron300 at the end of the file so it doesn't slow down the rest. Or put it in a separate script and call that at the end of cron.

User avatar
sincze
Posts: 962
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Saturday 18 August 2018 14:43

Egregius wrote:
Saturday 18 August 2018 13:05
I would indeed put it in cron120 or cron300 at the end of the file so it doesn't slow down the rest. Or put it in a separate script and call that at the end of cron.
Tnx for the tip at the end of Cron300.php
- Cron line calls function OmnikInverterUpdate() within pass2php library to update the omnik devices.

- The function calls 1 script 3 times (1 for each inverter)

Code: Select all

function OmnikInverterUpdate()
{
    shell_exec(domoticz_location.'scripts/pass2php/omnikwebinverter.sh "Omnik_1" > /dev/null 2>/dev/null &');
    shell_exec(domoticz_location.'scripts/pass2php/omnikwebinverter.sh "Omnik_2" > /dev/null 2>/dev/null &');
    shell_exec(domoticz_location.'scripts/pass2php/omnikwebinverter.sh "Omnik_3" > /dev/null 2>/dev/null &');
}
omnikwebinverter.sh calls a omnikwebinverter.php via the local webserver.

As the device name (Omnik_1) is passed as a parameter the omnikwebinverter.php can update the device using that name and you 'ud' function.

Maybe there is a better solution, but it is working ;-) :D

I could ofcourse move all inverter data to omnikwebinverter.php and have just 1 call
like so.

Code: Select all

function OmnikInverterUpdate()
{
    shell_exec(domoticz_location.'scripts/pass2php/omnikwebinverter.sh "all" > /dev/null 2>/dev/null &');
}

Code: Select all

function retrieve_omnik_data($command)
{
//    $device=$command;
 
    $to_inverters = array(     
                        array( 'device' => "Omnik 1",  'username' => 1_user, 'password' => 1_pwd, 'id' => 1_id ),
                        array( 'device' => "Omnik 2",  'username' => 2_user, 'password' => 2_pwd, 'id' => 2_id ),
                        array( 'device' => "Omnik 3",  'username' => 3_user, 'password' => 3_pwd, 'id' => 3_id )
                        );

    foreach ($to_inverters as $item) 
    {   
        $inverter = new \OmnikWebInverter\OmnikWebInverter($item['username'],$item['password'],$item['id']);
        $result=$inverter->omnikdata();
        $device=$item['device'];

        $str= ( round($result[0]).';'.($result[1]*1000) );
        lg('Omnik: '.$device.' result: '.round($result[0]).' and: '.$result[1].' for domoticz: '.$str);
        if ( isset($result[0]) && isset($result[1]) ) ud($device,0,$str,'Omnik: Inverter: Generation updated');
    }
}
Also working a well multiple roads to Rome...
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2715
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Saturday 18 August 2018 16:05

Why bother searching for other solutions if it works without issues?
Leave it like that ;)
I only make changes for new stuff, like yesterday for a Bose Soundtouch 30 😁

User avatar
sincze
Posts: 962
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Saturday 18 August 2018 23:47

Egregius wrote:
Saturday 18 August 2018 16:05
I only make changes for new stuff, like yesterday for a Bose Soundtouch 30 😁
Am I missing a piece on your github :lol: :lol:
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2715
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Sunday 19 August 2018 6:50

You also have a soundtouch?
I'm updating Github now ;)

There are some new functions for it:

Code: Select all

function bosekey($key,$sleep=100000){
	$xml = "<key state=\"press\" sender=\"Gabbo\">$key</key>";
	bosepost("key", $xml);
	usleep ($sleep);
	$xml = "<key state=\"release\" sender=\"Gabbo\">$key</key>";
	bosepost("key", $xml);
}
function bosevolume($vol){
	$vol=1*$vol;
	$xml = "<volume>$vol</volume>";
	bosepost("volume", $xml);
}
function bosepreset($pre){
	$pre = 1 * $pre;
	if($pre<1||$pre>6)return;
	bosekey("PRESET_$pre");
}
function bosepost($method,$xml){
	echo "$xml\n";
	$ch = curl_init("http://192.168.2.194:8090/$method");
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
	curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
	$response = curl_exec($ch);
	curl_close($ch);
	echo $response;
}
And there are also some changes in pass2php files to automate some stuff for it. Like start playing a random preset when entering the living room and choose a volume depending on how early I'm awake :D
And a new floorplan to control it. It has basic functions like power on/off, volume, choose preset and view what's playing.

User avatar
sincze
Posts: 962
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Sunday 19 August 2018 16:45

Egregius wrote:
Sunday 19 August 2018 6:50
You also have a soundtouch?
I'm updating Github now ;)

There are some new functions for it:
.....

And there are also some changes in pass2php files to automate some stuff for it. Like start playing a random preset when entering the living room and choose a volume depending on how early I'm awake :D
And a new floorplan to control it. It has basic functions like power on/off, volume, choose preset and view what's playing.
Excellent.
I did not dare to touch your floorplans functionality yet. Not that much knowledge.

I do have everything in pass2php 'regular' style and that works great.
Sometimes I figure out a nice addition to add to it. :D
Currently out of ideas.. as pretty much everything I own that has a wire or antenna is taking part of it. :lol:

I have to admit you once told me about PHP possibilities with so many examples around.
viewtopic.php?f=64&t=12343&start=40#p107590
It's true. :P
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2715
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Sunday 19 August 2018 22:19

You can run the floorplan completely besides the domoticz ui. Once you get the hang of it you’ll never use the domoticz ui again. Except for adding devices...
Sometimes I need to use the domoticz ui when I just added a switch, but what a pain it is to find a switch when you have +100 of them :shock:

User avatar
sincze
Posts: 962
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Monday 20 August 2018 21:35

Egregius wrote:
Sunday 19 August 2018 22:19
You can run the floorplan completely besides the domoticz ui. Once you get the hang of it you’ll never use the domoticz ui again. Except for adding devices...
Sometimes I need to use the domoticz ui when I just added a switch, but what a pain it is to find a switch when you have +100 of them :shock:
Ok, let me have a closer look. :D don't need to think about cookies and login :D I hope..

Something different...
As my stupid bash file did not want create a nice google notification warning anymore... for a reason I don't know..Spending my whole sunday figuring out what went wrong.
I decided to go the Pass2PHP way..
Job finished within 35 minutes.... :lol: and.. response is a lot quicker.. as I now reuse my created samples.

Code: Select all


<?PHP
//error_reporting(E_ALL);ini_set("display_errors", "on");
define('rootdir','/var/www/html/secure/');
require(rootdir.'pass2php/settings.php');


if(isset($_REQUEST['text'])){ googlenotification($_REQUEST['text']); }             // The Command to parse is ; separated

function googlenotification($command) 
{
    $location="/tmp/google/";
    $lang = "nl-NL";

    $commandline = explode(";",$command);
    if(isset($commandline[0])) $devicename=$commandline[0];        // Google device
    if(isset($commandline[1])) $text=$commandline[1];                   // Text

    if (isset($devicename))     
    {
        switch ($devicename)          // Determine the IP Address of GOOGLE Machine.
        {
            case 'googlehome':
                $devicename='192.168.1.114';
            break;
            case 'googleaudio':
                $devicename='192.168.1.131';
            break;                
            default:
                $devicename='192.168.1.114';
        }
    }

    if ( time>=strtotime('08:00') && time<strtotime('21:00') ) $volume='0.4';
    else $volume='0.2';

    $text=str_replace('_', ' ', $text); 
    // MP3 filename generated using MD5 hash
    // Added things to prevent bug if you want same sentence in two different languages
    $file = md5($lang."?".urlencode($text));
    $file = $location . $file . ".mp3";
    
    // Check folder exists, if not create it, else verify CHMOD
    if (!is_dir($location)) mkdir($location);
    else
        if (substr(sprintf('%o', fileperms($location)), -4) != "0777") chmod($location, 0777);

    if (!file_exists($file))
    {
        $mp3 = file_get_contents('http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&client=tw-ob&&tl='.$lang.'&q='.urlencode($text));        
        file_put_contents($file, $mp3);
    }

    if (file_exists($file))
    {
        $command=('python /home/pi/domoticz/scripts/system/stream2chromecast/stream2chromecast.py -devicename '.$devicename.' -setvol '.$volume);
        $output = passthru($command);

        $command=('python /home/pi/domoticz/scripts/system/stream2chromecast/stream2chromecast.py -devicename '.$devicename.' '.$file);
        $output = passthru($command);
    }

}
Only need to add something to check if the google device is online.. Could be as easy as

Code: Select all

status($devicename);
:lol: :lol: :lol:
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2715
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Tuesday 21 August 2018 14:45

Is the device pingable?
If so, there must be some code somewhere, perhaps the code I use to check if my nas is online.

Code: Select all

$nas=apcu_fetch('nas');
if(pingport('192.168.2.210',1598)==1){
	$prevcheck=apcu_fetch('check_diskstation_1598');
	if($prevcheck>0)apcu_store('check_diskstation_1598',0);
	if($nas!='On'){apcu_store('nas','On');apcu_store('Tnas',time());}
}else{
	$check=apcu_fetch('check_diskstation_1598')+1;
	if($check>0)apcu_store('check_diskstation_1598',$check);
	if($check>=3&&$nas!='Off'){apcu_store('nas','Off');apcu_store('Tnas',time());}
	$nas='Off';
}

User avatar
sincze
Posts: 962
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Sunday 26 August 2018 20:04

@Egregius, cleaned up your code? :D no more usage for setstatus and status on github ?
Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

User avatar
Egregius
Posts: 2715
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: Beta
Location: Beitem, BE
Contact:

Re: Pass2PHP

Post by Egregius » Sunday 26 August 2018 20:46

Could be, but then it's already a long time like that I think.

User avatar
sincze
Posts: 962
Joined: Monday 02 June 2014 22:46
Target OS: Raspberry Pi
Domoticz version: 4.9700
Location: Netherlands
Contact:

Re: Pass2PHP

Post by sincze » Sunday 16 September 2018 13:49

I found this nice https://github.com/Art-of-WiFi/UniFi-API-client class.
With the class you can retrieve info from the UniFi Controller. I created a script so I can do presence detection with PHP, just to phase out another cron job. :lol:

In addition I also used it to retrieve the public IP address and store that in a user variable. I used to have a separate function for that, however the online website I retrieved the information from was not always available. (not included in the example below)

Do's:
  • In the Unifi controller find your mobile phones and in the 'note' section add 'GSM' in the beginning.
  • The device name in the controller must be the same as the domoticz device name.
As the script takes several seconds to run I moved it to a separate file as suggested by @Egregius some posts ago, like the separate telegram script.
Spoiler: show

Code: Select all

<?PHP
define('rootdir','/var/www/****/*******/');
require(rootdir.'pass2php_include/settings.php');

checkunify();

function checkunify()
{
    require_once(rootdir.'pass2php_include/class/unificlient_class.php');
    
    $controlleruser     = unifi_controlleruser;         // the user name for access to the UniFi Controller
    $controllerpassword = unifi_controllerpassword;     // the password for access to the UniFi Controller
    $controllerurl      = unifi_controllerurl;          // full url to the UniFi Controller, eg. 'https://22.22.11.11:8443'
    $site_id            = unifi_site_id;
    
    /**
    * initialize the UniFi API connection class and log in to the controller and pull the requested data
    */

    $unifi_connection = new UniFi_API\Client($controlleruser, $controllerpassword, $controllerurl, $site_id);
    $loginresults     = $unifi_connection->login();

    $clients_array    = $unifi_connection->list_clients();
    $health_array     = $unifi_connection->list_health();

    $mobile_phones = array( "GSM Jan", "GSM Miep");                            // Check these phones (names)!
    $online_phones = array();

    foreach ($clients_array as $client) {
        if (isset($client->note) && (strpos($client->note, "GSM") === 0)) {         // Note field of USG contains the word GSM
            $online_phones[] = $client->name;                                       // Store found phone names in array
            }                                                                           
    }

    foreach ($mobile_phones as $mobile_phone ) {                                    // Retrieve a phone to check from the array        
        if ( in_array($mobile_phone, $online_phones) ) {
            if (status($mobile_phone) != 'On') sw($mobile_phone,'On'); 
        }
        else { 
            if (status($mobile_phone) != 'Off') sw($mobile_phone,'Off');
        }
        house_mode();
    }
}

Using Pass2php since 2016-12
LAN: RFLink, P1-Port, OTGW, MySensors
USB: RFXCom, ZWave
WIFI: Mi-light Wifi-Bridge, Sonoff, ESP8266, Xiaomi Gateway
Solar: Omnik Inverter, PVOutput
Video: Kodi clients with Harmony HUB
Sensors: You name it I probably got 1.

Post Reply

Who is online

Users browsing this forum: No registered users and 0 guests