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.

Twinkly script
#1
I like to connect my Twinkly christmas leds to the LM.

I found a LUA example for Fibaro (see attached Twinkly.LUA), but i can't get the example working.

My code:
Code:
--[[ documentation
api: https://xled-docs.readthedocs.io/en/latest/rest_api.html
mqtt: https://xled-docs.readthedocs.io/en/latest/msqtt_api.html
--]]

-- init
if not init then
  require('socket.http')
  require('ltn12')
  require('json')
  socket.http.TIMEOUT = 5
  host = "192.168.x.xx"
    authToken = nil
  init = true
end

local api = {
    base = "/xled/v1/",
    endpoints = {
        login = "login",
        verify = "verify",
        mode = "led/mode",
        deviceName = "device_name",
        reset = "led/reset",
        movieConfig = "led/movie/config",
        movie = "led/movie/full",
        gestalt = "gestalt",
        brightness = "led/out/brightness"
    }
}

function getTwinklyResponseData(endPoint, body, content_type)
    local content_type = content_type or "application/json"
    local url = "http://" .. host .. api.base .. api.endpoints[endPoint]
  local method = "GET"
  if body ~= nil then
    method = "POST"
    if type(body) == "table" and content_type == "application/json" then
      body = json.encode(body)
    end
  end

  local response_body = {}
  local payload = body
  log('input', url, method, payload)
  local res, code, response_headers, status = socket.http.request
    {
      url = url,
      method = method,
      headers =
      {
        ["Content-Type"] = content_type,
        ["X-Auth-Token"] = authToken
      },
      source = ltn12.source.string(payload),
      sink = ltn12.sink.table(response_body)
    }
  log(res, code, response_headers, status)
  log('payload', payload)
  log('response_body', response_body)
  if res and code == 200 then
    return true
  else
    return false
  end
end


log('start')
if getTwinklyResponseData("gestalt") then
  getTwinklyResponseData("login",{challenge = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\a"})
end
log('einde')

my data response from "gestalt" looks right with the correct device information, but the "login" gives as result:
Code:
* arg: 1
  * number: 1
* arg: 2
  * number: 200
* arg: 3
  * table:
   ["server"]
    * string: esp-httpd/0.5
   ["content-type"]
    * string: application/json
   ["connection"]
    * string: close
* arg: 4
  * string: HTTP/1.1 200 OK
thats looks also right, but my response_data is: 
Code:
* arg: 1
  * string: response_body
* arg: 2
  * table:
   [1]
    * string: {"code":1106}
and thats not right i think.

Who can help me?

Attached Files
.lua   Twinkly.LUA (Size: 10.11 KB / Downloads: 2)
Reply


Messages In This Thread
Twinkly script - by gjniewenhuijse - 07.12.2020, 09:12
RE: Twinkly script - by admin - 07.12.2020, 10:02
RE: Twinkly script - by gjniewenhuijse - 07.12.2020, 10:21
RE: Twinkly script - by admin - 07.12.2020, 11:14
RE: Twinkly script - by gjniewenhuijse - 07.12.2020, 12:02
RE: Twinkly script - by gjniewenhuijse - 07.12.2020, 19:50

Forum Jump: