CEC on the Raspberry Pi


My Raspberry Pi is hooked up to my AV Receiver using the HDMI port. My AV Receiver sits behind a cabinet door, so it cannot be controlled directly using the remote unless I open the door. This is not an issue when watching TV, as TV and AVR communicate using the HDMI-CEC protocol, allowing me to control the AVR (or even the connected PlayStation 3) with the TV’s remote.

I also wanted to have this convenience when listening to music from the Raspberry Pi, i.e. at least turning the AVR on and off from the webinterface of Pi Musicbox. Luckily, there’s a library called libcec that lets you do this. Unfortunately, I could not get it compiled on Pi Musicbox, so I had to start off from a clean installation of Raspbian Jessie. These are the steps I followed:

  • Download, installation and configuration of Raspbian Jessie.
  • Download, compilation and installation of libcec, I simply followed the instructions.
  • You can then test CEC using cec-client. There’s plenty of information online on how to use it. On my system, echo “on 5” | cec-client -s turns on the AVR.
  • Install and configure Mopidy and the Musicbox webclient, following the instructions. I could not get avahi/bonjour to work with my Windows computer, but at least my iOs devices can find the web interface at http://raspberrypi.local:6680.

So far so good, but how to call cec-client from the web interface? This would be easily done with PHP, but it took me a little longer to figure out. Here are the steps I undertook (once again not a full recipe):

  • For use with Pi Musicbox, the Musicbox webclient has a menu item System that is not shown on regular Mopidy installations. This links to a page system.html that has the links for shutdown and reboot of the system. I added a menu item CEC to the navigation menu (in /usr/local/lib/python2.7/dist-packages/mopidy_musicbox_webclient/static), copied system.html to cec.html, and edited this file. You can see that it calls two functions that are defined in js/controls.js. I created two new function calls and added their definition in controls.js, linking to /settings/avron and /settings/avroff instead of the shutdown/reboot links given there.
  • These links only work if the Websettings extension is installed. How they are handled is defined in the __init__.py file that is contained in  the websettings directory. I added new functions for the avron and avroff links.
  • The system calls need to be changed to what you want to do, e.g. os.system(‘echo “standby 5” | /usr/local/bin/cec-client -s’).
  • In order for this to work, user mopidy needs to be added to the audio and video user groups.

You probably want to run multiple commands when turning on the receiver – also switching it to the Raspberry port, and maybe turning the tv off (on my system, it is turned on automatically by the AVR when I switch to the Raspberry port. This can be done via

subprocess.call('echo "on 5" | /usr/local/bin/cec-client',shell=True)
time.sleep(5)
subprocess.call('echo "as" | /usr/local/bin/cec-client',shell=True)
time.sleep(5)
subprocess.call('echo "standby 0" | /usr/local/bin/cec-client',shell=True)

You might have to do a  python -m compileall . after changes to the Python script. service mopidy restart will then apply changes. Good luck!

Leave a comment

Your email address will not be published.