OPEN 3 TXT FILE on different server

Post Reply
Mazzokun
Posts: 88
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Milan, Italy
Contact:

OPEN 3 TXT FILE on different server

Post by Mazzokun » Wednesday 26 July 2017 12:16

Hi all! I have 3 Solar inverter and I have a script to read and push to domoticz the data.
This is my script and work

Code: Select all

   #!/usr/bin/php
    
    <?php
       ##### LOGIN SUNWAY INVERTER  #####

    $url = 'http://192.168.0.51/data/inverter.txt';
    
    $login = 'customer';
    $password = '00000000';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL,$url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($ch, CURLOPT_USERPWD, "$login:$password");
    $result = curl_exec($ch);
    curl_close($ch);

      #### ELEBORATING DATA #####

    $results = explode('#', $result);

    $actualPower = filter_var($results[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    $dailyPower = filter_var($results[7],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
    $totalPower = filter_var($results[10],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);

      #### WATT TO KWATT #####

    $actualKPower= $actualPower * 1000;
    $dailyKPower=$dailyPower * 1000;
    $totalKPower=$totalPower * 1000;

     ##### STOP AT NIGHT #####

    if (empty($totalPower)) die ;
    
    ##### PUSH DATA TO DOMOTICZ #####

    file_get_contents( "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=79&nvalue=0&svalue={$actualKPower};{$totalKPower}" );
    ### ALL ABOVE WORKS 
Now I Have to open the second url and redo the exact procedure ( username and password are the same ) and redo the procedure for the third. But I don't know how. At the end I have to sum the three value and push them to domoticz

Code: Select all

    $SumActual = $actualKPower1 + $actualKPower2 + $actualKPower3;
    $SumKTotal = $totalKPower1 + $totalKPower2 + $totalKPower3;
    
    
    ##### PUSH DATA TO DOMOTICZ #####
   
    file_get_contents( "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=82&nvalue=0&svalue={$SumActual};{$SumKTotal}” );
    
But I miss how I can open the second and third url into the same php file. Any help? :)

freijn
Posts: 387
Joined: Friday 23 December 2016 17:40
Target OS: Raspberry Pi
Domoticz version:
Location: Netherlands Purmerend
Contact:

Re: OPEN 3 TXT FILE on different server

Post by freijn » Wednesday 26 July 2017 17:15

Copy the lines between

<?php

and

?>


Then only change ( for the second copy ) The target ip adres in this line.
$url = 'http://192.168.0.51/data/inverter.txt';


Then if you want them reported separately
change the target idx= into the correct one
file_get_contents( "http://127.0.0.1:8080/json.htm?type=com ... otalKPower}" );

of first add all up and send it to the 79 idx. Depending on your requirements

Mazzokun
Posts: 88
Joined: Thursday 28 April 2016 23:55
Target OS: Raspberry Pi
Domoticz version: Beta
Location: Milan, Italy
Contact:

Re: OPEN 3 TXT FILE on different server

Post by Mazzokun » Wednesday 26 July 2017 17:53

Done! works! Thank you :)

Code: Select all

#!/usr/bin/php
<?php

 ##### LOGIN SUNWAY INVERTER 1 #####

$login1 = 'customer';
$password1 = '00000000';
$url1 = 'http://192.168.0.51/data/inverter.txt';
$ch1 = curl_init();
curl_setopt($ch1, CURLOPT_URL,$url1);
curl_setopt($ch1, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch1, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch1, CURLOPT_USERPWD, "$login1:$password1");
$result1 = curl_exec($ch1);
curl_close($ch1);

      #### RIELABORA DATI RICEVUTI#####

$results1 = explode('#', $result1);

$actualPower1 = filter_var($results1[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$dailyPower1 = filter_var($results1[7],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$totalPower1 = filter_var($results1[10],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);

      #### WATT TO KWATT #####

$actualKPower1 = $actualPower1 * 1000;
$dailyKPower1 = $dailyPower1 * 1000;
$totalKPower1 = $totalPower1 * 1000;


       ##### LOGIN SUNWAY INVERTER 2 #####

$login2 = 'customer';
$password2 = '00000000';
$url2 = 'http://192.168.0.52/data/inverter.txt';
$ch2 = curl_init();
curl_setopt($ch2, CURLOPT_URL,$url2);
curl_setopt($ch2, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch2, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch2, CURLOPT_USERPWD, "$login2:$password2");
$result2 = curl_exec($ch2);
curl_close($ch2);

      #### RIELABORA DATI RICEVUTI#####

$results2 = explode('#', $result2);

$actualPower2 = filter_var($results2[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$dailyPower2 = filter_var($results2[7],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$totalPower2 = filter_var($results2[10],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);

      #### WATT TO KWATT #####

$actualKPower2= $actualPower2 * 1000;
$dailyKPower2=$dailyPower2 * 1000;
$totalKPower2=$totalPower2 * 1000;


##### LOGIN SUNWAY INVERTER 3 #####

$login3 = 'customer';
$password3 = '00000000';
$url3 = 'http://192.168.0.53/data/inverter.txt';
$ch3 = curl_init();
curl_setopt($ch3, CURLOPT_URL,$url3);
curl_setopt($ch3, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch3, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch3, CURLOPT_USERPWD, "$login3:$password3");
$result3 = curl_exec($ch3);
curl_close($ch3);

      #### RIELABORA DATI RICEVUTI#####

$results3 = explode('#', $result3);

$actualPower3 = filter_var($results3[0],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$dailyPower3 = filter_var($results3[7],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);
$totalPower3 = filter_var($results3[10],FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION);

      #### WATT TO KWATT #####

$actualKPower3= $actualPower3 * 1000;
$dailyKPower3=$dailyPower3 * 1000;
$totalKPower3=$totalPower3 * 1000;


$SommaActual = $actualKPower1 + $actualKPower2 + $actualKPower3;
$SommaKTotal = $totalKPower1 + $totalKPower2 + $totalKPower3;

file_get_contents( "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=83&nvalue=0&svalue={$actualKPower1};{$totalKPower1}" );
file_get_contents( "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=84&nvalue=0&svalue={$actualKPower2};{$totalKPower2}" );
file_get_contents( "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=85&nvalue=0&svalue={$actualKPower3};{$totalKPower3}" );
file_get_contents( "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=86&nvalue=0&svalue={$SommaActual};{$SommaKTotal}" );
file_get_contents( "http://127.0.0.1:8080/json.htm?type=command&param=udevice&idx=22&nvalue=0&svalue={$SommaActual};{$SommaKTotal}" );
?>

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests