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.

WLED
#5
There you go, I will attach the library i made. Sorry I am italian so most of the comments are in italian

Code:
json = require('json')
http = require('socket.http')
wled_ctrl = {
  defaults = {
      debug_mode = false,
    token = nil
  }
}
--[[
Tutta la documentazione sulla Api puo essere ritrovata a https://kno.wled.ge/interfaces/json-api/.
Per usare questa libreria sará necessario creare degli oggetti a cui poi collegare uno script su evento
in cui richiamare le varie funzioni.
Es. indirizzo di gruppo 0/0/1 COMANDO ON-OFF WLED verra collegato allo script su evento
            local on_off = event.getvalue()
            wled = wled_ctrl:init({ip = {{IP}}, debug_mode = false})
            wled:accensione(on_off, ga_stato)
]]--
----- CREAZIONE DELLA CLASSE WLED CONTROLLER -----
function wled_ctrl:init(params)
  local n = setmetatable({}, { __index = wled_ctrl })
  local k,v
  -- set user parameters
  n.params = params
 
  for k, v in pairs(wled_ctrl.defaults) do
    if n.params[ k ] == nil then
      n.params[ k ] = v
    end
  end   
  return n
end
----- RICHIESTA POST HTTP GENERICA -----
function wled_ctrl:basic_post_req(body_req)
  res, err = http.request({
  url = 'http://'..self.params.ip..'/json/state',
  method = "POST",
  body = body_req,
  headers = {["Content-Type"] = "text/plain"}
  })
  return res,err
end
----- ACCENSIONE -----
function wled_ctrl:accensione(valore, stato)
  body1= '{"on":'..tostring(valore)..'}'
  res,err = self:basic_post_req(body1)
  if err == 200 then grp.write(stato, valore) end
  if self.params.debug_mode and err ~= 200 then log("accensione - Error: "..tostring(err)) end
end
----- LUMINOSITA -----
function wled_ctrl:luminosita(valore, stato)
  body1= '{"bri":'..tostring(math.floor(valore*2.55))..'}'
  res,err = self:basic_post_req(body1)
  if err == 200 then grp.write(stato, valore) end
  if self.params.debug_mode and err ~= 200 then log("luminosita - Error: "..tostring(err)) end
end
----- COLORE -----
function wled_ctrl:colore(valore, stato)
  hexcolor = string.format("%X",tostring(valore))
  while hexcolor:len() < 6 do
      hexcolor = "0"..hexcolor
    end
  log(hexcolor)
  body1= '{"seg":[{"col":["'..hexcolor..'"]}]}'
  res,err = self:basic_post_req(body1)
  if err == 200 then grp.write(stato, valore) end
  if self.params.debug_mode and err ~= 200 then log("colore - Error: "..tostring(err)) end
end
----- EFFETTI -----
function wled_ctrl:effetto(valore, stato)
  body1= '{"seg":[{"fx":'..tostring(valore)..'}]}'
  res,err = self:basic_post_req(body1)
  if err == 200 then grp.write(stato, valore) end
  if self.params.debug_mode and err ~= 200 then log("effetto - Error: "..tostring(err)) end
end
----- COLOR TEMP -----
function wled_ctrl:color_temp(valore, stato)
  body1= '{"seg":[{"cct":'..tostring(valore)..'}]}'
  res,err = self:basic_post_req(body1)
  if err == 200 then grp.write(stato, valore) end
  if self.params.debug_mode and err ~= 200 then log("color temp - Error: "..tostring(err)) end
end
----- PALETTE -----
function wled_ctrl:palette(valore, stato)
    body1= '{"seg":[{"pal":'..tostring(valore)..'}]}'
  res,err = self:basic_post_req(body1)
  if err == 200 then grp.write(stato, valore) end
  if self.params.debug_mode and err ~= 200 then log("palette - Error: "..tostring(err)) end
end 
----- STATO -----
function wled_ctrl:stato()
  res,err = self:basic_post_req('{}')
  local info = json.decode(res)
  if self.params.debug_mode then log("Response: "..tostring(res).." Error: "..tostring(err)) end
  return {
    accensione = info.state.on,
    luminosita = info.state.bri,
    colore =info.state.seg[1].col[1],
    effetto = info.state.seg[1].fx,
    palette = info.state.seg[1].pal
  }
end 
Reply


Messages In This Thread
WLED - by danborge - 20.05.2024, 20:43
RE: WLED - by admin - 21.05.2024, 07:24
RE: WLED - by Andrea Becagli - 21.05.2024, 11:37
RE: WLED - by danborge - 21.05.2024, 18:05
RE: WLED - by Andrea Becagli - 22.05.2024, 15:31
RE: WLED - by danborge - 22.05.2024, 16:45
RE: WLED - by AlexLV - 27.05.2024, 21:49

Forum Jump: