Sunday, October 18, 2015

Make use of your Banana PI IR receiver with python!

The goal

Control MPD running on Banana Pi with IR remote form your stereo set.

Usability

Improve your lazyness ;)

Parts

  • Banana Pi
  • IR remote (any ?)

Code

Hardware

  • Banana Pi

Description


Recently I was wondering if there is some better way to control internet radio running on MPD, than Android phone with MPC client. So I've remembered that my Banana Pi has an IR receiver, nice! There was a easy answer - write a C program that will ... but wait Is it there a simpler solution for this?

In the past I've not used languages other than C/C++, but during my work in several companies I've learned Perl, now I'm starting to learn Python so I thought that is a good way to do this. Two hits in a row - slightly improve python skills and make something useful.

Since IR receiver can be accessed via evdev it was a matter of minutes to write a python script that was actually work :)

Ok, lets get back to my setup. I've got Yamaha CRC-040 which has a line input marked as PORTABLE. In this mode music is played from front line in jack input.

My Yamaha CRX-040 with Banana Pi on it



My remote looks like this:

Yamaha CRX-040 remote

PORTABLE mode disables the CD buttons, like PLAY, PREV, NEXT, STOP etc. so I could use them to control internet radio running on Banana Pi.


Banana Pi is in a metal net to prevent my cat to eat it :D Since there is a SATA port, you can see 2.5 sata drive too.



Banana Pi front view in metal (anti-cat) case

Additionally I've bind POWER button (IR remote) to send 'halt' system command so I can power down Banana Pi and my Yamaha at the same time. When I put my stereo in PORTABLE mode, MPC send's info to MPD to start playing.

Python code you can find here.

You could run it like a deamon from /etc/rc.local like this:
nohup /usr/bin/ir.py >> /var/log/ir.log&
or for calibrating/debug, from console:
root@bananapi:~# /usr/bin/ir.py
Action for 170 code is not defined
Action for 169 code is not defined
Action for 28 code is not defined
Now all you have to do is to add codes above to function dictionary, create proper function to handle this code and that's it!
def my_func_1():
   print "Hey this is my function 1!"

fun_tbl = { 
  1   : MPD_stop,
  2   : MPD_pause,
  3   : MPD_next,
  4   : MPD_prev,
  15  : SYS_halt,
  223 : MPD_play,

  170 : my_func_1,
}
Simple, right?

1 comment:

  1. hey men do you the ir codes from your remote control?

    ReplyDelete