raspberrypi

Control your Raspberry Pi with your TV remote

 

This little script will allow you to control your Raspberry Pi using the buttons in your regular TV remote.

Usage

You can press the following buttons on the remote.

  • OK       – launch kodi
  • up       – launch browser
  • down  – change wallpaper (this is a custom script of mine)
  • left      – suspend your desktop computer
  • back    – halt Raspberry Pi
  • stop     – enter “mouse mode”, to move the mouse with the arrows. OK to double click
  • play     – exit “mouse mode”

These bindings are set as an example. Each person should change them to suit their own needs.

Instalation

First, follow the instructions to compile and install libcec. More details below.

Then, get the code from my gist, and create an executable script

You can run picec.sh  from whatever “startup programs” configuration your desktop provides. In my case, I just add the following line at the end of /etc/rc.local

Optionally, you can install xdotool  for moving the mouse, and osd_cat  for showing information in a TV OSD fashion. More details below.

 Details

As a means to control our RPi, we will use the CEC (Consumer Electronics Control) feature of HDMI, which is a standard protocol to send commands back and forth through HDMI.

CEC allows consumer devices to control each other with or without user intervention. The GPU of the Raspberry Pi already supports the CEC protocol, as most modern TVs do. There is a compatibility list here.

An often missed requirement is that your HDMI cable must support CEC as well. Most cables do, but some of the cheapest ones cannot talk CEC, so be careful with this.

In order to speak to our TV using the CEC standard, we make use of the libcec library by Pulse Eight. This library is already included in Kodi out of the box, and it is the reason we can readily control our media center with our remote.

From their website, we can follow the instructions to compile and install libcec on the Raspberry Pi.

After this, we obtain the library, some python bindings and a wrapper utility in C called cec-client .

We can now list connected devices,

, turn the TV on and off from SSH,

, and even configure the TV. To see all available commands:

As a quick and dirty way of playing with this, I wrote this little script that just parses the output of the cec-client utility. I figured it would be faster to implement than using the C library and end up calling system()  or popen() anyway.

There is lots of room for improvement, but I hope this can give you ideas to get started.

github

The script makes use of osd_cat for the visual feedback, and xdotool  for the mouse control functionality. It is recommended to install them with apt, but it’s not required because the script  will detect wether they are available or not.

As you can see on this list, each manufacturer supports the protocol to a varying degree, so you will have to play a little bit to see what button you can detect, and wether you can get the duration of the button press. For instance, on my Philips remote, I do not have access to the numbers.

Moving the mouse is not really very useful, as the latency is terrible. However, there are other cool things that you might want to associate to your remote using xdtool, for example you can read PDFs or websites binding

, or even bind to <CTRL><TAB>  to navigate through browser tabs.

Author: nachoparker

Humbly sharing things that I find useful
[ github dockerhub ]

2 Comments on “Control your Raspberry Pi with your TV remote

  1. I see you own a similar remote to mine. Might be that you are using a Phillips TV. I have one and CEC doesn’t seem to work. EasyLink is enabled, but it does not respond to the echo "on 0000" | cec-client -d 1 -s "standby 0" RPI I send when the Pi boots. That should power up the TV

Leave a Reply

Your email address will not be published. Required fields are marked *