Page 4 of 76
Re: RE: Re: LUA Pass2php
Posted: Wednesday 07 December 2016 8:11
by sincze
Egregius wrote:For me it's not only the speed up of my script, it's the freedom of using a well documented code.
Just for the fun, type in Google lua and php and see the difference.
129000000 vs 10930000000 results!
When I started with Domoticz I googled a lot for lua but found that the userbase is so small and specific that it wasn't easy. PHP on the other hand has such a great userbase that for every issue you have already tons of people posted about it.
Plus, I've seen stuff here in lua of wich I think WTF, really?
A yes. Freedom. I forgot to mention that. Let me try tonight and see if I can become a wizer man.
All my devices have names like 'Lamp Staand Voor' .. 'Motion Keuken' hopefully that will not be an issue with the php. I think
Array... 123->motion_keuken
With
function motion_Keuken() should at least be able to switch idx of 'lamp staand voor' on and off.
I did not know lua as well but mastered the necessary skills to obtain full functionality. Now it is time to do the same with php
Sent from my SM-G925F using Tapatalk
Re: LUA Pass2php
Posted: Wednesday 07 December 2016 9:14
by Egregius
Since version 2.2 you can have spaces in your switch names.
Only thing is that you need to replace them by underscores in the name of your function.
So for you that should be something like:
Code: Select all
function Motion_Keuken()
{
if( $s['Motion Keuken']=='On' && $s['Lamp Staand Voor']=='Off' )
sw($si['Lamp Staand Voor'],'On');
}
Re: LUA Pass2php
Posted: Wednesday 07 December 2016 16:04
by sincze
Just a quick check with your version 2.2
Your 2.2 script_device_pass2php.lua says:
Code: Select all
os.execute('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" http://127.0.0.1/secure/pass2php.php &')
As my file is stored not on a website but locally, can I change this safely to the old:
Code: Select all
os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..changed..'" "'..devices..'" "'..idx..'" "'..lastupdate..'" &')
Or do I need to modify it to:
Code: Select all
os.execute('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" /home/pi/domoticz/scripts/php/pass2php.php &')
Don't know what works. I can try fooling around or just ask
Just 2 be sure.
Re: LUA Pass2php
Posted: Wednesday 07 December 2016 16:10
by Egregius
Yes, shouldn't be a problem as pass2php handles it as 1st, 2nd parameter etc.
You can use os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..changed..'" "'..devices..'" "'..idx..'" "'..lastupdate..'" &')
At least, if you didn't change the lua script.
Otherwise, that would be
os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..c..'" "'..s..'" "'..i..'" "'..t..'" &')
Re: LUA Pass2php
Posted: Wednesday 07 December 2016 19:33
by Westcott
Hi Lvsigo,
Yes, this does work quite quickly -
http://easydomoticz.com/forum/viewtopic ... 55b841a5ce
I need to see how using
curl compares with
io.popen(url) and using a
socket.
On FOR x will be useful as well.
Re: LUA Pass2php
Posted: Wednesday 07 December 2016 19:49
by sincze
Egregius wrote:Yes, shouldn't be a problem as pass2php handles it as 1st, 2nd parameter etc.
You can use os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..changed..'" "'..devices..'" "'..idx..'" "'..lastupdate..'" &')
At least, if you didn't change the lua script.
Otherwise, that would be
os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..c..'" "'..s..'" "'..i..'" "'..t..'" &')
A yes I created this as well.
Code: Select all
JSON=loadfile('/home/pi/domoticz/scripts/lua/JSON.lua')()
base64=loadfile('/home/pi/domoticz/scripts/lua/ee5_base64.lua')()
c=base64.encode(JSON:encode(devicechanged))
s=base64.encode(JSON:encode(otherdevices))
i=base64.encode(JSON:encode(otherdevices_idx))
t=base64.encode(JSON:encode(otherdevices_lastupdate))
os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..c..'" "'..s..'" "'..i..'" "'..t..'" &')
commandArray={}
return commandArray
So that file should be functioning.
If I add "bla" to the code I will see an error in the domoticz log.
If I add a print statement between
Code: Select all
commandArray={}
print "test"
return commandArray
This will show up in the log. Now would there by any chance be something so that I can really check if the command:
Code: Select all
os.execute( '/home/pi/domoticz/scripts/php/pass2php.php "'..c..'" "'..s..'" "'..i..'" "'..t..'" &')
Is executed correctly?
In my pass2php.php script I created the following function:
Code: Select all
function Motion_Kastenkamer()
{
global $s,$i,$t;
if($s['Motion Kastenkamer'=="On" && $s['Lamp (CV Ruimte)']=='Off')
{
sw($i['Lamp (CV Ruimte)'],'On');
lg('test='.$s['write motion in the logfile']);
}
}
However Lamp (CV Ruimte) is not turned on if I move my hand over the Motion sensor.

Re: LUA Pass2php
Posted: Wednesday 07 December 2016 20:32
by Egregius
You can add print or lg statements, see some posts back. Like before the if in the function.
In the first if there's a ] missing.
Dont know if $i['Lamp (CV Ruimte)'] will work, you can avoid that (and for test) by replacing that with the idx of your switch: sw(123,'On')
Re: LUA Pass2php
Posted: Wednesday 07 December 2016 21:32
by sincze
I modified the first part of pass2php.php as shown in a few posts back. However NOTHING shows up in my domoticz log when I trigger for example the ZonweringAuto virtual switch. I guess something else is going wrong. pass2php.php has the following permissions:
-rwxrwxrwx.
I just guess os.execute is not executing anything as the log function does not work.
Code: Select all
<?php
error_reporting(E_ALL);
ini_set("display_errors","on");
date_default_timezone_set('Europe/Brussels');
define('time',$_SERVER['REQUEST_TIME']);
$c=json_decode(base64_decode($_REQUEST['c']),true);
$s=json_decode(base64_decode($_REQUEST['s']),true);
$i=json_decode(base64_decode($_REQUEST['i']),true);
$t=json_decode(base64_decode($_REQUEST['t']),true);
$r=str_replace(" ","_",key($c));
lg('Pass2PHP triggered by '.key($c));
if(function_exists(key($c)))
key($c)();
elseif(function_exists($r))
$r();
//START USER FUNCTIONS
function Motion_Kastenkamer()
{
lg('Executing function Mmotion_Kastenkamer');
global $s,$i,$t;
if($s['Motion Kastenkamer']=="On" && $s['Lamp (CV Ruimte)']=='Off')
{
sw($i['Lamp (CV Ruimte)'],'On');
lg('Switching on the light now');
sw(192,'On');
}
}
function ZonweringAuto()
{
lg('Executing function Mmotion_Kastenkamer');
global $s,$i,$t;
if($s['ZonweringAuto']=="On")
{
lg('Switching on the light now');
sw(192,'On');
}
}
Sending a manual curl
Code: Select all
curl "http://192.168.2.10:8080/json.htm?type=command¶m=addlogmessage&message='hello'"
sends a hello to the domoticz log.
What am i missing here,

just an easy
Code: Select all
php /home/pi/domoticz/scripts/php/pass2php.php ZonweringAuto
would be nice

, however that results into a
Code: Select all
PHP Parse error: syntax error, unexpected '(' in /home/pi/domoticz/scripts/php/pass2php.php on line 13
Re: LUA Pass2php
Posted: Thursday 08 December 2016 4:00
by Egregius
Think I see something...
You didn't specify that the script has to be executed by a PHP interpreter.
Either you put #!/usr/bin/php at the beginning of the PHP file, or you put /usr/bin/php before the path to the script.
I still would consider using curl and place the script on a PHP enabled web server. If you use PHP7 and OpCache the response time will be half of when executed with PHP CLI.
Re: LUA Pass2php
Posted: Thursday 08 December 2016 9:48
by sincze
Maybe move the production environment of my Domoticz to a Synology then

Re: LUA Pass2php
Posted: Thursday 08 December 2016 11:58
by Egregius
It's running smoothly here on a DS1513+ with Jumbotrolls packacke, webstation, PHP7 and MemCached

Re: LUA Pass2php
Posted: Thursday 08 December 2016 16:34
by sincze
Damn....
And now at least my pass2.php is being executed

when any device changes in Domoticz.
The log is flooding with "hello" messages.
Code: Select all
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://192.168.2.10:8080/json.htm?type=command¶m=addlogmessage&message='hello'");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
Now let's see if that solves the rest as well
Mmm.. if I modify the php like this:
Code: Select all
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://192.168.2.10:8080/json.htm?type=command¶m=addlogmessage&message='hello'");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
curl "http://192.168.2.10:8080/json.htm?type=command¶m=addlogmessage&message='hello2'"
?>
Nothing ends up in my domoticz log as if the file is not executed at all.
The command executed from shell
Code: Select all
curl "http://192.168.2.10:8080/json.htm?type=command¶m=addlogmessage&message='hello2'"
Gives me:
Code: Select all
{
"status" : "OK",
"title" : "AddLogMessage"
}
As it seems your script builds the curl scripts like that this might give a clue why the pass2php is not executed.
Re: LUA Pass2php
Posted: Thursday 08 December 2016 17:25
by sincze
Damn....
And now at least my pass2.php is being executed

when any device changes in Domoticz.
The log is flooding with "hello" messages.
Code: Select all
<?php
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://192.168.2.10:8080/json.htm?type=command¶m=addlogmessage&message='hello'");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
However still no luck with flipping the virtual switch... and adding some log lines.
Code: Select all
<?php
lg('Executing something what ... i don't know');
error_reporting(E_ALL);
ini_set("display_errors","on");
date_default_timezone_set('Europe/Brussels');
define('time',$_SERVER['REQUEST_TIME']);
$c=json_decode(base64_decode($_REQUEST['c']),true);
$s=json_decode(base64_decode($_REQUEST['s']),true);
$i=json_decode(base64_decode($_REQUEST['i']),true);
$t=json_decode(base64_decode($_REQUEST['t']),true);
$r=str_replace(" ","_",key($c));
lg('Pass2PHP triggered by '.key($c));
if(function_exists(key($c)))
key($c)();
elseif(function_exists($r))
$r();
//START USER FUNCTIONS
function ZonweringAuto()
{
lg('Executing function ZonweringAuto');
global $s,$i,$t;
if($s['ZonweringAuto']=="On")
{
lg('Switching on the light now');
sw(192,'On');
}
I would at least aspect "Executing something what ... i don't know" and "Pass2PHP triggered by" in my domoticz log.
Ow and I moved to a webserver as well..

, that one is working.
Code: Select all
os.execute('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" http://192.168.2.10/php/pass2php.php &')
Just a question.. I see your script starts with <?php should there be a closure of ?>

that I am missing?
Re: LUA Pass2php
Posted: Thursday 08 December 2016 19:38
by Egregius
No, no closing tag.
But it's not because you installed php5-curl that you can curl a php page.
Use
os.execute( '/usr/bin/php /home/pi/domoticz/scripts/php/pass2php.php "'..c..'" "'..s..'" "'..i..'" "'..t..'" &')
If you want to curl the page you'll need a php enabled webserver.
Re: LUA Pass2php
Posted: Thursday 08 December 2016 20:09
by sincze
Egregius wrote:No, no closing tag.
But it's not because you installed php5-curl that you can curl a php page.
Use
os.execute( '/usr/bin/php /home/pi/domoticz/scripts/php/pass2php.php "'..c..'" "'..s..'" "'..i..'" "'..t..'" &')
If you want to curl the page you'll need a php enabled webserver.
Tnx for feedback. I did the following
- create web server with php.

- hello.png (4.53 KiB) Viewed 2876 times
- webserver and php seem to be working.
Then I did the following:
- script_device_pass2.lua
Code: Select all
JSON=loadfile('/home/linaro/domoticz/scripts/lua/JSON.lua')()
base64=loadfile('/home/linaro/domoticz/scripts/lua/ee5_base64.lua')()
c=base64.encode(JSON:encode(devicechanged))
s=base64.encode(JSON:encode(otherdevices))
i=base64.encode(JSON:encode(otherdevices_idx))
t=base64.encode(JSON:encode(otherdevices_lastupdate))
os.execute('curl -s --data "c='..c..'&s='..s..'&i='..i..'&t='..t..'" http://192.168.2.10/php/pass3.php &')
commandArray={}
return commandArray
So this should call pass3.php from the webserver in case a device changes.
- pass3.php
Code: Select all
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, "http://192.168.2.10:8080/json.htm?type=command¶m=addlogmessage&message='hello'");
curl_setopt($ch, CURLOPT_HEADER, 0);
// grab URL and pass it to the browser
curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);
?>
As a result my domoticz log is filling with "hello".

- hello-domoticz.JPG (24.71 KiB) Viewed 2873 times
But when I change "
http://192.168.2.10/php/pass3.php" to "
http://192.168.2.10/php/pass2php.php" ... nothing happens anymore.
Re: LUA Pass2php
Posted: Thursday 08 December 2016 20:40
by sincze
Did some more debugging
Code: Select all
if(function_exists(key($c)))
key($c)();
elseif(function_exists($r))
$r();
If I comment out this function.. the lg() function does it's job well and fills my domoticz log with messages like:
"
Pass2PHP triggered by <device name>"
i must be doing something wrong with function names then...
Continue debugging

we will get to the starting point .....
Re: LUA Pass2php
Posted: Thursday 08 December 2016 21:18
by sincze
I wanted to know the contents of the other variables $s,$i,$t, this contents seems to be static.
Code: Select all
2016-12-08 21:11:04.682 => Pass2PHP triggered by ZonweringAuto
2016-12-08 21:11:04.684 => Pass2PHP Var s is: AV control
2016-12-08 21:11:04.686 => Pass2PHP Var i is: AV control
2016-12-08 21:11:04.689 => Pass2PHP Var t is: AV control
Re: LUA Pass2php
Posted: Thursday 08 December 2016 21:43
by Egregius
Those are arrays, you need print_r($s) to see them.
Re: LUA Pass2php
Posted: Thursday 08 December 2016 22:01
by sincze
Okay. I got it working now.
Need to make a slight modification to the code.
Code: Select all
<?php
error_reporting(E_ALL);
ini_set("display_errors","on");
date_default_timezone_set('Europe/Brussels');
define('time',$_SERVER['REQUEST_TIME']);
$c=json_decode(base64_decode($_REQUEST['c']),true);
$s=json_decode(base64_decode($_REQUEST['s']),true);
$i=json_decode(base64_decode($_REQUEST['i']),true);
$t=json_decode(base64_decode($_REQUEST['t']),true);
$r=str_replace(" ","_",key($c));
$c2=key($c); // Added this one....
if(function_exists(key($c)))
{
lg('Pass2PHP de functie bestaat: '.key($c));
lg('Pass2PHP echo functie is: '.$c2);
// key($c)(); // Removed this one as function was not executed
$c2(); // Added this... function is now executed.
}
elseif(function_exists($r))
{
lg('Pass2PHP de functie reversed: '.$r);
$r();
}
//START USER FUNCTIONS
function ZonweringAuto()
{
global $s,$i,$t;
if($s['ZonweringAuto']=="On")
{
sw($i['Lamp (CV Ruimte)'],'On');
lg('Event triggered');
}
else
lg('Dont know what to do... tell me..');
}
Don't know why I had add the conversion:
$c2=key($c);
And accordingly replace
key($c)(); with $c2(); to get it working. But now I have a working executing setup and I can move on.
Starting to speed up those scenarios
Re: LUA Pass2php
Posted: Thursday 08 December 2016 22:17
by Egregius
Maybe a difference between php5 & php7. Don't know.
$r is spaces in name replaced by underscores
