-->
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.



<keymap>
  <global>
    <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>
  </global>
  <FullscreenVideo>
    <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>
    </joystick>
  </FullscreenVideo>
</keymap>


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.

#!/bin/bash
#Start Kodi in the background and grab its pid
/usr/bin/kodi-standalone &
PID1=$!
#Start the Kodi Wiimote event client
/usr/bin/kodi-wiiremote --btaddr 00:1F:32:A2:6D:81 &
PID2=$!
# 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
do
sleep 10
kill=`tail ~/.kodi/temp/kodi.log | grep "remove window"`
if [[ -z $kill ]] ; then
sleep 10
else
killall kodi
break
fi
done
wait ${PID1}
kill ${PID2}
exit


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!

Saturday, March 14, 2015

Pi 2 Media Center and Gaming system all in one!

First a fair bit of my history with the Raspberry Pi, there is a Tl;dr you might want to skip to if you are in a hurry.

    I have had Raspberry Pi media centers around my place pretty much since the hardware hit the market, and while they have done the job I can't say that they have done it well or reliably, just ask my family. With the B Rev1 I had out of the box success with a Raspbmc build on Frodo in our living room. This encouraged me to purchase 3 Rev2 Bs to implement them as frontends for our bedroom, my daughter's bedroom and one to replace the memory lacking Rev1 board and repurpose that board as a tinker toy. That is when my bubble burst and paradise was lost. These Rev 2 Bs were an odd bunch as far as performance, I cloned the wonderfully stable image from the SD card on the Rev1 board as it was the fastest way I could get everything configured how I wanted, and brought them online one by one changing their hostname configurations as necessary.
 
    I say they were an odd bunch because while they were all running identical images on the same class SD card with the same power supplies, performance and stability were nowhere near identical across the three Rev2s. One would work just fine for a day or two then lock up completely requiring a hard reset which around 50% of the time would corrupt the SD card and require a re-image. One would work, but the UI was noticeably more laggy. The last is very stable and performs well, in fact it is still running the same image I put on it 3 years ago in our bedroom I haven't had to touch it since I deployed it. I tried OpenELEC to see if a different distro was any better on these little devils, no dice. After a while I could only attribute these variances to the pi hardware itself as the issues followed the pis not any of the rest of the variables. This experience left a very bad taste in my mouth and I started the search for something better, with more horsepower and stability.
 
    Given that my day job is best described as a developer for an R&D firm I no longer had to take the financial risk myself on these things, as an inexpensive micro-computer that is stable is very desirable as a platform to develop a project on, so I have had a chance to evaluate several micro computers. Many more powerful than the pi, but none as easy to get up and running on a project with, so the answer unfortunately always came back to the Pi.

    Some hardware stability came in with the B+ and its superior power supply, but not enough to make me jump for joy. So I can't explain the amount of hope, and fear, that burst onto my horizon when I heard the announcement of the Pi2. My fears were soon alleviated and replaced with an equal amount of joy after reading some of the first hand accounts from those souls that had them in hand already.
   
     We got some in for ourselves at work a couple weeks ago, but my other responsibilities there were keeping me from being able to tinker with them in the office. I asked the boss if I could take one home to evaluate, and to my delight he agreed as long as I made sure it was documented that I had it on loan, I don't think this blog post will count as proper documentation, but I'm documenting it here none the less. So my family get to be my guinea pigs for the stability of this hardware, but by now they are used to it.

Tl;dr : BOO!!! To Pi Rev2s. YAY!!! to PI2s, cool jobs, good bosses, and understanding families.

Now on the the project, a Media Center frontend and gaming system all in one.

The Hardware:

If you want to build this exact setup you are going to need to get a Raspberry Pi2 and all the fixins to power and connect it to your TV (SD card, power supply, HDMI), you are also going to need some game controllers. If you are like me, you still have some snes controllers or perhaps some gamecube controllers laying around collecting dust, if so you're in luck! Go get yourself a USB gamepad converter from amazon and you're set. I chose these http://www.amazon.com/gp/product/B002IXZ5DE/ref=oh_aui_detailpage_o03_s00?ie=UTF8&psc=1 and http://www.amazon.com/gp/product/B0089NVTDM/ref=oh_aui_detailpage_o03_s01?ie=UTF8&psc=1 they work great for my setup, no drivers necessary on raspbian just plug and play.

The OS:

This project was built on Raspbian, images are available from www.raspberrypi.org, I built mine on the 2-16-2015 image. Clone the image to the SD card however you desire, I prefer dd, but win32diskimager seems to be pretty popular. Hook it to your TV and go through the raspi-config to resize the disk, set the right timezone, change the password/hostname, and anything else you might want to tweak with the base raspbian build. I left mine set to boot to the console instead of starting X as we won't actually be needing X.

Here's a shot of the raspi-config tool, you will want to do item 1 for sure, I suggest option 2, setting the time zone is under option 3. If you want to use the tool to change the host name it is under the advanced options menu.


Games:

Once I heard about the RetroPie project back in 2012 I had a fit of nostalgia remembering all the games I played as a kid and wanted to get one spun up. However since I didn't want to have multiple Pis to accomplish the tasks I wanted, as I was certain the Pi 1 didn't have enough horsepower to do the job since it was struggling to run XBMC/Kodi, I never messed with it much. Once the Pi 2 was in hand I dug back in to the RetroPie project and seen that they had made tremendous strides since I had last checked them out and had a very simple to use setup script to take a vanilla raspbian build and install EmulationStation and all the emulators (Yay!) I followed the steps found on http://blog.petrockblock.com/2012/07/22/retropie-setup-an-initialization-script-for-retroarch-on-the-raspberry-pi/ for getting the Emulators and ES installed, I just went with the binary based installations as I didn't feel like waiting hours to test something.
Here's a shot of the RetroPie setup script, I did the recommended install then came back later and took option 3 to further configure my ES installation.
I was also surprised that there were a few games included after the build. Here's a shot of the one I care most about and the reason I started the project.







Media Frontend:

Next I checked out the Raspberry Pi forums to get Kodi installed on the same raspbian build an hoped that there wouldn't be any conflicts with dependencies. I followed these steps found on this forum post nearly to a tee http://www.raspberrypi.org/forums/viewtopic.php?f=66&t=99866 I didn't do the usermod steps as my pi user was already in all those groups so they were not necessary on my system. But I did notice something potentially troublesome with this setup of Kodi, it would always blackscreen when I wanted to exit and never return to terminal in spite of me implementing the recommended steps in the post to prevent this problem. So I made a launch script that contains a watchdog that watches the Kodi log for the exit command then kills the process, which, while not pretty, did solve my problem without introducing any new ones that I know about yet. The script is in the next section.

Yay! Kodi is working!
The User Experience:

So now we have Emulationstation, RetroArch and a handful of other emulators, and Kodi setup and working on the pi. Now my family needs an easy reliable way to access both games and media without having to run command from the terminal. After opening EmulationStation and poking around at the configs a bit I knew I had my answer. What we are going to do is simply create a launcher for Kodi inside EmulationStation, modify the ES config at /etc/emulationstation/es_systems.cfg and add a Kodi system.

    vi /etc/emulationstation/es_systems.cfg

Add the following lines to the end before the </systemlist> line, use nano if you don't know how to use vi.

      <system>
        <name>kodi</name>
        <fullname>Kodi Media Center</fullname>
        <path>~/RetroPie/roms/kodi</path>
        <extension>.sh .SH</extension>
        <command>%ROM%</command>
        <platform>pc</platform>
     </system>
 

Then make a directory for the kodi "roms" in the RetroPie/rom folder.

    mkdir /home/pi/RetroPie/roms/kodi

Then make the script that I mentioned in the Kodi section.

    vi /home/pi/RetroPie/roms/kodi/Start.sh
 
And copy in the contents from below.

    #!/bin/bash
    #first thing we do is launch the Kodi standalone binary and grab the pid so we can monitor it.
    /usr/bin/kodi-standalone &
    PID1=$!
    # enters a loop to watchdog the log file for an exit signal
    while kill -0 $PID1 2> /dev/null
    do
    sleep 10
    kill=`tail ~/.kodi/temp/kodi.log | grep "remove window"`
    if [[ -z $kill ]] ; then
    sleep 5
    else
    killall kodi
    break
    fi
    done
    wait ${PID1}
    exit
   

Then make it executable,

    chmod +x /home/pi/RetroPie/roms/kodi/Start.sh

Now you need to run the retropie setup script again and under Setup>Advanced>Autostart EmulationStation you want to enable it, while you're here, if you didn't configure your RetroArch controller I recommend doing so now. Once done with that reboot your Pi and it will come back and load into EmulationStation and you should see a Kodi system listed with the Start "rom" inside, this will start Kodi and when you exit Kodi you will come right back to ES where you can pick an Emulator or relaunch Kodi. You can load roms directly into the folders on the pi at /home/pi/RetroPie/roms/$systemfolder/ and they will show up next time EmulationStation is loaded.

Kodi system in Emulation station
Here's the script that we made earlier under the Kodi system.


I still have to implement lirc and the GPIO IR receiver I had set up on one of my old Pi's running Raspbmc but that is for another time, I have a nice little wireless keyboard that will serve as navigation in Kodi along side my Android and Yatse for now. You have probably noticed the little rainbow box is in all my shots, I did a bit of research and this is the Pi's built in low voltage warning so I need to get a better power supply for it. The one I'm using is a cheap 4 port Monoprice cell charger with a dedicated port that should serve 2A @ 5V, but apparently it doesn't hold the voltage under that kind of load. There is a way to turn that off in the boot config.txt file, but I would rather replace supply with one that will hold voltage.

Thanks to all the folks over at RetroPie for thier work to make this simple, also to Micheal Gorven for maintaining the repo for the Kodi build I am using. One more shot (sorry for the fuzziness, but you can probably make out what it is.) before I go back to putting this Pi2 through it's paces.



Good luck!