Advertisement
alcaron

PS-HueCycle

Dec 4th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # PowerShell script to randomly set a bulb to a random color (from a pre-defined list) and random brightness.
  2.  
  3. function Hue-Command([string]$light, [string]$func, [string]$json)
  4. {
  5.     # Convert post data to UTF8.
  6.     $post = [System.Text.Encoding]::UTF8.GetBytes($json)
  7.  
  8.     # Define location of Hue bridge, could just as easily be passed to the function via variable.
  9.     $url = "http://192.168.1.114/api/58d35ff5cd80afcc09d4b77e70567590/lights/{0}/{1}" -f $light,$func
  10.  
  11.     # Build WebRequest. Make sure to set the type to PUT, the content to text/plain and the length just in case. May be able to get away without this but why bother.
  12.     $http = [System.Net.WebRequest]::Create($url)
  13.     $http.Method = "PUT"
  14.     $http.ContentType = "text/plain"
  15.     $http.ContentLength = $post.Length
  16.     $http.ServicePoint.Expect100Continue = $false
  17.  
  18.     # Create, comit and close stream.
  19.     $stream = $http.GetRequestStream()
  20.     $stream.Write($post, 0, $post.Length)
  21.     $stream.Close()
  22.  
  23.     # Capture response, if you don't care if it fails or not you could probably skip this, but...
  24.     [System.Net.WebResponse] $response = $http.GetResponse();
  25.     $rs = $response.GetResponseStream()
  26.     $read = New-Object "System.IO.StreamReader" -ArgumentList $rs
  27.     $result = $read.ReadToEnd()
  28.    
  29.     # Punt the WebRequest response.
  30.     return $result
  31. }
  32.  
  33. # Variable used for limit tracking.
  34. [int]$cvar = 1
  35.  
  36. # Pre-Defined list of desired colors.
  37. $color = @("[0.1855,0.0765]", "[0.5916,0.3764]", "[0.6473,0.3309]", "[0.4401,0.2215]", "[0.1855,0.0765]", "[0.6363,0.3352]")
  38.  
  39. # At 500 this will run for 83.33 minutes, set higher or lower accordingly, or change to while($cvar) to run indefinitely.
  40. while($cvar -lt 500)
  41. {
  42.     $bri = Get-Random -Minimum 20 -Maximum 254
  43.     # Set -Maximum one higher than the number of bulbs you have.
  44.     $bulb = Get-Random -Minimum 1 -Maximum 4
  45.     $shift = $color | Get-Random
  46.     "Shift"+$shift
  47.     # Whatever command you want to pass, put it here. Not limited to brightness or color.
  48.     Hue-Command $bulb "state" "{`"bri`":$bri, `"xy`":$shift}"
  49.     Start-Sleep -Seconds 10
  50.     $cvar = $cvar+1
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement