< async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"">

Thursday, December 12, 2013

Infrared remote controlled (TV remote) bash script on raspberry pi (or raspbmc) with irexec

ADVERTISEMENT
After my post on using any TV remote to control raspberry pi based xbmc (raspbmc), i made this post to complete the purpose of the previous post; where one of the aim was to execute a command/script to restart the xbmc at the click of remote control. In this way the xbmc can be forcefully restarted (for instance when xbmc freezes and become unresponsive to http/android/iphone based remote controls) .

To achieve this irexec program is used which can be run as a demon. It looks staraight forward but in my case , irexec fails to start on system reboots. So it needs to be added to the startup and can be done by upstart in newer debian distros.

 sudo nano /etc/init/irexec.conf  

And then add the following lines there

 #<---start----------->  
 # irexec Daemon  
 #  
 description     "irexec daemon"  
 author      "None"  
 start on (started eventlircd)  
 stop on (xbmc-do-stop or runlevel [!2345])  
 exec irexec /home/pi/.lircrc  
 respawn  
 #<---end----------->  

This will keep irexec running. But wait a second, before this we need to tell irexec, what to do. So edit its configuration file

 nano /home/pi/.lircrc  

and add the following lines (you can change the scripts based on this example to what ever you need)

 #<---start here----------->  
 begin  
  prog = irexec  
 # remote = mceusbou   
  button = KEY_BLUE  
  config = /home/pi/rebootxbmc.sh  
 end  
 begin  
  prog = irexec  
 # remote = mceusbou  
  button = KEY_MENU  
  config = /home/pi/auto2.sh  
 end  
 #<---ends here----------->  

Note that config= followes your script to get executed and button should be the one you need to map

For instance below is an example script (rebootxbmc.sh) which helps to reboot the xbmc and contains the following lines (make sure you chmod +x rebootxbmc.sh)

 #!/bin/bash   
 # Kill XBMC and restart   
 echo "XBMC running, force restart."   
 sudo -u root initctl stop xbmc   
 #Optional logging for troubleshooting. Remove the hash to enable  
 #echo $(date) ": XBMC-live stop executed" >> /home/xbmc/.restart-log.log   
 sleep 2   
 sudo -u root initctl start xbmc   
 #Optional logging for troubleshooting. Remove the hash to enable  
 echo $(date) ": XBMC-live RESTART executed" >> /home/pi/rebootxbmc.log   
 exit  

But a  before it does its job we need to do something more! As xbmc restart needs sudo (super user), we need to set this script to run with higher privilege.

 sudo visudo  

and append following line to the end of the file (remember end of the file! don't look at the stuff above :)

 pi ALL=(ALL) NOPASSWD:/home/pi/rebootxbmc.sh  

where pi is the username.


Shutting down the Monitor/LCD  by turning the hdmi off in raspberry pi

This can be achieved by mapping the following script to the remote buttons.

So add the following to  

 nano /home/pi/.lircrc  
begin
 prog = irexec
# remote = mceusbou
 button = KEY_M
 config = /home/pi/off.sh
end
begin
 prog = irexec
# remote = mceusbou
 button = KEY_LANGUAGE
 config = /home/pi/on.sh


and then we need the on.sh (/home/pi/on.sh)  and off.sh (/home/pi/off.sh) 

on.sh


#!/bin/bash
/opt/vc/bin/tvservice -p
sudo initctl start xbmc

off.sh 


#!/bin/bash
sudo initctl stop xbmc
/opt/vc/bin/tvservice -o

Single button to toggle on /off using IR remote

Instead of using seperate buttons to turn on or off, it is possible to use the following script which basically toggle between on and off positions.

#!/bin/bash
if [ `sudo /sbin/initctl status xbmc | grep running | wc -l` = 1 ]
then
sudo initctl stop xbmc
/opt/vc/bin/tvservice -o
echo $(date -u) "Off" >> /home/pi/tvlog.txt
else
/opt/vc/bin/tvservice -p
sudo initctl start xbmc
echo $(date -u) "On" >> /home/pi/tvlog.txt
fi


end

No comments:

Post a Comment