Wii Controller + Raspberry Pi + Python = Awesome!!

        So this is going to be a many part series for a robot project i’m working on. This first one covers writing a program that runs on the Raspberry Pi. This program is going to connect through bluetooth to a Nintendo Wii controller and allow you to control the data via Python. Cool I know!

So what I did first was familiarize myself with connecting my Wii controller to my PC (virtual desktop running Ubuntu) and using Cwiid as a module I was able to write a small program to grab the data. Once I knew I could connect and communicate all I needed to do was port this over to my Raspberry Pi.


Parts list:

  • Raspberry Pi
  • bluetooth USB dongle – I bought this one SABRENT at Frys Electronics for $10
  • Wii Remote – Black
  • Standard Raspberry Pi cables (power, keyboard, monitor)

Make sure you first have debian installed and up to date on your Raspberry Pi. I did a ‘how to’ earlier that can be found HERE, the only difference is I installed the BETA ‘Wheezy’ version. Once you have your board up and working make sure you update everything.

sudo apt-get update

 

sudo apt-get upgrade

This could take hours depending…

Installing the Bluetooth Dongle and drivers


The first thing is to plug your Bluetooth dongle into one of the USB ports of the Pi.
Once you have your Raspberry Pi booted up install the bluetooth package for it. Mine tool about an hour to download. To reduce this time install it without the GUI started.

sudo apt-get install bluetooth

Once it is installed, make sure you can see the bluetooth device and scan for your Wii Remote.

sudo service bluetooth status

If it isn’t connected type this to start up your bluetooth, or reboot your Pi:

/etc/init.d/bluetooth start

To get your bluetooth address type,

hcitool dev

This will show your dongle address. Something like this:

Next you can scan for devices. Type the following in, then press the 1+2 on your Wii remote to make it searchable. By pressing the one and two this will put the controller into discovery mode.

hcitool scan

You should see something like this, representing your Wiimote address and name.

Installing CWiiD for Python

Next we need to download CWiiD, this is a great python module that allows us to control a Wii remote in Python, it has been ported to the ARM, x86, and AMD. To download and install type:

sudo apt-get install python-cwiid

Now that it has been installed lets try it out. I’ve created this program that controls the Wii remote in Python. All you have to do is download it to your main directory.

wget https://sites.google.com/site/brianhensleyfiles/wiimotetest.py

Then to use it type,

sudo python wiimotetest.py

It will ask you to press 1+2 on your Wiimote, once you do so it should say “Wii Remote connected…”:

I have this setup for part of my robot project so I have a center point AKA ‘position 50’ and it has limits of 0 to 100. So as you hold your wii remote horizontal you can press left and right and it will increment +/- 10 from 0 to 100. The other features are pressing the button number 1 and 2 individually. This will be displayed by saying those buttons were pressed. The last option is pressing some of the buttons together and having a message say it is moving forward or backwards and with what position.

I set the MINUS button to turn on and off the Wiiremote rumbling feature.

To disconnect and close the connection I setup the PLUS button on the wii remote to disconnect from the Wiimote.

Understanding CWIID / TIPS:

In python it’s important to know more about writing your code to take data from the CWIID module and controller. The first three things I recommend doing are including the module, enabling buttons, and try out the State options.

import cwiid
wm = cwiid.Wiimote()

Next be sure to enable button data reporting,

wm.rpt_mode = cwiid.RPT_BTN

What you can do next is determine what the value of each button is (see picture below). NOTE: when you press mutliple buttons the number values are added together. For example, when you hold the remote horizontally and press the left button it returns a value of 2048, when you press the right button you get 1024.

wm.state[‘buttons’]

If you click button #1 you get a state value of 2, so for example you could press the right button and the button #1 and get a state value of 1024 + 2 = 1026, because they were added. In my python script you can see I did comparison statements of the total status value for multiple buttons. Here is an image I drew up with the button values, labeled in orange. Use this to reference your Python program for what buttons you are pushing.

NOTE: it may be useful to add some delays in, so you have time to return the remote value and have it do something in your program.

Thanks for reading! Please be sure to comment your experience and follow me on twitter to keep up on my next blog posts! twitter.com/bhensley

Bibliography:

http://packages.debian.org/wheezy/bluetooth

http://wiki.debian.org/BluetoothUser

awesome inside wiiremote details:
http://wiibrew.org/wiki/Main_Page

Good Wiimote python example:
http://talk.maemo.org/showthread.php?t=60178

50 thoughts on “Wii Controller + Raspberry Pi + Python = Awesome!!

  1. My pi is able to see my wiimote, however after downloading your code and running, sudo python wiimotetest.py, I get a invalid syntax error, please help. thanks.

  2. Wow. It worked. Great instructions. I've been using a nunchuck and arduino to control my model train, but I'd like to use the Wiimote and pi to run it using python. I think you just sold me on the idea. Thanks!

  3. Um, The button named 516 in the illustration should possibly be 512. These are binary values as illustrated in the extensive wiiremote documentation on the detailed wiibrew site.

  4. Nice article, really helped me get on my feet with this, just needed the "hello world" to get rolling.

    A couple things to note, your image for the right pad button is labeled as 516, when it is actually 512.
    I see no reason why you need to execute the python script with sudo, it runs fine as a user (for me at least).
    Lastly, instead of invoking your bluetooth commands via /etc/init.d/bluetooth, it is recommended that you use the 'service' method… Like so: sudo service bluetooth status

    Thanks again.

  5. Thanks Andrew, I've made the correction to the picture to display 512. Ya looking back I wouldn't need to run it as sudo, rookie mistake. Glad you got it working. Enjoy!

    Cheers,
    Brian

  6. As there has been no follow up, was it not possible to go further with the controller? Looked really good too! I was thinking that if the controller was mounted on a robot it could provide feedback data on position etc? Great article tho!

  7. Dunno if it wasn't available back when this blogpost was posted, but cwiid provides constants for all states (cwiid.BTN_1, cwiid.BTN_2, etc).

  8. Up to a point, your example works great. Until everything is synched and I get the "Press the PLUS button to disconnect the Wii and end the application" message. After that, I get no response at all. Just the 4 lights blinking repeatedly whenever I press a button. Is it a bad wiimote, or something else?

  9. For debugging I would make sure the wii remote is connecting by setting one of the lights to show once it's connected. To do this you can send this command: wm.led = 1

    Try using the red sync button instead of the 1+2 button for connecting. Also try unplugging the USB bluetooth and reconnecting it.

    In my latest blog, the robot one, i go into detail about doing error checking for if the remote has connected. I don't know if this will help disconnect… but just play around with the python 😉

    http://www.brianhensley.net/2013/03/raspberry-pi-robot-wii-remote-phase-1.html

    Brian

  10. Hello!
    So far I've not even gotten my WiiMote to be seen by the Bluetooth dongle. It's a Targus one ostensibly aimed for their laptop mice. It works properly on my Linux setup who's running Slackware 13.37. For that I built and installed the Python based stuff that the Raspberry Pi also uses, on it. When I run the tool for scanning to find the controller, it promptly works for a bit and then complains that it times out. However the same instructions work on the box and even your Python script works. Oddly enough I had the same problems with the R.Pi and the same dongle and even the controller.

  11. But who makes your Bluetooth dongle? The branding on mine is that of Targus, but the internal chips is that of the same company who makes the processor on the Pi.

  12. Is there any way to use the tilt, speaker,and nunchuk? Also epic project me and my dad are planing to do the same with a rotatable camera and other cool things. he's helping me because im new and only 14.

  13. The wiimotetest.py if statements have some of the wrong wm.state['buttons'] values. I edited my local file to use the values in the picture. It now does things for all buttons, thanks for the tutorial.

  14. Do you happen to have a file that has all of the possible commands to interact with the Wii remote? Or can you somehow read the library that you need to download? A bit new, so help would be nice.
    -Thanks!

  15. Hello Brian. I saw the Video and bought 3 different USB-Bluetoth Dongles. I updated my PI (Raspbian installed) and then followd your Manual.

    from the command hcitool scan i get the answer: 2C:10:C1:C8:4E:A5 Nintendo RVL-CNT-01-TR

    But your python-script answers always:

    Press button 1 + 2 on your Wii Remote…
    No wiimotes found
    Traceback (most recent call last):
    File "wiimotetest.py", line 99, in
    main()
    File "wiimotetest.py", line 21, in main
    wm=cwiid.Wiimote()
    RuntimeError: Error opening wiimote Connection

    I tried all the 3 Sticks and always rebootet the PI. I get every time the same error.

    Do you have an idea for me?

    Marco, Germany

  16. Perhaps there is by any means to work with this tilt, phone speaker, along with nunchuk? In addition epic challenge myself along with my dad are generally planing to perform a similar using a rotatable photographic camera and also other cool items. he or she is supporting myself simply because im brand new in support of 14.

  17. Perhaps there is by any means to utilize this tilt, audio, along with nunchuk? Additionally impressive undertaking everyone along with dad usually are planing to try and do exactly the same with a rotatable photographic camera and also other neat things. he is encouraging everyone mainly because i will be completely new in support of 18.

  18. Hi!
    Thus far We have not just received my WiiMote being noticed from the Wireless bluetooth dongle. It's a Targus one evidently geared with regards to laptop these rodents. This performs properly on my Linux startup who has managing Slackware 13. 37. For that My spouse and i built as well as put in the actual Python structured stuff that the actual Raspberry Pi in addition utilizes, into it. After i function the actual device intended for encoding to find the controller, the item instantly performs for a tad and then complains that it periods available. However the exact same guidance develop the actual container as well as your current Python script performs. Strangely enough I did a similar difficulties with the actual Third. Pi as well as the exact same dongle as well as the actual controller.

  19. YES!!!!! Thanks for this!
    Long story short, we're opening a gaming cafe, planning on having some VR stuff going on, and now thanks to you I can integrate my wiimote directly into Blender with python and make some exclusive VR games and interactive experiences.

  20. Sorry for the question, but it there any way you might be able to post an image of your card? That way I can just flash it back to a Micro SD (opposed to downloading all of the individual packages). If you can, that we be awesome. Again, sorry for the request, but if you can, it would be very much appreciated.

Leave a Comment