I got tired of keeping a second remote control out just to turn my TV on and off, so I went in search of a way to get the Pi to do it for me. Here’s what I came up with. This should work with any IR device. This should work with any TV, but if your TV is CEC-capable, I think there’s much easier ways to do this.
LEARNING THE CODES
Stop lirc and record the keys from the remote you’d like to imitate:
sudo systemctl stop lircd_helper@lirc0
irrecord -d /dev/lirc0 ~/newlircd.conf
Follow the instructions in irrecord carefully and it will create a new file named newlircd.conf with the keys you just learned with it. You may want to use LIRC Key Names.
Here’s what my newlircd.conf came out looking like, I only learned the one key:
begin remote
name /home/osmc/newlircd.conf
bits 16
flags SPACE_ENC|NO_HEAD_REP|CONST_LENGTH
eps 30
aeps 100
header 8464 4223
one 549 1564
zero 549 506
ptrail 549
gap 46191
min_repeat 2
# suppress_repeat 2
# uncomment to suppress unwanted repeats
toggle_bit_mask 0x0
begin codes
KEY_POWER2 0xC0E8
end codes
end remote
I renamed the remote from "name /home/osmc/newlircd.conf"
to "name JVC"
to make it friendlier and added my existing remote to the newlircd.conf:
begin remote
name XBOX-ONE
bits 16
flags SPACE_ENC|CONST_LENGTH
eps 30
aeps 100
header 9061 4460
one 596 1662
zero 596 527
ptrail 582
repeat 9032 2232
pre_data_bits 16
pre_data 0x11B
gap 107260
toggle_bit_mask 0x0
begin codes
KEY_HOME 0x26D9
KEY_ZOOM 0x7689
KEY_TITLE 0xF609
KEY_UP 0x7887
KEY_DOWN 0xF807
KEY_LEFT 0x04FB
KEY_RIGHT 0x847B
KEY_OK 0x44BB
KEY_BACK 0xC43B
KEY_INFO 0x649B
KEY_VOLUMEUP 0x08F7
KEY_VOLUMEDOWN 0x8877
KEY_MUTE 0x708F
KEY_CHANNELUP 0x48B7
KEY_CHANNELDOWN 0xC837
KEY_REWIND 0xA857
KEY_FASTFORWARD 0x28D7
KEY_PLAY 0x0EF1
KEY_PREVIOUS 0xD827
KEY_NEXT 0x58A7
KEY_STOP 0x9867
end codes
end remote
begin remote
name JVC
bits 16
flags SPACE_ENC|NO_HEAD_REP|CONST_LENGTH
eps 30
aeps 100
header 8464 4223
one 549 1564
zero 549 506
ptrail 549
gap 46191
min_repeat 2
# suppress_repeat 2
# uncomment to suppress unwanted repeats
toggle_bit_mask 0x0
begin codes
KEY_POWER2 0xC0E8
end codes
end remote
Start lirc
back up:
sudo systemctl start lircd_helper@lirc0
Now, you can go into the remote section in My OSMC and browse to newlircd.conf in your Home folder to start using it.
If you like you can fire up irw
to see if the keys you just learned are being recognized. Start irw
and press a key:
osmc@osmc:~$ irw
164 0 KEY_POWER2 linux-input-layer
164 1 KEY_POWER2 linux-input-layer
164 0 KEY_POWER2_UP linux-input-layer
SENDING THE CODES
For this part you’ll need a IR LED hooked up to the GPIO pins on your Pi. I used GPIO17(Pin 11) and the Ground(Pin 9) right next to it. http://www.raspberrypi-spy.co.uk/wp-content/uploads/2012/06/Raspberry-Pi-GPIO-Layout-Model-B-Plus-rotated-2700x900.png
To send the codes I’m using irsend
, but I have to specify the device I want to use because OSMC doesn’t use the default:
osmc@osmc:~$ irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE JVC KEY_POWER2
You’ll need to edit JVC
to whatever you named your remote and KEY_POWER2
to whatever you named the key you want to send.
Next we have to decide how we want to send that key code to our device. I used a key on my main remote to trigger it. We’re going to use irexec
to do it, so we need to make a quick config file for it.
sudo nano ~/.lircrc
and tell it what you want to do
begin
prog = irexec
button = KEY_HOME
config = irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE JVC KEY_POWER2
end
Any time I press KEY_HOME
on my main remote while irexec
is running, it will issue the command to send the power code to my TV.
Lastly, we need to auto-start irexec
. I use cron
to do this. Edit yours with crontab -e
and add this line to auto-start irexec
:
* * * * * ps aux|grep -v grep|grep -q irexec || /usr/bin/irexec -d &
As long as it’s running it will watch for you to press the key on your main remote and issue the command to send out an IR code when you do. I’ve had problems with irexec either crashing or closing unexpectedly since I started using it. The above updated line checks if irexec is running and starts it if it’s not.