Skip to content

Instantly share code, notes, and snippets.

@chuyskywalker
Created August 14, 2014 04:48
Show Gist options
  • Save chuyskywalker/95cf72100c48d1482ed6 to your computer and use it in GitHub Desktop.
Save chuyskywalker/95cf72100c48d1482ed6 to your computer and use it in GitHub Desktop.
Phillips Hue: Dance Party
<?php
/*
* Make your Hue lights dance -- a very rudimentary Hue control script
*/
$ip = 'your-ip-here';
$user = 'your-username-here';
$lights = array_flip(array(1,2)); // 3,4,5, etc
foreach ($lights as $lightId => $noOneCares) {
$lights[$lightId] = curl_init();
curl_setopt_array($lights[$lightId], [
CURLOPT_URL => "http://$ip/api/$user/lights/$lightId/state",
CURLOPT_CUSTOMREQUEST => 'PUT',
CURLOPT_RETURNTRANSFER => true,
]);
}
while(1) {
foreach ($lights as $lightId => $curlObj) {
$delay = rand(1,3); // read 100-300ms
$danceValue = json_encode([
'on' => true,
'sat' => rand(100,255), // mostly saturated colors
'bri' => rand(175,255), // mostly bright
'hue' => rand(0,65535), // all over the spectrum
'transitiontime' => $delay, // slight smidge of delays
]);
curl_setopt_array($curlObj, [
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => $danceValue,
CURLOPT_HTTPHEADER => array('Content-Type: application/json', 'Content-Length: ' . strlen($danceValue)),
]);
curl_exec($curlObj);
usleep(100000 * $delay);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment