adding PHP to z-way-server on rPi

Discussions about Z-Way software and Z-Wave technology in general
Post Reply
rexy666
Posts: 16
Joined: 28 Mar 2013 02:46

adding PHP to z-way-server on rPi

Post by rexy666 »

How do I go to adding PHP to the z-way-server on a Raspberry Pi (RaZberry)
Do I install another http server like Apache?
User avatar
PoltoS
Posts: 7579
Joined: 26 Jan 2011 19:36

To use PHP you need to have

Post by PoltoS »

To use PHP you need to have an HTTP server supporting PHP. Apache is the one to have PHP support out of the box.
rexy666
Posts: 16
Joined: 28 Mar 2013 02:46

which server uses by default?

Post by rexy666 »

thanks! which http server is using currently?
Do I disable it first?
Or just run them both side by side?
User avatar
PoltoS
Posts: 7579
Joined: 26 Jan 2011 19:36

RaZberry uses it's own small

Post by PoltoS »

RaZberry uses it's own small HTTP server based on libmicrohttpd library. You can use both simultaneously.
rexy666
Posts: 16
Joined: 28 Mar 2013 02:46

it's slow

Post by rexy666 »

- using apache as the main server and calling RaZberry's APIs is extremely slow.
- using RaZberry's libmicrohttpd as main server and then using apache for custom functions has worked so far for me, but now I need .htaccess to work and this setup won't work

I understand libmicrohttpd is a very basic http server to add html output from C files and has very limited capabilities, and doesn't support .htaccess or php.

So basically all I can do efficiently is a custom UI using the giving APIs. where is the fun in that?
please give me an example of how do I go about expanding the system, thanks!
User avatar
PoltoS
Posts: 7579
Joined: 26 Jan 2011 19:36

First of all you can use the

Post by PoltoS »

First of all you can use the built in HTTP server to serve your own UI.

But you can also use Apache to serve you UI. It sounds strange for me that your UI served by Apache works slow with RaZberry, since once the page is loaded, Ajax requests goes directly to Z-Way mirohttpd server and there is no difference where were they loaded from (Apacha or Z-Way HTTP server). So, search for a problem in your browser. Also note that any browser running on Raspberry Pi would be extremelly slow - run it on a desktop machine. It might also happen that Apache eats too much resources and this sows down Z-Way too, since they are on same processors. Use "top" to check it.
Colin
Posts: 15
Joined: 13 Sep 2013 22:00

PHP Code For Z-Wave Razberry + Raspberry Pi Server

Post by Colin »

Anyone interested in using Razberry with php may find my below php code to be a useful starting point. Save the below code to a file and then run it via SSH/BASH to see the output as the "Z-Wave Binary Switch" is turned on or off etc.

Note: Don't run this code from a web browser because you won't see the output obviously, needs to be run via BASH.

The Z-Wave Php Code:

IT IS SWITCHED ON!! ";
echo "
";//THIS CARRIAGE RETURN MEANS PRINT OVER AND OVER THE SAME LINE ON BASH :-)
$BooleanBinarySwitch=true;
}
else
{
$BooleanBinarySwitch=false;
echo "Binary Switch Value=".$ResultFromCurl." @ Generic Unix Time 33[01;31m $t 33[0m ";
echo "
";//THIS CARRIAGE RETURN MEANS PRINT OVER AND OVER THE SAME LINE ON BASH :-)
}

CallFunctionOnlyOnceIfTrue_BinarySwitch($BooleanBinarySwitch,'SendEmail');//execute the function called SendEmail() ONLY ONCE if $BooleanBinarySwitch is true, resets if BooleanBinarySwitch is false. This is here so that lots of emails aren't sent out at once....

}//End of infinite while loop


function CallFunctionOnlyOnceIfTrue_BinarySwitch($BooleanOfSpecificSwitch,$functionToBeCalled)
{
static $executed = false; //static i.e. doesn't lose its value...
if($BooleanOfSpecificSwitch==true)
{
if ($executed==false)
{
echo "

Do something only once..."." Emailing via Executing the function called ".$functionToBeCalled."()
";

$functionToBeCalled();//Execute this function
$executed=true;
}
}
else
{
$executed=false; //reset
}
}


function BinarySwitchValue($SpecificBinarySwitch)//return value (0=off or 255=true) of the Binary Switch
{
global $ZwaveServerPath;
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
$ZwayServerRemainingPath="/ZWaveAPI/Run/".$SpecificBinarySwitch;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Return the response as a string
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_URL, $ZwaveServerPath.$ZwayServerRemainingPath);
return curl_exec($ch);
}

function SendEmail()
{
$to = "YourEmailAddressGoesHere.com";
$subject = "PHP Test mail - Binary Switch On";
$message = "This is a test email sent from my Z-Wave Raspberry Pi Server via php code. The Binary Switch has just been turned on...";
$from = "you@your.domain";
$headers = "From:" . $from;
mail($to,$subject,$message,$headers);
echo "Mail Sent...

";
}
?>


=========================================================================================


Here is a slightly better way of monitoring transitions:

= a UNIX time of 1379611025 etc

This code contacts the Z-Way Z-Wave Raspberry Pi Server every 'Current UNIX Time less one second'. If a Z-Wave change occurs then the O/P looks something like:

[The JSON array that is returned if a switch is turnedon/turned off/updated is something like]

{
"devices.2.data.lastSend": {
"name": "lastSend",
"value": 2861774,
"type": "int",
"invalidateTime": 1379548094,
"updateTime": 1379633915
},
"devices.2.data.lastReceived": {
"name": "lastReceived",
"value": 0,
"type": "int",
"invalidateTime": 1379515448,
"updateTime": 1379633915
},
"devices.2.data.failureCount": {
"name": "failureCount",
"value": null,
"type": "NoneType",
"invalidateTime": 1379548094,
"updateTime": 1379633915
},
"devices.2.instances.0.commandClasses.37.data.level": {
"name": "level",
"value": 0,
"type": "int",
"invalidateTime": 1379633914,
"updateTime": 1379633915
},
"updateTime": 1379633925
}


[If no chage has occured recently then the O/P looks something like]

{
"updateTime": 1379635909
}


*/


$TimeLessXSecond=1;//deduct this number from the current UNIX time stamp i.e. get the last X seconds worth of results

$LastValueFoundForBinarySwitch="";
// create a new cURL resource
$ch = curl_init();
// set URL and other appropriate options
$ZwaveServerPath="http://192.168.1.7:8083";

$ZwayServerRemainingPath="/ZWaveAPI/Data/";
$ResultsAfterAUnixTimeof="0";//All data since 1970 (i.e. I don't need all the history) so I will change this below
curl_setopt($ch, CURLOPT_URL, $ZwaveServerPath.$ZwayServerRemainingPath.$ResultsAfterAUnixTimeof);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //Return the response as a string
//curl_setopt($ch, CURLOPT_URL, "http://192.168.1.7:8083/ZWaveAPI/Data/0");
curl_setopt($ch, CURLOPT_HEADER, false);

echo "
This code starts checking for changes from when it is run onwards

";

$i=1;
while($i0)
{
echo "[Last Reported value of Binary Switch 33[0;32m".$LastValueFoundForBinarySwitch."33[0m]"."
";
}

try{
//Note below regarding the Command Class that 37 Decimal = 25 Hexadecimal i.e. (0x25)

if (array_key_exists('devices.2.instances.0.commandClasses.37.data.level', $obj)) {
print "Binary Switch value recently changed to: 33[0;31m".$obj['devices.2.instances.0.commandClasses.37.data.level']['value']."33[0m"."

";
$LastValueFoundForBinarySwitch=$obj['devices.2.instances.0.commandClasses.37.data.level']['value'];
}
}
catch(Exception $e)
{
echo "Error $e";
}

}
?>
Post Reply