Sunday, November 10, 2013

Control XBMC from the Raspberry PI GPIO's

I have recently worked on adding external controls for my Raspberry PI CarPC project because while driving it is better to have some physical controls to rely on, rather than looking at the touch screen to find the buttons.
This post provide information on how to set up buttons or rotary encoders to control XBMC from Raspberry PI.

Rotary encoder
Rotary encoders are very cheap and very nice controls(eBay link). You can find them in car stereos for volume control. You can also use them to browse through menu items or to skip to next song etc.
They are looking very similar to a potentiometer, but there are major differences: they can be turned in both directions with infinite steps(you know just the direction of the spin), they provide digital output, a full rotation have a number of steps and they have push button also.
Connections for Raspberry PI should be done as follows:
    + pin to 3.3V
    GND pin to Raspberry PI GND
    SW to one GPIO
    CLK and DT to two GPIOs
When the push button is pressed the pin labelled SW is connected to GND. This can be set up as any push button(see below).

Push button
A push button can have two states on or off. There are two ways to hook up a button to a logical circuit(e.g Raspberry PI GPIO's):
We have to use the first setup(with pull up resistor) for the tool to work properly. So, for the push button of the rotary encoder above, we have to cable it like this:
 
The tool
I have created a tool to allow you interface rotary encoders(and also push buttons) with Raspberry PI GPIO's. Also you can set an XBMC command to be executed for click, left rotation and right rotation.
The tool can be otbained from my Google Code project(link to page).
It is very easy to use it. You just have to accomplish two steps:
    - copy rpi-xbmc-remote in a place where it can be accessed from anywhere(e.g. /usr/bin)
    - call it using sudo rpi-xbmc-remote /path/to/configuration/file

The configuration file
Example:

ip:localhost

button:7:KB:return

encoder:clk:23:KB:up:dt:24:KB:down
The configuration file can reside anywhere on the disk. It provides a way to define two kind of inputs for XBMC: regular button and rotary encoder. Lines should be less than 100 characters in length. Lines starting with # are comments and are not being processed.
 

Example of regular button definition: 
button:7:KB:return
    - 'button' means it is a regular button
    - '7' means use GPIO7 for this button
    - 'KB' means XBMC device map
        "KB" - Standard keyboard map
        "XG" - Xbox Gamepad
        "R1" - Xbox Remote
        "R2" - Xbox Universal Remote
        "LI:devicename" - valid LIRC device map where 'devicename' is the actual
name of the LIRC device
    - 'return' means XBMC button name to be called(see XBMC keymaps)

Example of rotary encoder definition:

encoder:clk:23:KB:up:dt:24:KB:down
    - 'encoder' means it is a rotary encoder
    - '23' means use GPIO23 for rotary encoder CLK
    - 'KB' means XBMC device map for rotary encoder left turn(same as above)
    - 'up' means XBMC button name to be sent for left turn of the rotary encoder
    - '24' means use GPIO24 for rotary encoder DT
    - 'KB' means XBMC device map for rotary encoder right turn(same as above)
    - 'return' means XBMC button name to be called(see XBMC keymaps)

Keep in mind!
You have to put a pull up resistor for every push button you define in the configuration file. If you don't do this then the state of the button will be variable when not pressed(it will oscillate between 0 and 1) and it will behave like it is pressed randomly.


Have fun!

28 comments:

  1. why don't you use the steering wheel buttons on the car for controls? I like the potentiometer for volume, but for additional controls, steering wheel buttons are great..


    tap into the CANBUS / LIN Bus in the interior of the car with some wires run to a CAN transceiver and on RPi you'll need a CAN controller chip as well, then connect that to SPI on the RPi.

    The RPi not having these and off the shelf capability with CAN Capes for expansion boards was a main driver in my project for using the Beagle Bone Black instead of RPi.

    notes on setting up with SocketCAN on a BeagleBone Black and capturing CAN bus messages at http://the8thlayerof.net

    Sam

    ReplyDelete
    Replies
    1. Hi Same!

      It would be great to use steering wheel controls but my car doesn't have this option. Maybe I will buy a new steering wheel someday.
      Thanls for the CAN tip. I will try this in the future(get some data from the CAN bus), but at this moment I am working on integrating SI4703 I2C FM Radio receiver with XBMC.

      Andrei

      Delete
  2. Hi came across your post while looking for car stereo controls. I like your tip on "Example of regular button definition" Cheers

    ReplyDelete
  3. Thanks for the great guide. I managed to get it working with a switch I added to my Raspberry Pi with Openelec. Is there a way to play a file when the button is pressed? I know how to do this by calling the API with a JSON message, but is there a way to put this in the config file? Thanks.

    ReplyDelete
    Replies
    1. Please tell me what JSON command are you using. Also, I have significantly improved the controller application, please have a look at: https://code.google.com/p/andrei-development/source/browse/#svn%2Fbranches%2Fdev%2FCarPc%2FCarPC_Controller

      Delete
    2. This is the sort of command I use to play a video:

      http://10.100.101.109/jsonrpc?request={"jsonrpc":"2.0","id":"1","method":"Player.Open","params":{"item":{"file":"smb://my_computer/videos/childrens/Tom%20and%20Jerry/049%20-%20Texas%20Tom.mp4"}}}

      Is there a way to specify this in the configuration file? Thanks for your help.

      Delete
    3. You can simply add this in the gpio_description file:
      xbmcbuiltin_PlayMedia(smb://my_computer/videos/childrens/Tom%20and%20Jerry/049%20-%20Texas%20Tom.mp4)
      For more information, please check the function PlayMedia from [url]http://wiki.xbmc.org/index.php?title=List_of_built-in_functions[/url]
      ou can use any builtin funtion from there.
      Also, please use my latest carpc-controller. You can download it from my google Drive(link in the right of this blog).

      Delete
  4. I need to create a video display kiosk that has three push buttons on the panel, each of which when pressed play a different video. Will it be possible for me to connect multiple push buttons to the Raspberry Pi to do this? Is it possible to program the Pi to translate each button pressed event and play a different video each time?

    ReplyDelete
    Replies
    1. You can use my openCarPC-controller which has this functionality. You can control as many pins as you wish, just have to hook them up with a pull up resistor(as in this post: http://www.engineering-diy.blogspot.ro/2013/11/control-xbmc-from-raspberry-pi-gpios.html).
      You can get the latest code from my Downloads page, in the top right of this blog.

      Delete
  5. I believe your rpi-xbmc-remote no longer exists in Google Code. I really need it though so could you reupload it?

    ReplyDelete
    Replies
    1. It is available as source code in Google Code page. You just have to compile it and then install it.
      If you want the executable version please get it from: https://drive.google.com/folderview?id=0B__Rs5JF53-kY05ZeTVEQi1xQkE&usp=sharing&tid=0B__Rs5JF53-kVk03SGQ5OWY4Z0k#list
      The new name is now opencarPC-controller

      Delete
  6. Hallo,
    First I want to say thank you for your great work.
    I want to use your opencarpc-controller on a raspberry with raspbmc.
    When I start the opencarpc-controller via SSH-Command-Line (sudo opencarpc-controller /etc/gpioconfig), the opencarpc-controller starts.
    If i press the button on the GPIO-Pin I got a "KB" on my SSH-Command-Line, but on the XBMC-Desktop happens nothing.
    How can I start opencarpc-controller correct?

    ReplyDelete
    Replies
    1. Hallo,
      now I can start the opencarpc-controller via SSH-Console. Inputs via my push-button where recognized, the xbmc reacts correct.
      The problem, how i can start the opencarpc-controller at systemstartup automaticaly does still exist.
      An entry in /etc/rc.local sudo sudo opencarpc-controller /etc/gpi_descritption) was not successful.

      Delete
    2. Okay,
      i created .xbmc/userdata/autoexec.py:
      import os
      #GPIO Eingang aktivieren
      os.system("sudo opencarpc-controller /etc/gpio_description")

      That fixed my problem

      Delete
    3. Hi,

      I'm working on my own Car-PC project for a while now with the above mentioned CAN-gateway communication. By pressing buttons on my nav-system (Audi Rns-e) I capture the CAN-messages (hardware PiCan from SK-Pang). I eddited the candump.c script to be able to send keypresses on specific CAN messages.

      The last step is to send XBMC commands (by above SSH-console commands).
      I added XBMC to my Raspbian (otherwise I couldn't use the CAN modules) and now I notice that the RASPBIAN build does not seem to support or run some XBMC server, wherefor the JSON commands return 'curl (7) host not found'. I get ping replies from 127.0.0.1 but am lost in solving this.

      I radically tried to install fresh XBMC and got the SSH commands to be picked up in XBMC. Now the modules for CAN return errors :(

      Summaried: Does anyone know how to get up and running XBMC server or any way to talk to XBMC from RAPBIAN build? (3.12.20+ or up)...

      Kind regards Jeroen

      Delete
  7. Could you upload your code to Github, now that Google Code has shutdown

    ReplyDelete
  8. Yes, Google Code is out !
    Thanks

    ReplyDelete
  9. Your link to Google Code returns a 404 :/

    lg, couka

    ReplyDelete
    Replies
    1. Nevermind, I found your GitHub-Page :D
      https://github.com/bboyandru/carpc-controller

      lg, couka

      Delete
  10. Hi, how can i use (start) your Tool?
    I am on Ubuntu 15.04.

    PS: Great Work!!!

    ReplyDelete
    Replies
    1. I download the files from GITUB, the other are gone.

      Now i ve 3 Files
      carpc-controller
      .project
      .cproject

      What should i do with this?

      regard

      Delete
  11. Once again you've published a top notch blog post on your blog. You have been very consistent in publishing your posts and most of them have been top notch. I hope you'll keep this up in the future as well.

    ReplyDelete
  12. You could install some hacking tools into your , entertainment system!


    Booster seat for 4 year olds

    ReplyDelete