Using Domoticz with Eastron SDM 220

Moderator: leecollings

User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Using Domoticz with Eastron SDM 220

Post by Egregius »

Something like this I think:

Code: Select all

#!/usr/bin/php
<?php
$data = array();
for($x=1;$x<=2;$x++){
    $x = exec("/usr/local/bin/sdm120c -v -c -p -g -f -e -i -t -q -w3 -z5 /dev/ttyUSB0");
    $data = preg_split('/[[:space:]]+/', $x);
    if($data[8] = 'OK'){
        $tensione = $data[0];
        $corrente = $data[1];
        $potenza = $data[2];
        $cosfi = $data[3];
        //$frequenza = $data[4];
        $prelevata = $data[5];
        $immessa = $data[6];
        $energia = $data[7];
        udevice(1,0,$tensione);
        udevice(2,0,$corrente);
        udevice(3,0,"$potenza;$energia");
        udevice(4,0,$cosfi);
        udevice(72,0,$prelevata);
        udevice(69,0,$immessa);
    }
    $data = json_decode(file_get_contents('http://192.168.1.103/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
    if(isset($data['Body']['Data']['PAC']['Value'])){
        $froniuspac=$data['Body']['Data']['PAC']['Value'];
        $froniusday=$data['Body']['Data']['DAY_ENERGY']['Value'];
        $froniustotal=$data['Body']['Data']['TOTAL_ENERGY']['Value'];
        udevice(58,0,$froniuspac);
        udevice(59,0,$froniusday);
        udevice(60,0,$froniustotal);
    }
    //Calculate the total
    $totalenergy = $froniuspac + $prelevata - $immessa;
    udevice(123,0,$totalenergy);
    sleep(28);
}

function udevice($idx,$nvalue,$svalue){
    $reply=json_decode(file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue),true);
    if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
    logwrite('    '.$idx.'    N='.$nvalue.'    S='.$svalue.'    = '.$reply);
    usleep(250000);
    return $reply;
}
function logwrite($msg) {
    $time    = microtime(true);
    $dFormat = "Y-m-d H:i:s";
    $mSecs   =  $time - floor($time);
    $mSecs   =  substr(number_format($mSecs,3),1);
    $fp = fopen('/var/log/fronius.log',"a+");
    fwrite($fp, sprintf("%s%s %s\n", date($dFormat), $mSecs, $msg));
    fclose($fp); 
}
 
The sleep is down to 28 seconds because there are 9 time a 1/4 sec sleep in the script. I also changed the 'goto inizio' into a loop that only runs twice. You'll get more accurate timings with a cronjob run every minute than a endless loop. I also think it's more stable to run the script every minute. With a endless loop you don't have recovery options when something goes wrong.
videodrome
Posts: 64
Joined: Wednesday 11 May 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Using Domoticz with Eastron SDM 220

Post by videodrome »

Egregius, the new script seems to have some problem

Code: Select all

HP Parse error:  syntax error, unexpected '$x▒' (T_VARIABLE) in /home/pi/SDM120C/new.php on line 5
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Using Domoticz with Eastron SDM 220

Post by Egregius »

Oh, of course. Using $x as variable in a loop that counts $x...

Code: Select all

#!/usr/bin/php
<?php
$data = array();
for($x=1;$x<=2;$x++){
    $y = exec("/usr/local/bin/sdm120c -v -c -p -g -f -e -i -t -q -w3 -z5 /dev/ttyUSB0");
    $data = preg_split('/[[:space:]]+/', $y);
    if($data[8] = 'OK'){
        $tensione = $data[0];
        $corrente = $data[1];
        $potenza = $data[2];
        $cosfi = $data[3];
        //$frequenza = $data[4];
        $prelevata = $data[5];
        $immessa = $data[6];
        $energia = $data[7];
        udevice(1,0,$tensione);
        udevice(2,0,$corrente);
        udevice(3,0,"$potenza;$energia");
        udevice(4,0,$cosfi);
        udevice(72,0,$prelevata);
        udevice(69,0,$immessa);
    }
    $data = json_decode(file_get_contents('http://192.168.1.103/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
    if(isset($data['Body']['Data']['PAC']['Value'])){
        $froniuspac=$data['Body']['Data']['PAC']['Value'];
        $froniusday=$data['Body']['Data']['DAY_ENERGY']['Value'];
        $froniustotal=$data['Body']['Data']['TOTAL_ENERGY']['Value'];
        udevice(58,0,$froniuspac);
        udevice(59,0,$froniusday);
        udevice(60,0,$froniustotal);
    }
    //Calculate the total
    $totalenergy = $froniuspac + $prelevata - $immessa;
    udevice(123,0,$totalenergy);
    sleep(28);
}

function udevice($idx,$nvalue,$svalue){
    $reply=json_decode(file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx='.$idx.'&nvalue='.$nvalue.'&svalue='.$svalue),true);
    if($reply['status']=='OK') $reply='OK';else $reply='ERROR';
    logwrite('    '.$idx.'    N='.$nvalue.'    S='.$svalue.'    = '.$reply);
    usleep(250000);
    return $reply;
}
function logwrite($msg) {
    $time    = microtime(true);
    $dFormat = "Y-m-d H:i:s";
    $mSecs   =  $time - floor($time);
    $mSecs   =  substr(number_format($mSecs,3),1);
    $fp = fopen('/var/log/fronius.log',"a+");
    fwrite($fp, sprintf("%s%s %s\n", date($dFormat), $mSecs, $msg));
    fclose($fp); 
}
  
videodrome
Posts: 64
Joined: Wednesday 11 May 2016 16:11
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Using Domoticz with Eastron SDM 220

Post by videodrome »

i changed the script but the problem rest the same... (the T variable), maybe a wrong bracket in a part of the script?
User avatar
Egregius
Posts: 2592
Joined: Thursday 09 April 2015 12:19
Target OS: Linux
Domoticz version: v2024.7
Location: Beitem, BE
Contact:

Re: Using Domoticz with Eastron SDM 220

Post by Egregius »

Remove all the spaces in front and after each equal sign. In the code you posted at 10:44 there's a strange character.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest