项目作者: cormullion

项目描述 :
interface Philips Hue light bulbs from Julia
高级语言: Julia
项目地址: git://github.com/cormullion/PhilipsHue.jl.git
创建时间: 2014-09-22T07:54:17Z
项目社区:https://github.com/cormullion/PhilipsHue.jl

开源协议:Other

下载


PhilipsHue

juliaphilips logo

Functions to control Philips Hue light bulbs from Julia.

Uses JSON, HTTP, and Colors packages.

Summary

To access and control the bridge you need to know its IP address, and register and obtain a ‘username’ (a string of hex) which you can use in subsequent sessions.

If you haven’t used your PhilipsHue bridge before

For the very first run:

  1. using PhilipsHue
  2. B = PhilipsHueBridge("192.168.1.2")
  3. initialize(B, devicetype="juliascript#user1")

You’ll now have to run to the Hue bridge and hit the button:

  1. julia> initialize(B, devicetype="juliascript#user1")
  2. initialize(): Trying to get the IP address of the Philips bridge.
  3. initialize(): Found bridge at 192.168.1.2.
  4. initialize(): Trying to register juliascript#user1 with the bridge at 192.168.1.2...
  5. register(): Quick, you have ten seconds to press the button on the bridge!
  6. register(): Successfully registered juliascript#user1 with the bridge at 192.168.1.2
  7. register(): username is KbZxj8G5nBDsDYgqOmHicytLC-aTALLSEaJN
  8. initialize(): Registration successful
  9. your username is KbZxj8G5nBDsDYgqOmHicytLC-aTALLSEaJN
  10. true

and the ‘username’ should also be stored in a field of B. Remember it!

  1. testlights(B)

B now refers to your bridge, and most of the functions require this as the first argument.

Once you know your username

In a subsequent Julia session, or if you already know your ‘username’, you don’t have to do this initialization, and can simply do:

  1. using PhilipsHue
  2. B = PhilipsHueBridge("192.168.1.2", "KbZxj8G5nBDsDYgqOmHicytLC-aTALLSEaJN")
  3. testlights(B)

since you remembered that ‘username’.

Try it out

You can now try:

  1. isinitialized(B) # returns true if correctly initialized
  2. getbridgeinfo(B) # returns a dictionary with information about the bridge
  3. getlight(B, 1) # return a dictionary of information about light #1
  4. getlights(B) # return a dictionary with info about all lights on the bridge
  5. getlightnumbers(B) # return array of the "logical" light numbers
  6. # set the color of light #1:
  7. setlight(B, 1, Dict("sat" => 128, "on" => true, "hue" => 20000, "bri" => 200))
  8. using Colors
  9. setlight(B, 1, colorant"Pink")
  10. # set all the lights to the same settings
  11. setlights(B, Dict("sat" => 128, "on" => true, "hue" => 20000, "bri" => 200))
  12. # quickly test lights for say 20 repetitions
  13. testlights(B, 20)
  14. # cycle colors for all colored lights with delay 1 repetitions 10
  15. randomcolors(B, 1, 10)

Setting light parameters

To set the parameters of a light, pass a dictionary with one or more key/value pairs to one of the set functions. Typically this dict is something like this:

  1. Dict{Any,Any}("bri" => rand(0:255), "hue" => rand(1:65000), "sat" => rand(1:255))

where “sat” and “bri” values are saturation and brightness from 0 to 255, and “hue” is from 0 to 65280 (?), where 0 is red, yellow is 12750, green is 25500, blue is 46920, etc. If any keys are omitted, that aspect of the light won’t be changed. Keys are strings, values can be numeric and will get converted to strings.

  1. setlight(B, 1, Dict("on" => false))
  2. setlight(B, 1, Dict("on" => true, "hue" => 10000))
  3. setlights(B, Dict("sat" => 255, "bri" => 255, "hue" => 20000, "on" => true))
  4. setlights(B, Dict("sat" => 25, "on" => true))
  5. setlights(B, Dict("sat" => "25", "on" => true))

Using the definitions and conversions in Colors.jl, you can do things like this:

  1. using Colors
  2. setlight(B, 1, RGB(1, 0.3, 0.6))
  3. setlight(B, 1, colorant"Pink")

Getting information

Note that light numbers are not necessarily consecutive, or numbered from 1. For example, if you have four lights, their numbers might be [1, 3, 6, 9]. You can use getlightnumbers() to get the numbers of lights connected to the bridge:

  1. getlightnumbers(bridge::PhilipsHueBridge)

which returns eg:

  1. [1, 3, 5, 6]

Get information for a specific light. For example:

  1. getlight(B, 2)

returns (true,25,254,15000) - On, Saturation, Brightness, Hue

Get the bridge’s current registered IP address (which can usually be read from http://meethue.com using get("https://www.meethue.com/api/nupnp")):

  1. getIP()

Get the current bridge configuration:

  1. getbridgeconfig(B)

which returns a dictionary with keys such as “timezone”, “apiversion”, “paddles”, “whitelist”, and so on. You can obtain specific values:

  1. getbridgeinfo(B)["apiversion"]

returns

  1. "1.29.0"

A list of current approved apps and users is stored in the whitelist:

  1. getbridgeinfo(B, "config")["whitelist"]
  1. Dict{AbstractString,Any} with 25 entries:
  2. "lSsXQfrm7rC32SQ0" => Dict{AbstractString,Any}("name"=>"fred’s iPhone","last use date"=>"2015-08-27T10:32:24","create date"=>"2013-12-31T14:05:05")
  3. "352f5a4637d93da7121471aa39dc2db" => Dict{AbstractString,Any}("name"=>"Harmony","last use date"=>"2014-10-13T04:56:30","create date"=>"2014-09-06T12:15:28")
  4. "5F062ECE840DB0FE8D468855570D2EB9" => Dict{AbstractString,Any}("name"=>"QuickHue","last use date"=>"2014-01-09T14:47:13","create date"=>"2014-01-01T12:24:36")
  5. "0f607264fc6318a92b9e13c65db7cd3c" => Dict{AbstractString,Any}("name"=>"fred’s iPad","last use date"=>"2014-01-03T00:13:28","create date"=>"2013-12-31T13:10:36")
  6. "hue-js-JkRkZIa8Lasd" => Dict{AbstractString,Any}("name"=>"Philips hue JavaScript","last use date"=>"2015-06-12T13:34:00","create date"=>"2015-06-12T13:26:00")
  7. "newjuliauser2" => Dict{AbstractString,Any}("name"=>"test developer","last use date"=>"2014-09-21T14:30:18","create date"=>"2014-09-21T13:40:11")
  8. "q3a619KRfwoS6suF" => Dict{AbstractString,Any}("name"=>"HueForAppleWatch#fred’s iPhone 6","last use date"=>"2015-08-20T22:47:41","create date"=>"2015-08-20T16:50:32")
  9. "juliauser1" => Dict{AbstractString,Any}("name"=>"juliascript","last use date"=>"2015-08-27T15:06:43","create date"=>"2014-09-21T10:30:53")
  10. ...

Get information for all lights:

  1. getlights(B)

For example:

  1. getlights(B)
  1. Dict{String,Any} with 3 entries:
  2. "1" => ["name"=>"Hue Lamp","swversion"=>"66010820","pointsymbol"=>["8"=>"none","4"=>"none","1"=>"none","5"=>"none",…
  3. "2" => ["name"=>"Hue Lamp 1","swversion"=>"66010820","pointsymbol"=>["8"=>"none","4"=>"none","1"=>"none","5"=>"none…
  4. "3" => ["name"=>"Hue Lamp 2","swversion"=>"66010820","pointsymbol"=>["8"=>"none","4"=>"none","1"=>"none","5"=>"none