I’ve recently got into home automation and so thought it would be fun to integrate my smart lighting with my media centre and remote control. When I watch TV or streaming video I want the main lights to go off and the side/back lights to come on in my living room. When I pause I want the main lights to come up a little and when I play I want main lights to turn off again 🙂
I use LightwaveRF devices to control my main lights (2 dimmers) and various side-lights and screen backlights, I’ve also got a LightwaveRF Link hub which allows the Lightwave’d lights to be controlled by app, but also by simple UDP packets.
I’ve got a Logitech Harmony One remote control, because it allows you to setup custom sequences and controls my TV, DVR box and sound bar.
I decided to use a Raspberry Pi 3 (now with Wifi and Bluetooth built in!) to do the lightwave udp packets and Flirc to interpret the IR signals and convert them into simple text commands. Then I wrote a little python program that listens to the incoming commands from the Flirc USB Infrared receiver. There are other ways of interpreting the IR commands, but this was a super simple one!
I have the Raspberry Pi setup as a device in MyHarmony using Brand: Raspberry Pi and Device: Kodi because it’s a simple profile that both the Harmony One and Flirc understand. The Flirc is also loaded with the kodi profile. This is a really an easy way to send number strings to the Pi because the number buttons/commands on the remote get turned by Flirc into number key presses and the Ok button of the remote gets translated to the Enter key. Unfortunately MyHarmony doesn’t let you assign custom sequences to a device setup so I need to add my lighting sequences to my variouus Harmony activities. I’ll just run through setting up one here, but the need to do this multiple times is one of the motivators for keeping it as simple as possible.
Using my LightwaveRF app I’ve setup a few “moods”:
* TV Time (dim side and back lights on, main lights off)
* Dim room (TV Time + Main lights on low dim settings)
* All on (Main lights at full brightness and side lights on)
* All off (yep)
My python script will listen for <number keys + enter> so I just need to map numbers to whatever I want the script to do. For my setup here’s my codes, there’s no need to use single digits but it’s quicker than really long ones:
0 – Exit Script
1 – Lights: TV Time
2 – Lights: All off
3 – Lights: All on
4 – Lights: Dim room
5 – Set main lights to 5% without changing any others (for pause)
6 – Turn main lights off without changing others (for play)
7 – Lock light changes (stop video events changing lights)
8 – Unlock light changes (re-enable video events changing lights)
999 – Reboot Rasberry Pi (script auto-starts on boot – see below)
(and some others that aren’t to do with lightwave)
I can then set up screen buttons for each activity to invoke those commands. I have an activity that already turns my TV+soundbar on, selects my DVR etc. To add lighting control to it I change the start up options and also the screen options:
Create simple command sequence (this one is Lights: TV, 1 + Enter)
Put it on the screen
Also, I want the lights to be set to “TV Time” whenever the various activities are started so I can customise the startup options too:
Customise activity start
Finally I also want the main lights to dim up a little when I pause (or stop) and go off when I press play. To do that I customise the play and pause buttons for each activity to add a RPi command:
Add light control to pause button by making a custom sequence
Assign the new sequence to the correct button
Raspberry Pi Setup
As I said earlier, I’ve got Flirc setup using the Kodi profile because it’s nice and easy and the Harmony remote sends <number(s)> + <Enter>. So I wrote a little script to listen for these numbers and do the various commands, then I need the script to run interactively everytime the Pi boots up. This is my very first python program, so the style may not be perfect but I decided to use Python for educational purposes (for me and my kids).
To control LightwaveRF via UDP:
If you ask the friendly support folks at LightwaveRF they’ll send you a spreadsheet of commands, and it’s on various websites but the simplest way is to simply set up your Lightwave kit in the Lightwave app on android or iOS and then use a network sniffer like WireShark to see that happens when you press the various buttons. Oddly I’ve had trouble seeing the UDP packets that the Raspberry Pi is sending out with Wireshark, but they’re being sent because they’re controlling my lights! Either way the Lightwave commands are pretty simple text strings.
I can’t detail how to control it here because the API is subject to an NDA (I’ve no idea why, APIs should be open) but the Lightwave folks seem happy to let people use it. There’s lots of great help on the lightwaverf forums.
To send UDP commands in python 3:
LW_UDP_IP = "192.168.0.10"
LW_UDP_PORT = 9760
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # Internet and UDP
sock.sendto(msg.encode('utf-8'), (LW_UDP_IP, LW_UDP_PORT))
To listen to inputs I created a simple while loop:
aCommand = ""
while aCommand != "0":
aCommand = input("Command? ")
if aCommand == "1":
To make the script run interactively at startup:
Create a file called ir_commander.py in a sensible place: touch ~/source/python/ir_commander.py
Make sure it’ll be interpreted as a python script by making the first line: #!/usr/bin/python3
Make sure the file is executable: chmod + x ~/source/python/ir_commander.py
Add a file called ir_command.desktop to ~/.config/autostart with the following contents:
Exec=lxterminal -e "/home/pi/source/python/ir_commander.py"
The full code of ir_commander.py is on github if you want it. I’ll probably add a time element to it so that the lights don’t change during daylight hours, or maybe a light sensor to do it automatically! Also my remote can’t turn on my PS4 so I’m planning to get the Pi to turn it on over the network as part of a remote control sequence.