I recently bought some cheap LED backlights to replace the old one from my screen. My old one had a simple switch to just turn it on and off. All the new ones I could find seem to be multicoloured and remote control. I ended up buying some made by Nexillumi for £10 on Amazon which were bluetooth controlled. The Android app for them is called HappyLighting, and it does work but takes a while to connect to the lights when you start it up. It’s also a bit annoying having to start the app up just to turn the light on or off.
After a bit of searching I found that these Happylighting LEDs are also known as Triones, and there is a Python module called trionesControl which can control them. So the first thing to do was install this module and it’s requirements with pip. I am using pip but you may have to use pip3 if your system still has python2 as the default.
~$ sudo pip install trionesControl
The trionesControl module needs to know the MAC address of your lights. To find this you can use hcitool to scan for BLE devices. Your Linux system will need a working Bluetooth V4 or newer deveice which supports BLE. You will probably need to run this as root or with sudo
~$ sudo hcitool lescan
When starting the scan you should see a list of all the BLE devices in range, and can press CTRL-C to stop it scanning. The output should look something like this:
51:E9:E4:DD:94:E7 (unknown) 51:E9:E4:DD:94:E7 (unknown) 0F:1A:03:00:02:2B QHM-022B 00:7C:2D:77:FE:89 (unknown) 0F:1A:03:00:02:2B QHM-022B 6E:16:2A:1F:F4:FA (unknown) 6E:16:2A:1F:F4:FA (unknown) 51:E9:E4:DD:94:E7 (unknown) 0F:1A:03:00:02:2B QHM-022B 0F:1A:03:00:02:2B (unknown) 0F:1A:03:00:02:2B QHM-022B 0F:1A:03:00:02:2B QHM-022B 0F:1A:03:00:02:2B QHM-022B
My backlight is the one shown as QHM-022B with the MAC address 0F:1A:03:00:02:2B. So now I can make a python script called ledstrip.py that will connect to that MAC address and turn the light on or off:
#!/usr/bin/python3 import sys import trionesControl.trionesControl as tc # Get MAC address with hcitool -i hci0 lescan # Connect to the LED strip device = tc.connect('0F:1A:03:00:02:2B') # Read the hex color code from the command line, eg ff0000, eeaa99 # also accept "off" at the command line to turn the light off if(sys.argv != "off"): print("Setting colour to " + sys.argv) # Convert it to R G B values col1 = tuple(int(sys.argv[i:i+2], 16) for i in (0, 2, 4)) R = col1 G = col1 B = col1 # turn on the light tc.powerOn(device) # set the color tc.setRGB(R, G, B, device) else: # turn off the light tc.powerOff(device) # finished tc.disconnect(device)
This script can now be used to control the light.
~$ python3 ./ledstrip.py ff0000 # RED ~$ python3 ./ledstrip.py 0000ff # BLUE ~$ python3 ./ledstrip.py 00ff00 # GREEN ~$ python3 ./ledstrip.py fdf4dc # Warm White, apparently ~$ python3 ./ledstrip.py off # Off
I have found that with some USB bluetooth devices you need to try a few times to get it to connect to the backlight. I also ran into a problem when running the script from cron where the pygatt module would cause gatttool to get stuck running at 100% CPU in the background and never exit, so I needed to add a killall gatttool to my crontab.