If you wish to express your gratitude for this information, supporting the advertisers on this site in turn supports me, so feel free to check out what they have to offer.

Monday, June 1, 2015

Adding a WiiMote to the Pi2 media center

I decided to forego the lirc setup, instead I used a cheap bluetooth dongle and added a wiimote to control Kodi. Here are the steps I used below to do that.

First plug in the dongle, then we need to install some utilities to get bluetooth working.

    apt-get install bluez-utils

Now we need to make sure that things are working properly at this point between the pi and dongle.

    sudo service bluetooth status

Now we install the event client for kodi.

    sudo apt-get install kodi-eventclients-wiiremote

Now Kodi needs to know what to do when the buttons are pressed on the wiimote, it parses xmls in the keymaps, so lets make a file to let it know what it needs to do. Replace vi with nano if you prefer it .

    vi /home/pi/.kodi/userdaya/keymaps/wiimote.xml

and place these contents into the file.

    <joystick name="WiiRemote">
      <button id="1">Up</button>
      <button id="2">Down</button>
      <button id="3">Left</button>
      <button id="4">Right</button>
      <button id="5">Select</button>
      <button id="6">Back</button>
      <button id="7">VolumeDown</button>
      <button id="8">ActivateWindow(Home)</button>
      <button id="9">VolumeUp</button>
      <button id="10">ContextMenu</button>
      <button id="11">ActivateWindow(PlayerControls)</button>
    <joystick name="WiiRemote">
      <button id="1">BigStepForward</button>
      <button id="2">BigStepBack</button>
      <button id="3">StepBack</button>
      <button id="4">StepForward</button>
      <button id="5">OSD</button>
      <button id="6">Stop</button>
      <button id="10">CodecInfo</button>
      <button id="11">AspectRatio</button>

Now we need to get the unique address of the wiimote you want to be using for this implementation.

    sudo hcitool scan

and press 1 and 2 on the wiimote while it is scanning and it will spit out an address of the device you put into discovery mode. We're going to launch the wiimote-eventclient and force it to bind to that address. We are going to modify the Start script from my previous post a bit to get the wiimote integrated to Kodi, if you don't have that script in place don't worry I'll just copy what the whole thing should look like into this block below. The one thing you will need to change is the address after --btaddr, again this needs to be the address that we've just found with hcitool.

    vi /home/pi/RetroPie/roms/kodi/Start.sh

And put the following in.

#Start Kodi in the background and grab its pid
/usr/bin/kodi-standalone &
#Start the Kodi Wiimote event client
/usr/bin/kodi-wiiremote --btaddr 00:1F:32:A2:6D:81 &
# enters a loop while Kodi is running to watch the log and kill it when the exit command is found.
while kill -0 $PID1 2> /dev/null
sleep 10
kill=`tail ~/.kodi/temp/kodi.log | grep "remove window"`
if [[ -z $kill ]] ; then
sleep 10
killall kodi
wait ${PID1}
kill ${PID2}

Now when you run the script above you should see it start kodi and you should see your adapter light up and you should also notice a notification in the bottom right that says that it has found a new connection for wiimotes. Now take the same wiimote you used earlier and press 1 and 2 at the same time, it should connect to your pi and you should see a notification on the screen that it has found the remote and give you a battery percentage read out and you should see solid lights on the wiimote. Congratulations you can now use your wiimote with Kodi. When you are done doing things you can press and hold the power button on the wiimote to turn it off and conserve battery, when you want to do stuff again just press the 1 and 2 button at the same time again and it will reconnect.

If you would like to customize your keymap for the wiimote please see the documentation at http://kodi.wiki/view/EventServer for button number reference and see the keymap.xml options at http://kodi.wiki/view/Keymap.xml

Thats it for now, good luck!