Guest User

Hue php script

a guest
Oct 24th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. <?php
  2.  
  3. // Code copied from http://www.everyhue.com/vanilla/discussion/comment/1052#Comment_1052
  4. $HUE_BRIDGE_ADRESS = 'address';
  5. $HUE_API_KEY = 'key';
  6.  
  7. $url = ''.$HUE_BRIDGE_ADRESS.'/api/'.$HUE_API_KEY.'/lights';
  8.  
  9. $bulbs = $_GET["bulb"];
  10. $bulblist = explode(',', $bulbs);
  11.  
  12. foreach($bulblist as $bulb)
  13. {
  14. $previousState[$bulb] = getState($bulb);
  15.  
  16. $newState = createData();
  17.  
  18. $response = sendData($newState, $bulb);
  19. echo $response;
  20. }
  21.  
  22. if ($blinktime = $_GET["blinktime"]){
  23. sleep($blinktime);
  24. foreach($bulblist as $bulb)
  25. {
  26. print_r($previousState[$bulb]);
  27.  
  28. $response = sendData($previousState[$bulb], $bulb);
  29. echo $response;
  30. }
  31. }
  32.  
  33. function getState($bulb){
  34. global $url;
  35. echo(''.$url.'/'.$bulb.'');
  36. $cf = curl_init(''.$url.'/'.$bulb.'');
  37. curl_setopt($cf, CURLOPT_RETURNTRANSFER, true);
  38. curl_setopt($cf, CURLOPT_CUSTOMREQUEST, "GET");
  39. $response = curl_exec($cf);
  40. $lightstate = json_decode($response);
  41. if(!$response) {
  42. return false;
  43. }
  44.  
  45. return $lightstate->state;
  46. }
  47.  
  48. function createData(){
  49. if (isset($_GET['hue'])) $arrData['hue'] = (int)$_GET['hue'];
  50. else $arrData['hue'] = 100;
  51.  
  52. if (isset($_GET['sat'])) $arrData['sat'] = (int)$_GET['sat'];
  53. else $arrData['sat'] = 254;
  54.  
  55. if (isset($_GET['trans'])) $arrData['transitiontime'] = (int)$_GET['trans'];
  56. else $arrData['transitiontime'] = 10;
  57.  
  58. if (isset($_GET['bri'])) $arrData['bri'] = (int)$_GET['bri'];
  59. else $arrData['bri'] = 240;
  60.  
  61. if (isset($_GET['on'])) $arrData['on'] = (bool)$_GET['on'];
  62. else $arrData['on'] = true;
  63.  
  64. return $arrData;
  65. }
  66.  
  67. function sendData($data, $bulb){
  68. global $url;
  69. $ch = curl_init(''.$url.'/'.$bulb.'/state');
  70. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  71. curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
  72. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
  73. $response1 = curl_exec($ch);
  74.  
  75. if(!$response1) {
  76. return false;
  77. }
  78. return response1;
  79. }
  80. ?>
Advertisement
Add Comment
Please, Sign In to add comment