Tag Archive for 'bluez'

Programming with the WiiMote (II)

I will use in the following posts Ubuntu Linux (8.04) as operating system, that I’ll give some specific tips only for it, on other systems some things might work different, but the code should work without modification everywhere. At first we need to install some software requirements, the python wrapper for the WiiMote and for general Bluetooth actions the bluez python wrapper:

sudo apt-get install python-cwiid python-bluez

To start simple, we have to read out the adress of the WiiMote we use, this is done by activating our bluetooth device, pressing 1+2 on the WiiMote and running the following command:

hcitool scan

After about 10 seconds this will produce an output like this:

00:1F:45:8B:22:6F    Nintendo RVL-CNT-01

The first of this two columns is the identifier of your WiiMote which we will need for our first python script. For the beginning we only want to connect with our WiiMote and let the LEDs blink. Knowing the Hardware adress of the WiiMotes simplifies the connection via python:

import cwiid

wiimote = cwiid.Wiimote(”00:1F:45:8B:22:6F”)

Now after we connected to the WiiMote via Python(you still need to press 1+2 while the connection is initiated) we want let the LEDs blink. This is done by setting the wiimote.led to valid mask. For example, if you want to turn on the LEDs 1, 3 and 4 this is done via this short code snippet:

wiimote.led = cwiid.LED1_ON | cwiid.LED3_ON | cwiid.LED4_ON

To let them blink, we put all in this in a never-ending while loop and get the following whole script:

import cwiid
import time

wiimote = cwiid.Wiimote("00:1F:45:8B:22:6F")

while True:
    time.sleep(0.1)
    s.led = cwiid.LED1_ON | cwiid.LED4_ON
    time.sleep(0.1)
    s.led = cwiid.LED2_ON | cwiid.LED3_ON