In Raspberry Pi as a Television Remote: Part 1, we covered how to put together the hardware for this project, including a Raspberry Pi protoboard and LED emitters/receivers. Now, we’re going to put them to use.
As in the previous part, I’ll be covering how my setup differs from Alex Bain’s Open Source Universal Remote Project, and again, a lot of credit goes to him for pioneering this.
The first (and perhaps biggest) recommendation I’ll make is the choice of operating system for your Raspberry Pi. Rather than using Raspbian, I recommend OSMC for this project. Since your pi is going to be near your TV anyway, you might as well take advantage of the super cool features that come with a pi media player.
LIRC stands for Linux Infrared Remote Control, and it’s the package that will allow us to interface with our LEDs without directly controlling the hardware. One of the nice things about OSMC is that it comes with LIRC installed, and many common remote control types pre-configured.
The /etc/modules file determines which GPIO pins LIRC uses to communicate with your LEDs. You have two choices here. If you didn’t solder the control wires directly into the GPIO pins, you can move them to whatever pins are listed in the file. Alternatively, you can change what the file says to whatever pins you setup your proto board to use. I used pins 22 and 23, so I added this to my file:
lirc_dev lirc_rpi gpio_in_pin=23 gpio_out_pin=22
Next, you’ll want to add this to your /boot/config.txt file and then reboot:
Thanks again to Alex Bain and Marc W. for this part of the configuration.
For those of you who are following along with Alex Bain’s post as well, you may notice that I’m not having you mess with the /etc/lirc/hardware.conf file. This is because, if you used OSMC, that’s already set up for you. If you didn’t use OSMC, then follow Alex Bain’s guide here.
Setting Up the Remote
Before continuing, take a look at the folder /etc/lirc/ and see if there’s a remote configuration that matches your TV already. If so, all you should need to do is copy over the file that matches your remote to /etc/lirc/lircd.conf — I couldn’t do that myself, because my TV is a rarer brand, so I can’t say for sure if this will work. If it does, great! You can skip to the next section. If you don’t see your remote, or copying over doesn’t work for you, you’ll need to setup the remote yourself.
To do so, you’ll first want to stop LIRC with this command:
sudo /etc/init.d/lirc stop
and then print out a list of all the possible keys are for a remote (since when creating the config file you’ll need to name the keys as you press them) using this command:
Finally, execute this command to go through the process of creating a configuration file:
irrecord -d /dev/lirc0 ~/lircd.conf
SUPER IMPORTANT: read ALL the instructions carefully! That last command gives you a wall of text, but take the time to go through it. I had to do this step like 5 times because I didn’t read carefully all the way through.
After you’ve generated the configuration file, move it to /etc/lirc and start LIRC again:
sudo cp ~/lircd.conf /etc/lirc/lircd.conf sudo /etc/init.d/lirc start
Now, try it out! OSMC uses a slightly different command format than that given by Alex Bain, so I’ll an example here. The name of my remote is ‘ocosmo’ after the brand of TV I have — you’ll want to switch that out for whatever you named your remote:
irsend -d /var/run/lirc/lircd-lirc0 SEND_ONCE "ocosmo" "KEY_POWER"
This command should send a power signal to your TV. If you pointed the LEDs at your TV, it should have turned on or off. If it did, you’re all done with this part!
Now, you may be concerned that you’ll have to ssh into your pi and type out that super clumsy command every time you want to turn your TV on — not a fun experience. Lucky for you, the next part will cover remotely controlling your remote control! I’ll take you through building an app that can communicate with your pi, and configure your pi for that communication. This app is setup so that you can build other pi projects for other things in your house, and the app will be able to interface with them too. Or, if you’re not interested in polymorphic app interfaces and just want to be able to control your TV from the web, check out Alex Bain’s lirc_web.