Logic Machine Forum
WLED - Printable Version

+- Logic Machine Forum (https://forum.logicmachine.net)
+-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1)
+--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8)
+--- Thread: WLED (/showthread.php?tid=5423)



WLED - danborge - 20.05.2024

Hi,

Has anyone done a WLED integration? Tried searching the forums (and google), without luck. My goal is to use KNX+ LM visualization to control WLED (on/off, dim, color, effects).

I've also tried looking at other json scripts, but its quite difficult to understand with zero lua scripting knowledge...
The WLED page on json isn't very helpful (https://kno.wled.ge/interfaces/json-api/)

WLED also support MQTT (https://kno.wled.ge/interfaces/mqtt/), but that was even more difficult to understand Confused

--Dan


RE: WLED - admin - 21.05.2024

Example for brightness control.

Attach an event script to 0..100% scale object. Change WLED_IP to your WLED device IP address.

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

url = 'http://WLED_IP/json/state'

value = event.getvalue()

data = json.encode({
  on = value > 0,
  bri = math.round(value * 2.55),
})

res, code = http.request({
  url = url,
  method = 'POST',
  headers = {
    ['content-type']  = 'application/json',
    ['content-length'] = #data,
  },
  source = ltn12.source.string(data),
})

log(res, code)



RE: WLED - Andrea Becagli - 21.05.2024

(20.05.2024, 20:43)danborge Wrote: Hi,

Has anyone done a WLED integration? Tried searching the forums (and google), without luck. My goal is to use KNX+ LM visualization to control WLED (on/off, dim, color, effects).

I've also tried looking at other json scripts, but its quite difficult to understand with zero lua scripting knowledge...
The WLED page on json isn't very helpful (https://kno.wled.ge/interfaces/json-api/)

WLED also support MQTT (https://kno.wled.ge/interfaces/mqtt/), but that was even more difficult to understand Confused

--Dan

I have developed a pretty good library if you want. It just controls one segment though, but you can adapt it a little to control more than one


RE: WLED - danborge - 21.05.2024

(21.05.2024, 11:37)Andrea Becagli Wrote:
(20.05.2024, 20:43)danborge Wrote: Hi,

Has anyone done a WLED integration? Tried searching the forums (and google), without luck. My goal is to use KNX+ LM visualization to control WLED (on/off, dim, color, effects).

I've also tried looking at other json scripts, but its quite difficult to understand with zero lua scripting knowledge...
The WLED page on json isn't very helpful (https://kno.wled.ge/interfaces/json-api/)

WLED also support MQTT (https://kno.wled.ge/interfaces/mqtt/), but that was even more difficult to understand Confused

--Dan

I have developed a pretty good library if you want. It just controls one segment though, but you can adapt it a little to control more than one

Thanks, that would be great Smile Got one segment on two different ESP32s, so its just what I'm looking for.

(21.05.2024, 07:24)admin Wrote: Example for brightness control.

Attach an event script to 0..100% scale object. Change WLED_IP to your WLED device IP address.

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

url = 'http://WLED_IP/json/state'

value = event.getvalue()

data = json.encode({
  on = value > 0,
  bri = math.round(value * 2.55),
})

res, code = http.request({
  url = url,
  method = 'POST',
  headers = {
    ['content-type']  = 'application/json',
    ['content-length'] = #data,
  },
  source = ltn12.source.string(data),
})

log(res, code)

Thanks, I'll make a script and try this


RE: WLED - Andrea Becagli - 22.05.2024

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 



RE: WLED - danborge - 22.05.2024

(22.05.2024, 15:31)Andrea Becagli Wrote: 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 
Thanks. I'll test this Smile No problem with the Italian, google translate if its something I dont understand Big Grin


RE: WLED - AlexLV - 27.05.2024

Hi,

long time ago I used very interesting project with ESP8266 and ws2812 LEDs. It was Tobias Blum's MC lighting project.

Some links I found below.

https://github.com/toblum/McLighting/
https://github.com/FabLab-Luenen/McLighting

https://hackaday.io/project/122568-mc-lighting
https://www.hackster.io/news/mclighting-is-a-multi-client-neopixel-control-system-you-can-build-with-just-an-esp8266-6b1705a44de

Why I decided to mention it - you can very easy control yours WLED's over WiFi with mobile phone, and main - using your LM/SL/HL and just REST API commands - only one line string commands.

Possible to change WLED effects, colors, On/Off, Dimm etc. I also created color clock - at 7:00 all wleds are green, at night all are red etc Smile

BR,

Alexander