The Wii Remote can be used for many different projects that require a remote control. This very basic project will show you the basics.
I've used the Wii remote in a lot of my projects including my Arkwright project which was a robot showing just how diverse it can be.
1. Firstly, we need to wire it up to the pi. (Pin numbers):
1. Firstly we need to update and upgrade the Pi. Please do this step as it will minimise any issues.
$ - sudo apt-get update $ - sudo apt-get upgrade
2. Next we will get the necessary bluetooth files
$ - sudo apt-get install --no-install-recommends bluetooth -y
3. Then get the cwiid library for interfacing with the Wii Remote
$ - sudo apt-get install python-cwiid
4. The following code should be put into the python2 editor
import cwiid
import time
import RPi.GPIO as io
io.setmode(io.BCM)
relay_pin = 4
io.setup(relay_pin, io.OUT)
io.output(relay_pin, 1)
#The relay is off when the pin is set to high
button_delay = 0.1
print 'Please press buttons 1 + 2 on your Wiimote now ...'
time.sleep(1)
try:
wii=cwiid.Wiimote()
except RuntimeError:
print "Unsuccessful. Run again holding buttons 1 + 2!"
quit()
print 'Wiimote connection successful!\n'
print 'Press PLUS and MINUS together to disconnect and quit.\n'
time.sleep(3)
wii.rpt_mode = cwiid.RPT_BTN
while True:
buttons = wii.state['buttons']
if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
print '\nClosing connection ...'
# NOTE: This is how you RUMBLE the Wiimote
wii.rumble = 1
time.sleep(1)
wii.rumble = 0
exit(wii)
if (buttons & cwiid.BTN_A):
io.output(relay_pin,0)
time.sleep(2)
io.output(relay_pin,1)
5. This code uses pin 4 for the relay pin but you can change this. To use the relay, wire one up like so:
6. The following code can be adapted to your needs but just now only shows text displaying what button has been pressed. It can be edited for any of your other projects!
import cwiid
import time
button_delay = 0.1
print 'Please press buttons 1 + 2 on your Wiimote now ...'
time.sleep(1)
try:
wii=cwiid.Wiimote()
except RuntimeError:
print "Unsuccessful. Run again holding buttons 1 + 2!"
quit()
print 'Wiimote connection successful!\n'
print 'Press PLUS and MINUS together to disconnect and quit.\n'
time.sleep(3)
wii.rpt_mode = cwiid.RPT_BTN
while True:
buttons = wii.state['buttons']
if (buttons - cwiid.BTN_PLUS - cwiid.BTN_MINUS == 0):
print '\nClosing connection ...'
# NOTE: This is how you RUMBLE the Wiimote
wii.rumble = 1
time.sleep(1)
wii.rumble = 0
exit(wii)
if (buttons & cwiid.BTN_LEFT):
print 'Left pressed'
time.sleep(button_delay)
if(buttons & cwiid.BTN_RIGHT):
print 'Right pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_UP):
print 'Up pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_DOWN):
print 'Down pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_1):
print 'Button 1 pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_2):
print 'Button 2 pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_A):
print 'Button A pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_B):
print 'Button B pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_HOME):
wii.rpt_mode = cwiid.RPT_BTN | cwiid.RPT_ACC
check = 0
while check == 0:
print(wii.state['acc'])
time.sleep(0.01)
check = (buttons & cwiid.BTN_HOME)
time.sleep(button_delay)
if (buttons & cwiid.BTN_MINUS):
print 'Minus Button pressed'
time.sleep(button_delay)
if (buttons & cwiid.BTN_PLUS):
print 'Plus Button pressed'
time.sleep(button_delay)