Haiku Using HaikuHelper's API from PHP

lupinglade

Senior Member
HaikuHelper is designed to make integration between different systems/programming languages and HAI controllers easy. With HaikuHelper's JSON over HTTP API, you can access and control HaikuHelper and your HAI controller with ease from most programming languages and usage scenarios, allowing you to collect information, further automate the system or integrate with third party solutions. Here's an example on how to use HaikuHelper's API from within a PHP script:

Code:
<?php
define('HHAPI_URL', 'http://10.0.1.5:9399/api/');
define('HHAPI_USER', 'OmniPro II');
define('HHAPI_PASS', 'yourpass123');
 
// Grab the name and date properties from the controller object
 
$name = hhapi('controller.name');
echo 'Controller Name: ' . $name . PHP_EOL;
 
$date = hhapi('controller.dateDescription');
echo 'Controller Date: ' . $date . PHP_EOL;
 
// Or you could grab all of the controller object's properties at once of course as below
 
$controller = hhapi('controller');
echo 'Controller Model: ' . $controller->modelDescription . PHP_EOL;
echo 'Controller Phone: ' . $controller->localPhone . PHP_EOL;
 
// Get the areas
 
$areas = hhapi('controller.areas');
foreach($areas as $area) {
	echo 'Area: ' . $area->bestDescription . ', Mode: ' . $area->modeDescription . PHP_EOL;
}
 
// Get the aux sensors
 
$sensors = hhapi('controller.auxiliarySensors');
foreach($sensors as $sensor) {
	echo 'Auxiliary Sensor: ' . $sensor->bestDescription . ', Value: ' . $sensor->valueDescription . PHP_EOL;
}
 
function hhapi($cmd) {
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, HHAPI_URL . $cmd);
	curl_setopt($ch, CURLOPT_USERPWD, HHAPI_USER . ':' . HHAPI_PASS);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
 
	$result = curl_exec($ch);
 
	curl_close($ch);
 
	return json_decode($result);
}

Sample output:

Code:
Controller Name: OmniPro II
Controller Date: March 27, 2012 3:31 PM
Controller Model: OmniPro II
Controller Phone: 5553334567
Area: Garage, Mode: Off
Area: House, Mode: Off
Auxiliary Sensor: Outdoor Temp, Value: 7.0 C
Auxiliary Sensor: Outdoor Humidty, Value: 16% RH
Auxiliary Sensor: Bsmt Temp, Value: 20.0 C
Auxiliary Sensor: Bsmt Humidity, Value: 29% RH
Auxiliary Sensor: Foyer Temp, Value: 21.0 C
Auxiliary Sensor: Foyer Humidity, Value: 36% RH
Auxiliary Sensor: Utility Temp, Value: 18.0 C
And here's how you can control a light, for example:

Code:
hhapi('controller.unitWithNumber(5).on()');
You can do some pretty creative things with this ;-)
 
Updated code to use POST:

Code:
<?php
define('HHAPI_URL', 'http://10.0.1.10:9399/api');
define('HHAPI_USER', 'your.user');
define('HHAPI_PASS', 'your.password');

// Grab the name and date properties from the controller object

$name = hhapi('controller.name');
echo 'Controller Name: ' . $name . PHP_EOL;

$date = hhapi('controller.dateDescription');
echo 'Controller Date: ' . $date . PHP_EOL;

// Or you could grab all of the controller object's properties at once of cours$

$controller = hhapi('controller');
echo 'Controller Model: ' . $controller->modelDescription . PHP_EOL;
echo 'Controller Phone: ' . $controller->localPhone . PHP_EOL;

// Get the areas

$areas = hhapi('controller.areas');
foreach($areas as $area) {
        echo 'Area: ' . $area->bestDescription . ', Mode: ' . $area->modeDescri$
}

// Get the aux sensors

$sensors = hhapi('controller.auxiliarySensors');
foreach($sensors as $sensor) {
        echo 'Auxiliary Sensor: ' . $sensor->bestDescription . ', Value: ' . $s$
}

function hhapi($cmd) {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_URL, HHAPI_URL);
        curl_setopt($ch, CURLOPT_USERPWD, HHAPI_USER . ':' . HHAPI_PASS);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $cmd);

        $result = curl_exec($ch);
        $error = curl_error($ch);

        curl_close($ch);

        if($error) echo $error;

        return json_decode($result);
}
 

Similar threads

Back
Top