Monday, June 3, 2013

Raspberry Wi-Fi Pi

Hope you like my play on words in the title Wi-Fi Pi! I'm doing a tutorial on getting your raspberry pi to run off a wi-fi USB key. This way you no longer have to use an Ethernet connection or have that cord running across your living room floor like mine was doing for months. I also wanted a way to connect to the RasPi when I *cough* finish my robot project. Yes I will very soon. So lets get started by covering the require parts and tutorials you might need to follow to get everything working.

Previous Tutorials Recommended:

Parts List:
  • Raspberry Pi (any version)
  • Wi-Fi USB (EdiMax EW-7811Un) 

I chose the Wi-Fi USB module that I did because it had compatibility success stories online with working on linux, it runs at 802.11b/g/n and it was cost effective. All in my favor.   


If you don't have it installed then you need to install the WPA Supplicant package.
sudo apt-get install wpasupplicant

Next open up the network settings and adjust it to look like the following picture. 

sudo nano /etc/network/interfaces

The important parts are as follows, be sure to save on exit (press ctrl+O (save) then ctrl+X (exit)):

auto wlan0
iface wlan0 inet manual
allow-hotplug wlan0
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf

The next thing is editing the wpa_supplicant.conf file that holds all the network connection information.    This will all vary by your network setup. I happen to be running a WPA2 that is TKIP/AES with a network name of "LALALALALALA" and you'll want to set your password instead of 'mypassword'.

sudo nano /etc/wpa_supplicant/wpa_supplicant.conf

Now all I had to do was reboot the raspberry pi and check my connection,

sudo shutdown -r now

End Result (Connected!)

ifconfig wlan0

My favorite part is connecting to Putty over ssh from my laptop to my raspberry pi without any cords. Then i can sit in the other room and do my coding. Sweet!!! 

>>> Putty Download <<<

Tuesday, March 5, 2013

Raspberry Pi Robot + Wii remote (Phase 1)

So I know my last post on how to use the Nintendo Wii controller with the Raspberry Pi was quite popular. I've been working on the next stage of the project which includes connecting my Digilent Cerebot II Atmega64L micro-controller to the Raspberry Pi. This required that I get the SPI connection working between the Pi and the micro-controller and get the Wii remote to control everything. I constructed this project using a set of Python and C programming with modules I either created or borrowed.

Check out Previous Posts to help build the robot!

My current hardware setup looks like this:
  • Raspberry Pi
  • Digilent Cerebot II (Atmega64 Microcontroller)
  • 2x H-bridges (Digilent)
  • 2x 6 Volt DC Motors (PWM controlled)
  • USB to Bluetooth - this one SABRENT at Fry's is what I used
  • Nintendo Wii Controller
  • 8x 2AA batteries (6V for the Micro, 6V for the Pi)

This picture describes the basic setup of my remote control robot.

Getting Started (Python + Bluetooth):

So in this post i'm going to go over the different parts that build up my robot project. The first one is the Raspberry Pi and Bluetooth module. As I've used in my previous Wii Controller + Python + Raspberry Pi = Amazing! blog I used the Cwiid python module that enables my to connect to a USB Bluetooth dongle I put in my Raspberry Pi. You'll need to install Cwiid for python on the Pi.

sudo apt-get install python-cwiid

Once this is done you'll have to start your program with some python code that connects to the controller. So in your Python code make sure you include the library and then cwiid.Wiimote() connects to the Nintendo Wii remote.

import cwiid
wm = cwiid.Wiimote()

After this you'll want to enable button data reporting.

wm.rpt_mode = cwiid.RPT_BTN

All the buttons on the wii remote have values that can be queried. The best part is they add, so if you're pressing multiple buttons such as left + gas, which are values of 2048 + 1 = 2049. Something like this

wm.state['buttons'] == 2049

(button values for CWiid)

Getting Started (Python + SPI):

The next phase of this project was getting the Serial Peripheral interface (SPI) working with the hardware. As with many parts of my blog this one can take a lot longer as I had to write code in C and make a module that can be called in Python. Please check out my previous post on Python Controlling SPI Bus on the Raspberry Pi and Beagleboard XM for getting Python working. You have to make sure you have SPI enabled in the hardware and from there you can check it by going to the /dev directory. I have another blog Getting SPI working on the Raspberry Pi that helps with getting this setup.

Once your hardware is showing up you can use the module I created and call it in Python using something similar to this. For the record my SPI command is in this format, no real reason, just an arbitrary one I chose for hex values.

spidata = ['FFDDLLRR00']

Where the DD is representing the direction of the robot, LL is the left wheel speed and RR is the right wheel speed. 

spilist = []
#combine the string
combine = str('FF')+ direction[0] +str(rws)+str('0')+str(lws)+str('0')+str('00')
spilist.append(combine) # append the string of combined direction and wheelspeeds
length_data = len(spilist[0])/2 + 1
spiconnection.transfer(spilist[0], length_data)

There is a lot more detail here that i'm not going to go into, but all the logic to my code, directions, logic behind what buttons are pushed and such are written in Python and located in file name "" where x is the version number. All my code revisions will be found in the following folder location. Feel free to check back over time for newer versions.

===> My Code versions: version 0.1 at time of post. <===

One of the many important things to know about SPI is how it works. Understanding it has a data line in and out, a clock and a slave select. All data from the master controller goes to all the slaves, but the master just tells which slave to listen. SPI was originally created by Motorola many years ago and is still very popular in computer communication.

Getting Started (SPI + PWM + Atmega64):

So the last part of this project deals with setting up the Digilent Inc board that is a Cerebot II miro-controller. I'm using AVR studio 4.0 and first started by setting up the mico-controller as a SPI slave so it will take in the clock and Slave Select (SS) from the Raspberry Pi.

Once the SPI signal was setup and tested I configured the Pulse Width Modulation (PWM) signals that would talk to the H-Bridges and help higher voltages go to the DC motors. There are registers that control the speed and the direction. So I have the SPI data read in and then it configures what the speed for each wheel is and set the direction. These values change the speed and feed back the data to the Python code saying what was read in, small form of error checking.  Check the code folder for "WiiRobotProject" as this is my code for AVR code. Still a work in progress so there are tons of comments and things I was trying. it will look cleaner in my final code release.

There are a bunch of features I would love to still get out of this Cerebot II board, such as the motor feedback, maybe add some SERVOs to control a camera (phase 3)? Time will only tell if i get around to it.

Like posted earlier if you would like to check out my code or get some ideas from what I'm doing please check the link below for my source code. Comments. suggestions, and questions are always welcome. Please check out previous tutorials i've written to help with the small details.

===> My Code versions: version 0.1 at time of post. <===

Until next time, peace!


So my next phases of this project will include some of the following as basic ideas:
  • Battery operated
  • wireless putty connection for controll
  • Wii Nunchuck support for camera/laser
  • Servo control - camera/ front wheels
  • Hash checking of SPI data

Saturday, January 19, 2013

Beagleboard XM - How to install Ubuntu 12.10 (Quantal)

UPDATE 7/9/2013: updated links in download folder

Hi folks so i'm back after taking quite a bit of time off. Had the unfortunate experience of my dad passing away recently. I truly believe that family comes first so i've been spending my time elsewhere. Thanks for your patients.

Versions of Ubuntu have changed over the last year so i'm going to give you the run down of the latest version, called Ubuntu 12.10 (Quantal) onto your Beagleboard XM. This procedure will be very similar to installing 11.04 but i'm going to give some more broad answers so if links/URLs die and such you can find your way.

-Beagleboard XM (mine is rev C)
-Micro SD card (4GB or higher I recommend)
-Ubuntu system to install things onto your SD card (i'm using VirtualBox with 12.04)

NOTE: you might need to get a few ubuntu packages for your system (virtualbox), i've already installed a few

apt-get install uboot-mkimage btrfs-tools pv

The first thing i'm doing is referencing Roberts home site and i'm going to look for the latest version of Ubuntu for the beagleboard that I can find.


I'm going to look into the Quantal folder and look for the latest version of the file, then download it. You might have to do the same.


Unzip the files...

tar xJf ubuntu-12.10-console-armhf-2013-06-14.tar.xz

Lets go into the folder... pressing the tab button finishes the folder/file name just in case you are new to linux syntax.

cd ubuntu-12.10-console-armhf-2013-06-14.tar.xz/

So there will be a file located in this folder called '' this is a script that sets up the SD card for you, which is pretty neat. All you need to do is call the location of your SD card and away it goes. So I called the following command to see where my SD card was mouted.

df -h

I found that my 4GB SD card was located as /dev/sdb1 but i'm going to use the name /dev/sdb so as not to include just the partition name but the whole drive location.

Now to install the following, you'll need to start the install process, be sure to add your SD card name correctly!! if you have a beagleboard or beaglebone you need to look for the right syntax instread of 'beagle_xm'.

sudo ./ --mmc /dev/sdb --uboot beagle_xm

It will want to confirm that the right drive is being selected, and ask for verification. Just press 'y' and then hit enter. Now this procedure might take a while depending on your connection to your SD card and the speed of your computer/SD card it could take 5-15 mins so be patient.  Just to note I did have one pop up warning me, but all i had to do was click ok and it continued.

Once this is complete and you get a message saying everything was complete, unmount your SD card if it hasn't been done in the script already.

Now take the SD card and insert it into your Beagleboard XM and power it up!

Default username/password: ubuntu / temppwd

The first thing I recommend is updating the packages and upgrading everthing.

sudo apt-get update
sudo apt-get upgrade


Ethernet - One of many ways to enable Ethernet connection

sudo ifconfig -a
sudo dhclient eth0
sudo ifconfig -a

Serial Peripheral Interface (SPI) - How to enable SPI hardware...

cd ~
cd boot/uboot
nano uEnv.txt

You'll need to remove the # symbol from in front of buddy=spidev, once this is done save by pressing the ctrl+x and confirm the name with an enter.

Continue over to my SPI for beagleboard post that shows how to test that everything works. But the short part is you should see the spidev located under the /dev folder like this after rebooting.

GUI Desktop - How to install a simple GUI desktop

To get the GUI installed you can do many different approaches  I did a couple,  first I started by installing the following, but ran into the problem that xserver no longer made OMAP3 video drivers. I think they're going towards xf86-video-omap. But until that is completely true I did the following.
sudo apt-get install gdm xfce4 network-manager
Then I found Robert's script that took care of the rest. Both the previous and the next commands take a couple hours, but you get a few selections for desktops. LXDE and XFCE.
/bin/bash /boot/uboot/tools/ubuntu/

Thanks for your feedback, and feel free to email or message me.

Good Sources:

Friday, August 3, 2012

Wii Controller + Raspberry Pi + Python = Awesome!!

UPDATE 1/2/2013:
-Corrected button numbers
-Bluetooth commands

        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.


Then to use it type,

sudo python

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.


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!

Useful Links:

awesome inside wiiremote details:

Good Wiimote python example:

Wednesday, July 25, 2012

Getting SPI working on the Raspberry Pi

So one of the phases in my project is connecting the Raspberry Pi to a micro-controller that has many Pulse Width Modulation (PWM) signals. Since the Raspberry doesn't have the number of PWM signals I need to control i'm going to do so through SPI. This post is the 'how to' on getting your SPI signals outputting from your Raspberry Pi. More blogs to come later to show you more details.

First you'll need to download the beta "Wheezy" debian package from here. Then follow the normal instructions for installing it to your board. Once you have installed it and booted up for the second time i ran some updates (That took hours!!) 

sudo apt-get update

sudo apt-get upgrade

While that is updating lets talk a little bit of SPI background knowledge is needed in order to get this working. First off Serial Peripheral interface (SPI) is made up of 4 wires normally. It is a standard that was designed by Motorola for use with their micro controllers. If you're interested in learning more about SPI i've added the Wikipedia link. Here is what a standard SPI setup looks like, multiple slaves is optional.

SS - Slave Select (Beagleboard community calles this Chip Select (CS))
MOSI - Master Out Slave In
MISO - Master In Slave Out
CLK - Clock

Next it was time to update to the latest firmware for the board, as the latest has brought out the SPI controller. To do so I used Andrews Hexxeh rpi-update to do this. Some needed tools...

sudo apt-get install git

Now lets download his tool and install change who has access to read/write the tool.

sudo wget -O /usr/bin/rpi-update
sudo chmod +x /usr/bin/rpi-update

Once this is done you can run it by calling the following command. (mine took a while).

sudo rpi-update

*NOTE - while I was doing this I ran out of space on my 4GB drive. I figured I wasn't really low on space and when I checked it I found my windows tool originally only made me a 2GB partition. There are many ways to do this, but I used GPARTED to expand my space to the real 4GB. It's a live CD...

SanDisk 4GB Extreme 3 SD Card w/Reader

So once the updates are all completed. I rebooted my Pi.

sudo shutdown -h now

Unplug the power and plug it back in. And when I rebooted I went to my /dev/ directory and found my spidev devices!

The last part of this is to test the SPI signal. I'm going to download spidev_test.c

wget;a=blob_plain;f=Documentation/spi/spidev_test.c -O spidev_test.c

Next we want to short the MISO and MOSI pins. Located as GPIO 9 & GPIO 10 as seen below.

We want to edit the spidev_test.c file so it it uses the correct spidevice in our /dev/ folder.

nano spidev_test.c

scroll down and change the device to "spidev0.0" and then save it by pressing the Ctrl+O to save and Ctrl+X to exit. lets compile this thing and run it now.

gcc spidev_test.c
sudo ./a.out

If you see this it's working.

If you see this it's not working.

For more information on how to control your SPI with python try checking out my post for how to do so on the beagleboard xm. Many have found it useful and i'm sure you will too. Python controlling SPI on the Beagleboard XM