Controlling LIFX bulbs

Discussion in 'C-Bus Automation Controllers' started by Ashley, Aug 5, 2020.

  1. Ashley

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,125
    Likes Received:
    110
    Location:
    Adelaide, Australia
    I think we need a place to post SHAC lua code (like we have for pascal code)

    The following functions allow you to turn lifx bulbs on and off and set their colour.
    The functions are:
    lifx_pwron(ip) -- turn the bulb on where ip is ip address string (e.g. '192.168.1.65)
    lifx_pwroff(ip) -- turn bulb off
    lifx_hsvk(ip, hue, saturation, intensity, kelvin, time) -- Set colour where hue is 0-360, saturation and intensity is 0-100,
    kelvin in degrees and time in seconds.

    Create a new user library called lifx, set it to auto load, and add in the following code:

    Code:
    -- Basic library to control (test!) LIFX bulbs
    -- Local functions
    
    lifx_pwroncmd = string.char(0x2A, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00)
    
    lifx_pwroffcmd = string.char(0x2A, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x75, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00)
    
    lifx_colcmd = string.char(0x31, 0x00, 0x00, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
      0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x66, 0x00, 0x00, 0x00, 0x00)
    
    function lifx_send(ip, cmd)
      require('socket').udp():sendto(cmd, ip, 56700)
    end
    
    function Lifx16toStr(value)
      return string.char(value % 256, value / 256 % 256)
    end
    
    function Lifx32toStr(value)
      return string.char(value % 256, value / 256 % 256, value / 65536, value / 16777216 % 256)
    end
    -- End local functions
    
    ---------------------
    -- Start user functions
    
    -- turn on light
    function lifx_pwron(ip)
      alert('power on')
      lifx_send(ip, lifx_pwroncmd)
    end
    
    -- Turn off light
    function lifx_pwroff(ip)
      lifx_send(ip, lifx_pwroffcmd)
    end
    
    --[[
    Set colour of lifx bulb
    ip = ip address eg '192.168.1.65'
    hue is colour in degrees from 0-360
    sat is saturation from 0% (white) to 100%
    val is intensity from 0% to 100%
    kelvin is colour temperature
    secs is time to perform change over in seconds
    
    Notes
      All values are floating point (e.g. you can have 1.5 seconds etc)
      Doesn't turn on light if it's off. Just sets colour for next time it's turned on.
    --]]
    function lifx_hsvk(ip, hue, sat, val, kelvin, secs)
          local cmd = lifx_colcmd .. Lifx16toStr(hue / 360 * 65535) .. Lifx16toStr(sat / 100 * 65535) .. Lifx16toStr(val / 100 * 65535) .. Lifx16toStr(kelvin) ..
      Lifx32toStr(secs * 1000)
      lifx_send(ip, cmd)
    end
    To test it you can use the following event based script:

    Code:
    ip = '192.168.1.65' -- change to the ip of your lifx bulb!
    
    local level = event.getvalue()
    
    if level == 0 then
      lifx_pwroff(ip)
    elseif level == 255 then
      lifx_pwron(ip)
    elseif level == 1 then -- Just to set colour setting
      lifx_hsvk(ip, 120, 100, 50, 3500, 4)
    end
     
    Ashley, Aug 5, 2020
    #1
    Mr Mark likes this.
    1. Advertisements

  2. Ashley

    Ashley

    Joined:
    Dec 1, 2005
    Messages:
    1,125
    Likes Received:
    110
    Location:
    Adelaide, Australia
    Oops, There's a typo in the lifx_pwroffcmd string. The 3rd last byte should be a 0x00, not an 0x20.

    Shame you can't edit posts like in the old system.
     
    Ashley, Aug 5, 2020
    #2
    philthedill likes this.
    1. Advertisements

  3. Ashley

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    63
    Likes Received:
    0
    Location:
    Melbourne
    Great work Ashley - this is a great way to do it and makes the configuration for individual lamps quite straightforward.
    I got it working on a test lamp ahead of commissioning the rest
     
    philthedill, Aug 7, 2020
    #3
  4. Ashley

    philthedill

    Joined:
    Mar 21, 2010
    Messages:
    63
    Likes Received:
    0
    Location:
    Melbourne
    I now have my 5 lamps/strips working and am using only the hsvk section of the set up proposed by Ashley. I've set up different settings in a series of if..then statements that get selected depending on the event value. so far so good!
     
    philthedill, Aug 8, 2020
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.