Controlling lights with Bash

So lets say you have an extremely small Linux (or maybe OSX) system and you would like to control your LIFX bulbs using it. This method is not very flexible, and does not attempt to confirm its commands worked so don’t use it when you need reliability. You will also need to give your lights static IP addresses in your DHCP server if you want to talk to them directly.

All you need for this method is bash, however I’ll show examples for using netcat and socat at the end (because they’re a little more powerful).

Step 1 - Make some packets

Using the LAN Documentation you build the packets that you wish to send to the bulbs and save them in files. Save them in hex format with \x between every byte. This way we can use the /bin/echo command to convert them to binary. Below are two examples, using the documentation you could create many more. In these examples we will save these files to /etc/lifx/ but you can save them anywhere you like.

All On - allon.echo

\x2a\x00\x00\x34\xb4\x3c\xf0\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x75\x00\x00\x00\xff\xff\xe8\x03\x00\x00

All Off - alloff.echo

\x2a\x00\x00\x34\xb4\x3c\xf0\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x75\x00\x00\x00\x00\x00\xe8\x03\x00\x00

Step 2 - Test the commands

This command uses the /bin/echo command to create binary versions of our packets, then sends it to the bulbs IP address on the LIFX port. As a result you should see your bulb turn off. You should be able to use other packets to perform other actions.

/bin/echo -e "$(cat /etc/lifx/alloff.echo)" > /dev/udp/172.16.1.4/56700

Step 3 - Add aliases

Obviously these commands are annoying to type so to make it easier you can make an alias for them. Add this line to the end of your ~/.bashrc. You can make several commands for different packets you may wish to send.

alias lifx-off='/bin/echo -e "$(cat /etc/lifx/alloff.echo)" > /dev/udp/172.16.1.4/56700'

Appendix - Multiple lights

Instead of using the bash builtin for sending packets you can use netcat or socat to send commands as a broadcast to multiple lights at a time. Below are examples of both broadcasting to an entire subnet.

Netcat

/bin/echo -e "$(cat /etc/lifx/alloff.echo)" | nc -u 172.16.1.255 56700

Socat

/bin/echo -e "$(cat /etc/lifx/alloff.echo)" | socat - udp-datagram:172.16.1.255:56700,broadcast
1 Like

Hello,

Thanks for the example !

But i now feel like a noob as i’m not able to figure out how to set color for example…

I don’t even get how to read the LAN documentation and reproduce the Hex code given.

Could you elaborate more on this specific part ?

My ultimate goal is to have a python script with arguments to set on/off and colors of my bulbs (via the LAN protocol and cloud free).

Thanks !

I’ve had a few requests for this, so I’ll follow up later today with a thread specifically about that. It gets into some light computer science subjects like Endianness and Bitwise Operations.

In a Python script you would generally want to write Python code to build the packet for you rather than manually building the hex data and putting that into the script.

I tried this but couldn’t get it to work. I have all the lights on, freshly power-cycled. I can ping them,see them on the router and even control them via the iOS app. I made an alloff.echo file like you did and ran the command in step 2 and when that didn’t work, I embedded it in the command itself. This should work right?

/bin/echo -e \x2a\x00\x00\x34\xb4\x3c\xf0\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x75\x00\x00\x00\x00\x00\xe8\x03\x00\x00 > /dev/udp/192.168.1.56/56700

I even tried “” around the packet. Do you know what could be wrong? Is it a copy and paste thing? I copied the packet from the browser onto the terminal window on my Mac.

On OSX /bin/bash is a BSD version of bash. Drop the -e argument and just use the shell builtin echo instead.

Let me know how that goes. I don’t have a mac with me to test with.

Thank you for writing back Daniel. Unfortunately that didn’t work either. I tried multiple approaches. see logs below:

$ cat /etc/lifx/alloff.echo
\x2a\x00\x00\x34\xb4\x3c\xf0\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x75\x00\x00\x00\x00\x00\xe8\x03\x00\x00
$ /bin/echo “\x2a\x00\x00\x34\xb4\x3c\xf0\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x0b\x00\x00\x00\x00\x00\x00\x00\x00\x75\x00\x00\x00\x00\x00\xe8\x03\x00\x00” > /dev/udp/192.168.1.56/56700
$ /bin/echo “$(cat /etc/lifx/alloff.echo)” | nc -u 192.168.1.255 56700
$ /bin/echo “$(cat /etc/lifx/alloff.echo)” > /dev/udp/192.178.1.56/56700

No luck on the MAC.
(Incidentally, I tried it on an ARM based microcontroller ssh session and it worked!)

I also tried the nc method and I get a permission denied error despite running as root:

root# /bin/echo -e “$(cat ./alloff.echo)” | nc -u 192.168.1.255 56700
write(net): Permission denied

Instead of /bin/echo just use echo.

Though I’m not sure what the permission error is about. I’ll borrow someone’s Mac on Monday and see if I can get it working.

echo still gave the permission denied error. Just to clarify, the nc commands were done on the ARM based microprocessor. I did get the socat to work on it though.

Second this! It would be very helpful to provide a few more examples of the packets… The documentation is great but I usually only have a few minutes after work to tinker with my lights so cannot justify the time to build a proper packet generator :slight_smile:

The Building a LIFX Packet post includes instructions that can be used to build any packet you like.

Thank you very much! Did not see that post before!

Feel free to use this as a starting point! http://petr.io/2015/09/16/very-simple-lifx-bulb-control/ or directly GitHub - petrklus/lifx-simple: Controlling LiFX bulbs using command line. Pure Python 2.7

1 Like

update
I was able to get a script to work on my iPhone terminal using this syntax:

allon.sh:

#!/bin/bash
/bin/echo -e “$(cat /frak/allon.echo)” | socat - udp-datagram:255.255.255.255:56700,broadcast

allon.echo:

\x2a\x00\x00\x34\xb4\x3c\xf0\x84\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\x0d\x00\x00\x00\x00\x00\x00\x00\x00\x75\x00\x00\x00\xff\xff\xe8\x03\x00\x00

1 Like

@daniel_hall
I have confirmed that the Gen 3 bulbs do not respond to the bash command above to turn the lights on and off.
Out of a total of 9 LIFFX bulbs, all six older bulbs turnoff/on and the three Gen 3 bulbs refuse to turn on/off when I use the bash command above.
Can you please explain why? The firmware onthe new LIFX bulbs appears to reject these broadcast packets.The firmware on the older bulbs do not.
This inconsistent behaviour really messes up the scripts that I have written to control all my lights.

1 Like

I’d really like to see an answer here, as I’m on the verge of buying new bulbs and make heavy use of bash scripting like this!

Hello,

@mattfarley you might wanna checkout lightsd: https://docs.lightsd.io/.

It has a small interface for shell scripting, e.g:

#!/bin/sh

. $(lightsd --prefix)/share/lightsd/lightsc.sh

lightsc power_toggle ${*:-'"*"'}
1 Like

Hi, followed all the back and forth and still hadnt been able to get anything to work… are there any developments with this?