This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm that you accept these cookies being set.

Philips Hue Authentication
#2
Hi Koen,

Philips have changed the API due homekit support.

Use this to control your HUE devices with latest HUE FW update :

First run bridgeSetup() once to receive the user (see log after running) and enter where now "xxxxxxxxxxxxxxxxxx' is filled in.

Code:
require('json')
require('socket.http')
require("ltn12")

--************************************************
ip_add = '192.168.1.25'
user = "XXXXXXXXXXXXXXXXXXXXX"    -- Response from bridgeSetup()
--************************************************

response = {}

function bridgeSetup()
 body_bridgeSetup= '{"devicetype":"homelynk#homelynk"}'
 -- Set username for homeLYnk
    socket.http.request({
     url = "http://"..ip_add.."/api",
  method = 'POST',
    sink = ltn12.sink.table(response),
 headers = {
     ['content-length'] = #body_bridgeSetup,
       ['content-type'] = 'application/json',
           },
   source = ltn12.source.string(body_bridgeSetup),
       })
    log(response)
end

function checkUser()
  body_bridgeSetup= '{}'
 -- Set username for homeLYnk
    socket.http.request({
     url = "http://"..ip_add.."/api/"..user,
  method = 'GET',
    sink = ltn12.sink.table(response),
 headers = {
     ['content-length'] = #body_bridgeSetup,
       ['content-type'] = 'application/json',
           },
   source = ltn12.source.string(body_bridgeSetup),
       })
    log(response)
 ret = table.concat(response)
    data = json.pdecode(ret)
 
 if (data[1].error.type)==1 then
   log("Hue bridge user is not set")
   return false
 else
   log("Hue bridge user is set")
   return true
 end
 
end

function searchHueLights()
 body_searchHueLights= ''
 -- Set username for homeLYnk
    socket.http.request({
     url = "http://"..ip_add.."/api/"..user.."/lights",
  method = 'POST',
    sink = ltn12.sink.table(response),
 headers = {
     ['content-length'] = #body_searchHueLights,
       ['content-type'] = 'application/json',
           },
   source = ltn12.source.string(body_searchHueLights),
       })
    log(response)
end

function sendToLight(Light_num,body_request)
 socket.http.request({
     url = "http://"..ip_add.."/api/"..user.."/lights/"..tostring(Light_num).."/state",
  method = 'PUT',
    sink = ltn12.sink.table(response),
 headers = {
       ['content-length'] = #body_request,
       ['content-type'] = 'application/json',
           },
     source = ltn12.source.string(body_request),
       })
    --log(response)
end

function sendToGroup(Group_num,body_request)
 socket.http.request({
     url = "http://"..ip_add.."/api/"..user.."/groups/"..tostring(Light_num).."/action",
  method = 'PUT',
    sink = ltn12.sink.table(response),
 headers = {
       ['content-length'] = #body_request,
       ['content-type'] = 'application/json',
           },
     source = ltn12.source.string(body_request),
       })
    --log(response)
end

function setRGB(Light_num,RGB_variable)
--RGB color set in homeLYnk
Red=bit.rshift(bit.band(RGB_variable,0xff0000),16)
Green=bit.rshift(bit.band(RGB_variable,0x00ff00),8)
Blue=bit.band(RGB_variable,0x0000ff)

-- Norm of RGB values
Red_f=    Red/255
Green_f=Green/255
Blue_f=    Blue/255

--Gamma correction
if Red_f>0.04045 then
 Red_f=((Red_f+0.055)/(1+0.055))^2.4
else
 Red_f=Red_f/12.92
end

if Green_f>0.04045 then
 Green_f=((Green_f+0.055)/(1+0.055))^2.4
else
 Green_f=Green_f/12.92
end

if Blue_f>0.04045 then
 Blue_f=((Blue_f+0.055)/(1+0.055))^2.4
else
 Blue_f=Red_f/12.92
end

--Coversion RGB->xy
X=Red_f*0.649926+Green_f*0.103455+Blue_f*0.197109
Y=Red_f*0.234327+Green_f*0.743075+Blue_f*0.022598
Z=Green_f*0.053077+Blue_f*1.035763

x=X/(X+Y+Z)
y=Y/(X+Y+Z)

--HTTP request send
body_msg = '{"on":true,"xy":['..x..','..y..']}'
sendToLight(Light_num,body_msg)  
end
 
Sample to set RGB value from color picker object event based script:
Code:
value = event.getvalue()
lamp_id = 1

if value == 0 then
 body_msg = '{"on":false}'
 sendToLight(lamp_id,body_msg)
else
 setRGB(lamp_id,value)
end

Good luck (:

BR,
Erwin
Reply


Messages In This Thread
Philips Hue Authentication - by George - 04.02.2017, 02:40
RE: Philips Hue Authentication - by Erwin van der Zwart - 04.02.2017, 20:12
RE: Philips Hue Authentication - by George - 05.02.2017, 06:05

Forum Jump: