{"id":1884,"date":"2016-02-14T11:14:54","date_gmt":"2016-02-14T09:14:54","guid":{"rendered":"http:\/\/www.wittwer.nl\/?p=1884"},"modified":"2016-02-15T12:36:00","modified_gmt":"2016-02-15T10:36:00","slug":"cec-on-the-raspberry-pi","status":"publish","type":"post","link":"https:\/\/www.wittwer.nl\/?p=1884","title":{"rendered":"CEC on the Raspberry Pi"},"content":{"rendered":"<p>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&#8217;s remote.<\/p>\n<p>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 <a href=\"http:\/\/www.wittwer.nl\/?p=1878\">Pi Musicbox<\/a>. Luckily, there&#8217;s a library called <a href=\"https:\/\/github.com\/Pulse-Eight\/libcec\">libcec<\/a> 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:<\/p>\n<ul>\n<li>Download, installation and configuration of <a href=\"https:\/\/www.raspberrypi.org\/downloads\/raspbian\/\">Raspbian Jessie<\/a>.<\/li>\n<li>Download, compilation and installation of <a href=\"https:\/\/github.com\/Pulse-Eight\/libcec\">libcec<\/a>, I simply followed the instructions.<\/li>\n<li>You can then test CEC using <em>cec-client<\/em>. There&#8217;s plenty of information online on how to use it. On my system, <em>echo &#8220;on 5&#8221; | cec-client -s <\/em> turns on the AVR.<\/li>\n<li>Install and configure <a href=\"https:\/\/www.mopidy.com\/\">Mopidy<\/a> 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 <em>http:\/\/raspberrypi.local:6680.<\/em><\/li>\n<\/ul>\n<p>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):<\/p>\n<ul>\n<li>For use with Pi Musicbox, the Musicbox webclient has a menu item <em>System<\/em> that is not shown on regular Mopidy installations. This links to a page <em>system.html<\/em> that has the links for shutdown and reboot of the system. I added a menu item <em>CEC <\/em> 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.<\/li>\n<li>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\u00a0 the websettings directory. I added new functions for the avron and avroff links.<\/li>\n<li>The system calls need to be changed to what you want to do, e.g. <em>os.system(&#8216;echo &#8220;standby 5&#8221; | \/usr\/local\/bin\/cec-client -s&#8217;).<\/em><\/li>\n<li>In order for this to work, user mopidy needs to be added to the audio and video user groups.<\/li>\n<\/ul>\n<p>You probably want to run multiple commands when turning on the receiver &#8211; 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<br \/>\n<code><br \/>\nsubprocess.call('echo \"on 5\" | \/usr\/local\/bin\/cec-client',shell=True)<br \/>\ntime.sleep(5)<br \/>\nsubprocess.call('echo \"as\" | \/usr\/local\/bin\/cec-client',shell=True)<br \/>\ntime.sleep(5)<br \/>\nsubprocess.call('echo \"standby 0\" | \/usr\/local\/bin\/cec-client',shell=True)<br \/>\n<\/code><\/p>\n<p>You might have to do a\u00a0 <em>python -m compileall .<\/em> after changes to the Python script. <em>service mopidy restart<\/em> will then apply changes. Good luck!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1885,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[78],"tags":[87],"class_list":["post-1884","post","type-post","status-publish","format-standard","has-post-thumbnail","category-tech","tag-raspberry-pi","czr-hentry"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/www.wittwer.nl\/wp-content\/uploads\/2016\/02\/Raspi-PGB001.png","jetpack_shortlink":"https:\/\/wp.me\/plKIk-uo","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=\/wp\/v2\/posts\/1884","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1884"}],"version-history":[{"count":7,"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=\/wp\/v2\/posts\/1884\/revisions"}],"predecessor-version":[{"id":1892,"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=\/wp\/v2\/posts\/1884\/revisions\/1892"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=\/wp\/v2\/media\/1885"}],"wp:attachment":[{"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1884"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1884"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wittwer.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1884"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}