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 api v2
#14
(04.11.2024, 14:17)Fcs Wrote: Hi @gjniewenhuijse,

Have you been able to make any progress on using api V2? like you, I need to use dynamic scenes and motion sensors.

thanks you

I made some code to use the api v2

Code:
-- hue v2 api begin --------------------------------------------------------------------------------------------------

--[[
https://developers.meethue.com/develop/hue-api-v2/api-reference/
https://developers.meethue.com/develop/hue-api-v2/getting-started/
--]]

-- bridge communication base
function hueExec(iMethod, iCommand, iBody)
    -- load modules
  https = require('ssl.https')
  ltn12 = require('ltn12')
  json = require('json')
  resp = {}
  body = json.encode(iBody)
  res, code, headers = https.request({
    url = 'https://'..bridge..'/clip/v2/resource'..iCommand,
    method = iMethod,
    source = ltn12.source.string(body),
    sink = ltn12.sink.table(resp),
    headers = { ['hue-application-key'] = usr,
                ['Content-Type'] = 'application/json',
                ['Accept'] = 'application/json',
                ['Content-Length'] = #body   
              }
  })
  -- handle feedback
  if not res or code ~= 200 then
    return res,code
  else
    return json.pdecode(table.concat(resp))
  end
end


-- get all lights
function getLights()
    return hueExec('GET', '/light')
end
--log(getLights().data)


-- get light
function getLight(iLight)
    return hueExec('GET', '/light/'..iLight)
end
--log(getLight('e4a916c9-9ca3-4f3d-98be-779c55a90b5d'))


-- (api v2) activate scene (active or dynamic_palette)
function runScene(iScene, iAction)
  vAction = iAction or 'active'
    hueExec('PUT', '/scene/'..iScene, {recall = {action = vAction}})
end
--runScene('185d4336-273e-4f09-913c-91c76d85bfac') -- woonkamer soho
--runScene('185d4336-273e-4f09-913c-91c76d85bfac', 'dynamic_palette') -- woonkamer soho dynamic

-- hue v2 api end --------------------------------------------------------------------------------------------------

and tested with the event handler
Code:
--[[
https://developers.meethue.com/develop/hue-api-v2/migration-guide-to-the-new-hue-api/#Event%20Stream
--]]

-- load modules
json = require('json')

if not sock then
  host = 'x.x.x.x'
  port = 443
  proto = 'tlsv12'
  path = '/eventstream/clip/v2'
  appkey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

  require('ssl')
  sock = require('socket').tcp()
  sock:settimeout(10)
  res, err = sock:connect(host, port)
  if res then
    sock = ssl.wrap(sock, proto)
      res, err = sock:dohandshake()
    if res then
      sock:send(
        'GET ' .. path .. ' HTTP/1.1\r\n' ..
        'Host: ' .. host .. '\r\n' ..
        'Accept: text/event-stream\r\n' ..
        'hue-application-key: ' .. appkey .. '\r\n' ..
        '\r\n'
      )
        else
        log('handshake failed: ' .. tostring(err))
      end
  else
    log('connect failed: ' .. tostring(err))
    sock:close()
  end
end

line, err = sock:receive()
if line then
  log('line: ' .. line)
  if line:find(': hi') then
    log('connection ok: ', line)
  elseif line:find('data:') then
    line = '{"data": '..string.sub(line,7)..'}'
        log('textline',line)
    line = json.pdecode(line)
    log(line.data)
    for _l,l_items in pairs(line.data) do
      log(l_items)
      log(l_items.type, l_items.data[1].id_v1, l_items.data[1].on.on)
    end
  end
else
  log('receive failed: ' .. tostring(err))
  sock:close()
  sock = nil
end
Reply


Messages In This Thread
Philips Hue api v2 - by gjniewenhuijse - 02.02.2022, 13:17
RE: Philips Hue api v2 - by gjniewenhuijse - 02.02.2022, 14:53
RE: Philips Hue api v2 - by admin - 02.02.2022, 15:53
RE: Philips Hue api v2 - by gjniewenhuijse - 02.02.2022, 18:51
RE: Philips Hue api v2 - by admin - 03.02.2022, 07:21
RE: Philips Hue api v2 - by gjniewenhuijse - 03.02.2022, 09:11
RE: Philips Hue api v2 - by admin - 03.02.2022, 09:22
RE: Philips Hue api v2 - by gjniewenhuijse - 03.02.2022, 09:34
RE: Philips Hue api v2 - by admin - 03.02.2022, 09:50
RE: Philips Hue api v2 - by gjniewenhuijse - 03.02.2022, 10:12
RE: Philips Hue api v2 - by gjniewenhuijse - 03.02.2022, 14:58
RE: Philips Hue api v2 - by Joep - 10.01.2023, 09:16
RE: Philips Hue api v2 - by Fcs - 04.11.2024, 14:17
RE: Philips Hue api v2 - by gjniewenhuijse - 05.11.2024, 07:18

Forum Jump: