Created
August 5, 2014 13:24
-
-
Save ajslaghu/7b134d6923571e509572 to your computer and use it in GitHub Desktop.
A script that demonstrates local API use of Philips Hue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$username = 'newdeveloper'; | |
$red = 65000; | |
$green = 25000; | |
$html = file_get_contents('https://www.meethue.com/api/nupnp'); | |
$jsonpnp = json_decode($html); | |
if ($jsonpnp == null) | |
die; | |
$jsonconfig = file_get_contents('http://' . $jsonpnp[0]->internalipaddress . '/api' . '/' . $username . '/config'); | |
//print_r(json_decode($jsonconfig)); | |
while (true) { | |
// check if the account already exists | |
$html = file_get_contents('http://' . $jsonpnp[0]->internalipaddress . '/api' . '/' . $username . '/lights'); | |
$jsonstatus = json_decode($html); | |
// var_dump(current($jsonstatus)); | |
if (isset(current($jsonstatus)->error)) { | |
print_r(current($jsonstatus)->error); | |
$ci = curl_init(); | |
curl_setopt($ci, CURLOPT_URL, $jsonpnp[0]->internalipaddress . '/api'); | |
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'POST'); | |
curl_setopt($ci, CURLOPT_POSTFIELDS, '{"devicetype":"test user","username":"' . $username . '"}'); | |
$response = curl_exec($ci); | |
print_r($response); | |
echo " Retrying.. press the big button"; | |
sleep(5); | |
} else { | |
echo " Successfully found account\n"; | |
// var_dump($jsonstatus); | |
break; | |
//display waiting message and retry | |
} | |
} | |
// set lights to red with an interval of a sec. | |
$i = 1; | |
foreach ($jsonstatus as $light) { | |
if ($light->state->reachable != true) { | |
$i++; | |
continue; | |
} | |
$ci = curl_init(); | |
curl_setopt($ci, CURLOPT_URL, $jsonpnp[0]->internalipaddress . '/api' . '/' | |
. $username . '/lights' . '/' . $i . '/state'); | |
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ci, CURLOPT_POSTFIELDS, '{"on":true, "hue":' . $red | |
. ', "bri":253, "sat":253}'); | |
$response = curl_exec($ci); | |
print_r($response); | |
print("\n\n"); | |
sleep(1); | |
$i++; | |
} | |
// set lights to green asap. | |
$i = 1; | |
foreach ($jsonstatus as $light) { | |
if ($light->state->reachable != true) { | |
$i++; | |
continue; | |
} | |
$ci = curl_init(); | |
curl_setopt($ci, CURLOPT_URL, $jsonpnp[0]->internalipaddress . '/api' . '/' | |
. $username . '/lights' . '/' . $i . '/state'); | |
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
curl_setopt($ci, CURLOPT_POSTFIELDS, '{"on":true, "hue":' . $green | |
. ', "bri":253, "sat":253}'); | |
$response = curl_exec($ci); | |
print_r($response); | |
print("\n\n"); | |
$i++; | |
} | |
//finally we are done delete account | |
$ci = curl_init(); | |
curl_setopt($ci, CURLOPT_URL, $jsonpnp[0]->internalipaddress . '/api' . '/' | |
. $username . '/config/whitelist/' . $username); | |
curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
$response = curl_exec($ci); | |
print_r($response); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment