-Corrected button numbers
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.
- 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:
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.
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.
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.
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.
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
awesome inside wiiremote details:
Good Wiimote python example: