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";
}
}
?>