Page 1 of 2

Using Domoticz with Eastron SDM 220

Posted: Thursday 11 August 2016 18:20
by videodrome
I need a help to edit a php script
The script below, reads from a meter (eastron sdm 220 c) under rs485 modbus.
I can't import the reading of the imported energy from the grid. (-i) on this php script for eastron. All the readings works well.
I entered in line n. 5 -i for reading, but this seems not to work.

Code: Select all

#!/usr/bin/php
<?php
$data = array();
inizio:
$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_2 = $data[4];
  $immessa = $data[5];
  $prelevata = $data[6];
  $energia = $data[7];
}

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=1&nvalue=0&svalue=$tensione");
      $oem = curl_exec($ch);
curl_close($ch);
 
$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=2&nvalue=0&svalue=$corrente");
      $oem = curl_exec($ch);
curl_close($ch);


$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=3&nvalue=0&svalue=$potenza;$energia");
      $oem = curl_exec($ch);
curl_close($ch);
 
$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=4&nvalue=0&svalue=$cosfi");
      $oem = curl_exec($ch);
curl_close($ch);


$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=43&nvalue=0&svalue=$immessa");
      $oem = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
       curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=44&nvalue=0&svalue=$prelevata");
       $oem = curl_exec($ch);
 curl_close($ch);

//sleep(2);
goto inizio;

?>
@Egregius (thanx for previous solar script): i try using your script, but this seems not to work at all, what's wrong?
Maybe I forgot to enter some parameters?

Code: Select all

#!/usr/bin/php
<?php
$data = array();
inizio:
$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 = udevice(1,0,$data[0]);
  $corrente = udevice(2,0,$data[1]);
  $potenza = $data[2];
  $cosfi = udevice(4,0,$data[3]);
  //$frequenza_2 = $data[4];
  $immessa = udevice(43,0,$data[5]);
  $prelevata = udevice(44,0,$data[6]);
  $energia = udevice(3,0,$data[7]);
}

sleep(2);
goto inizio;

function udevice($idx,$nvalue,$svalue) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=$idx&nvalue=$nvalue&svalue=$svalue");
    curl_exec($ch);
    curl_close($ch);
}

Re: Using Domoticz with Eastron SDM 220

Posted: Friday 12 August 2016 7:16
by Egregius
can you add a 'echo $x;' line after x=...
and a 'print_r($data);' after $data=...

The script for the Fronius is working now?
If you can get the data of the Eastron in variable $x we'll get it in domoticz.

Re: Using Domoticz with Eastron SDM 220

Posted: Friday 12 August 2016 7:28
by Egregius
A tip while programming in PHP:
add many echo's for your variables. If the output of echo is just 'array()' you need to use print_r($variable) to show it. Also adding <hr> to it helps finding what's outputted from where. The html tag <pre> shows the array in a nice way.

So for your peace of code:

Code: Select all

#!/usr/bin/php
<?php
$data = array();
inizio:
$x = exec("/usr/local/bin/sdm120c -v -c -p -g -f -e -i -t -q -w3 -z5 /dev/ttyUSB0");
echo 'x = '.$x.'<hr>';
$data = preg_split('/[[:space:]]+/', $x);
echo 'data = <pre>';print_r($data);echo '</pre><hr>';
if ($data[8] = 'OK') {
    echo 'data 8 ok<hr>'.$data[0].'-'.$data[1].'-'.$data[2].'-'.$data[3].'-'.$data[4].'-'.$data[5].'-'.$data[6].'-'.$data[7];
  $tensione = udevice(1,0,$data[0]);
  $corrente = udevice(2,0,$data[1]);
  $potenza = $data[2];
  $cosfi = udevice(4,0,$data[3]);
  //$frequenza_2 = $data[4];
  $immessa = udevice(43,0,$data[5]);
  $prelevata = udevice(44,0,$data[6]);
  $energia = udevice(3,0,$data[7]);
}

sleep(2);
goto inizio;

function udevice($idx,$nvalue,$svalue) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=$idx&nvalue=$nvalue&svalue=$svalue");
    curl_exec($ch);
    curl_close($ch);
}
There's also no need to mask the ip address as it is an internal IP. Nobody can do anything with it.
A script like this should be run from command line. Because of the sleep and loop a browser would never show the output.

Re: Using Domoticz with Eastron SDM 220

Posted: Friday 12 August 2016 16:29
by videodrome
Egregius wrote: The script for the Fronius is working now?
Yes the solar script works well.
And i I took your advice by increasing the sleep in the script (eastron) to 30 seconds. Looking at the log there were too many redundant and close readings. Now is much better.
When at home, I edit the script of the Eastron and I'll tell you.

Re: Using Domoticz with Eastron SDM 220

Posted: Monday 15 August 2016 0:02
by pj-r
Egregius wrote:A tip while programming in PHP:
add many echo's for your variables. If the output of echo is just 'array()' you need to use print_r($variable) to show it. Also adding <hr> to it helps finding what's outputted from where. The html tag <pre> shows the array in a nice way.
Or use var_dump 8-)

Re: Using Domoticz with Eastron SDM 220

Posted: Thursday 18 August 2016 2:42
by videodrome
Egregius wrote:

Code: Select all

#!/usr/bin/php
<?php
$data = array();
inizio:
$x = exec("/usr/local/bin/sdm120c -v -c -p -g -f -e -i -t -q -w3 -z5 /dev/ttyUSB0");
echo 'x = '.$x.'<hr>';
$data = preg_split('/[[:space:]]+/', $x);
echo 'data = <pre>';print_r($data);echo '</pre><hr>';
if ($data[8] = 'OK') {
    echo 'data 8 ok<hr>'.$data[0].'-'.$data[1].'-'.$data[2].'-'.$data[3].'-'.$data[4].'-'.$data[5].'-'.$data[6].'-'.$data[7];
  $tensione = udevice(1,0,$data[0]);
  $corrente = udevice(2,0,$data[1]);
  $potenza = $data[2];
  $cosfi = udevice(4,0,$data[3]);
  //$frequenza_2 = $data[4];
  $immessa = udevice(43,0,$data[5]);
  $prelevata = udevice(44,0,$data[6]);
  $energia = udevice(3,0,$data[7]);
}

sleep(2);
goto inizio;

function udevice($idx,$nvalue,$svalue) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=$idx&nvalue=$nvalue&svalue=$svalue");
    curl_exec($ch);
    curl_close($ch);
}
there's a syntax error on line 6
"PHP Parse error: syntax error, unexpected ''x▒=▒'' (T_CONSTANT_ENCAPSED_STRING) in /home/pi/SDM120C/ele2.php on line 6"

Re: Using Domoticz with Eastron SDM 220

Posted: Thursday 18 August 2016 5:13
by Egregius
There's no error in line 6.
echo 'x = '.$x.'<hr>';
try it with doubl quotes
echo "x = ".$x."<hr>";

Re: Using Domoticz with Eastron SDM 220

Posted: Thursday 18 August 2016 19:15
by videodrome
Egregius wrote:There's no error in line 6.
echo 'x = '.$x.'<hr>';
try it with doubl quotes
echo "x = ".$x."<hr>";
same problem on line 8 ''data▒=▒<pre>'' (T_CONSTANT_ENCAPSED_STRING)
but not work if i the put the quotation marks "data = <pre>"

Code: Select all

echo 'data = <pre>';print_r($data);echo '</pre><hr>';

Re: Using Domoticz with Eastron SDM 220

Posted: Thursday 18 August 2016 19:32
by Egregius
I don't know what to say. The code is good, tested it.
If I don't know the value of x I can't help you putting it in an array.

Re: Using Domoticz with Eastron SDM 220

Posted: Thursday 18 August 2016 20:46
by videodrome
Egregius wrote:I don't know what to say. The code is good, tested it.
If I don't know the value of x I can't help you putting it in an array.
I'm a totally newbie of php
but if you ask me the output of x (Eastron) is:
230.68 4.86 1055.34 0.94 50.02 1228547 794604 2023151 OK

where the outputs are:
voltage - current (A) - active power - power factor - frequency - exported power (Wh) - imported power (Wh)- total power (Wh)

Re: Using Domoticz with Eastron SDM 220

Posted: Thursday 18 August 2016 21:42
by Egregius
Well, if I use that as x it works fine for me:

Code: Select all

#!/usr/bin/php
<?php
$data = array();
inizio:
$x = "230.68 4.86 1055.34 0.94 50.02 1228547 794604 2023151 OK";
echo '<hr>x = '.$x.'<hr>';
$data = preg_split('/[[:space:]]+/', $x);
echo 'data = <pre>';print_r($data);echo '</pre><hr>';
if ($data[8] = 'OK') {
    echo 'data 8 ok<hr>'.$data[0].'-'.$data[1].'-'.$data[2].'-'.$data[3].'-'.$data[4].'-'.$data[5].'-'.$data[6].'-'.$data[7].'<hr>';
  $tensione = udevice(1,0,$data[0]);
  $corrente = udevice(2,0,$data[1]);
  $potenza = $data[2];
  $cosfi = udevice(4,0,$data[3]);
  //$frequenza_2 = $data[4];
  $immessa = udevice(43,0,$data[5]);
  $prelevata = udevice(44,0,$data[6]);
  $energia = udevice(3,0,$data[7]);
}

//sleep(2);
//goto inizio;

function udevice($idx,$nvalue,$svalue) {
    echo $idx.' '.$nvalue.' '.$svalue.'<br/>';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://192.168.0.10:8080/json.htm?type=command&param=udevice&idx=$idx&nvalue=$nvalue&svalue=$svalue");
    curl_exec($ch);
    curl_close($ch);
}
 
gives this output:
#!/usr/bin/php

x = 230.68 4.86 1055.34 0.94 50.02 1228547 794604 2023151 OK

data =
Array
(
[0] => 230.68
[1] => 4.86
[2] => 1055.34
[3] => 0.94
[4] => 50.02
[5] => 1228547
[6] => 794604
[7] => 2023151
[8] => OK
)

data 8 ok 230.68-4.86-1055.34-0.94-50.02-1228547-794604-2023151
1 0 230.68
2 0 4.86
4 0 0.94
43 0 1228547
44 0 794604
3 0 2023151

What version of PHP are you running? Maybe something with that. I use 5.6.

Re: Using Domoticz with Eastron SDM 220

Posted: Friday 19 August 2016 0:53
by videodrome
I understand .... for some strange reason the output of the counter reverses the 2 readings of exported and imported energy.... from the command line I run "sdm120c -v -c -p -g -f -e -i -t -q -z5 /dev/ttyUSB0" and the replay was 230.68 4.86 1055.34 0.94 50.02 1228547 794604 2023151, but the correct reading should have been 794604 1228547 ( I noticed it reading the real output from the Eastron's display).
I changed my first script as below and now everything works well. :D

one last thing ... is it possible to add in this script another sensor that calculates the autoconsumption (ie total energy generated by the PV, sensor from solar.php) + imported energy (sensor n.72) - exported energy (sensor n.69)? :oops:

Code: Select all

#!/usr/bin/php
<?php

$data = array();

inizio:

$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_2 = $data[4];
  $prelevata = $data[5];
  $immessa = $data[6];
  $energia = $data[7];
}

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.120:8080/json.htm?type=command&param=udevice&idx=1&nvalue=0&svalue=$tensione");
      $oem = curl_exec($ch);
curl_close($ch);
 
$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.120:8080/json.htm?type=command&param=udevice&idx=2&nvalue=0&svalue=$corrente");
      $oem = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.120:8080/json.htm?type=command&param=udevice&idx=3&nvalue=0&svalue=$potenza;$energia");
      $oem = curl_exec($ch);
curl_close($ch);
 
$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.120:8080/json.htm?type=command&param=udevice&idx=4&nvalue=0&svalue=$cosfi");
      $oem = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.120:8080/json.htm?type=command&param=udevice&idx=72&nvalue=0&svalue=$prelevata");
      $oem = curl_exec($ch);
curl_close($ch);

$ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, "http://192.168.1.120:8080/json.htm?type=command&param=udevice&idx=69&nvalue=0&svalue=$immessa");
      $oem = curl_exec($ch);
curl_close($ch);

sleep(30);
goto inizio;

?>

Re: Using Domoticz with Eastron SDM 220

Posted: Friday 19 August 2016 7:08
by Egregius
You should use a function for the udevice command. Code will be lots smaller and better readable.
You could also combine the two scripts into one.

Code: Select all

#!/usr/bin/php
<?php
$data = array();

inizio:
$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') {
  $tensione1 = $data[0];
  $corrente1 = $data[1];
  $potenza1 = $data[2];
  $cosfi1 = $data[3];
  //$frequenza1 = $data[4];
  $prelevata1 = $data[5];
  $immessa1 = $data[6];
  $energia1 = $data[7];
}
udevice(1,0,$tensione1);
udevice(2,0,$corrente1);
udevice(3,0,$potenza;$energia1);
udevice(4,0,$cosfi1);
udevice(72,0,$prelevata1);
udevice(69,0,$immessa1);

$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') {
  $tensione2 = $data[0];
  $corrente2 = $data[1];
  $potenza2 = $data[2];
  $cosfi2 = $data[3];
  //$frequenza2 = $data[4];
  $prelevata2 = $data[5];
  $immessa2 = $data[6];
  $energia2 = $data[7];
}
udevice(1,0,$tensione2);
udevice(2,0,$corrente2);
udevice(3,0,$potenza;$energia2);
udevice(4,0,$cosfi2);
udevice(72,0,$prelevata2);
udevice(69,0,$immessa2);

//Calculate the total
$totalenergy = $prelevata1 + $prelevata2 - $immessa;
udevice(123,0,$totalenergy);

sleep(30);
goto inizio;

function udevice($idx,$nvalue,$svalue) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=$idx&nvalue=$nvalue&svalue=$svalue");
    curl_exec($ch);
    curl_close($ch);
    usleep(50000);
}
 

Re: Using Domoticz with Eastron SDM 220

Posted: Friday 19 August 2016 23:42
by videodrome
You should use a function for the udevice command. Code will be lots smaller and better readable.
You could also combine the two scripts into one.
Where do I enter in the script the reference to the total energy sensor (sensor n. 57, sensor name "Energia tot. prodotta Pv Fronius")?

Re: Using Domoticz with Eastron SDM 220

Posted: Friday 19 August 2016 23:47
by Egregius
Did you look at the code or just copy/paste it? Have you seen the 'Calculate the total' line?

Re: Using Domoticz with Eastron SDM 220

Posted: Saturday 20 August 2016 0:07
by videodrome
Egregius wrote:Did you look at the code or just copy/paste it? Have you seen the 'Calculate the total' line?
Yes but, correct me if I'm wrong, the value of "$prelevata1" in the line "calculate the total" does not match with the value of energy produced from my photovoltaic. This value (on Kwh) is from another sensor (below) from solar.php script.

Image

Re: Using Domoticz with Eastron SDM 220

Posted: Saturday 20 August 2016 1:24
by Egregius
then combine the script into the solar.php script?

Re: Using Domoticz with Eastron SDM 220

Posted: Saturday 20 August 2016 18:25
by videodrome
Egregius wrote:You should use a function for the udevice command. Code will be lots smaller and better readable.
You could also combine the two scripts into one.
So I just have to paste your script below in solar.php?

Code: Select all

#!/usr/bin/php
<?php
$data = array();

inizio:
$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') {
  $tensione1 = $data[0];
  $corrente1 = $data[1];
  $potenza1 = $data[2];
  $cosfi1 = $data[3];
  //$frequenza1 = $data[4];
  $prelevata1 = $data[5];
  $immessa1 = $data[6];
  $energia1 = $data[7];
}
udevice(1,0,$tensione1);
udevice(2,0,$corrente1);
udevice(3,0,$potenza;$energia1);
udevice(4,0,$cosfi1);
udevice(72,0,$prelevata1);
udevice(69,0,$immessa1);

$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') {
  $tensione2 = $data[0];
  $corrente2 = $data[1];
  $potenza2 = $data[2];
  $cosfi2 = $data[3];
  //$frequenza2 = $data[4];
  $prelevata2 = $data[5];
  $immessa2 = $data[6];
  $energia2 = $data[7];
}
udevice(1,0,$tensione2);
udevice(2,0,$corrente2);
udevice(3,0,$potenza;$energia2);
udevice(4,0,$cosfi2);
udevice(72,0,$prelevata2);
udevice(69,0,$immessa2);

//Calculate the total
$totalenergy = $prelevata1 + $prelevata2 - $immessa;
udevice(123,0,$totalenergy);

sleep(30);
goto inizio;

function udevice($idx,$nvalue,$svalue) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://192.168.X.XXX:8080/json.htm?type=command&param=udevice&idx=$idx&nvalue=$nvalue&svalue=$svalue");
    curl_exec($ch);
    curl_close($ch);
    usleep(50000);
}
 

Re: Using Domoticz with Eastron SDM 220

Posted: Saturday 20 August 2016 18:48
by Egregius
I don't know. What's in solar.php? What does it do?
And do you really need all that data every 30 seconds? For what purpose?
I'm always trying to limit the calls to Domoticz to keep it as fast as possible.

Re: Using Domoticz with Eastron SDM 220

Posted: Saturday 20 August 2016 19:19
by videodrome
solar.php reads the production of photovoltaic system. 3 sensors, 58, 59 and 60 give me the output of power, day and total energy of inverter.
In the function totalenergy to calculate the auto consumption I should replace the value of $prelevata1 with the value of sensor 58 (day energy).


//Calculate the total
$totalenergy = $prelevata1 + $prelevata2 - $immessa;
udevice(123,0,$totalenergy);

Code: Select all

#!/usr/bin/php
<?php
$data = json_decode(file_get_contents('http://192.168.1.103/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceID=1&DataCollection=CommonInverterData'),true);
print_r($data);
if(isset($data['Body']['Data']['PAC']['Value'])) {
    $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=58&nvalue=0&svalue='.$data['Body']['Data']['PAC']['Value']);
    logwrite($result);
    usleep(250000);
        $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=59&nvalue=0&svalue='.$data['Body']['Data']['DAY_ENERGY']['Value']);
    logwrite($result);
    usleep(250000);
        $result = file_get_contents('http://192.168.x.xxx:8080/json.htm?type=command&param=udevice&idx=60&nvalue=0&svalue='.$data['Body']['Data']['TOTAL_ENERGY']['Value']);
    logwrite($result);
}

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); 
}