Wednesday, January 14, 2009

Arduino Webcam Servo Project




Having received my first Arduino I had to come up with a good first microcontroller project.  Something not too complicated but more than just a book example.  I had just purchased a wireless webcam (Linksys WVC54GCA) and thought it would be cool if I could move it around on a pan & tilt mechanism.  And how much cooler would it be to be able to control it over the internet?

After I completed this project I looked around the internet and found several other examples of this type of thing.  But I hope the documentation here is still useful as I don't recall any examples showing detail of how to use the Arduino and more specifically, the Xport in the designs.

Here are the pieces I used:

* Arduino ethernet shield (http://ladyada.net/make/eshield/index.html)
* 2 servos from my crashed RC glider
* The mechanical bits.  I used machined aluminum but you can use anything you like

CODE

I started with Ladyada's examples (http://ladyada.net/make/eshield/examples.html)

You can find my Arduino code here:  http://docs.google.com/Doc?id=dhjz44fg_5ct5p2374

You'll need to make sure you include the necessary libraries (AF_Xport.h, AFSoftSerial.h, ServoTimer1.h) which are needed by the code.

The code is fairly straightforward except for the bits about the web page.  Being an HTML rookie, I struggled a bit, but keeping things simple and using MS Frontpage as a crutch, I managed to create a very simple webpage.  And it needs to be simple to fit within the limited memory of the Arduino.  The large char strings early on in the code store the HTML.  Note that for every quotation mark in your HTML, you need to add a \ before so that you don't terminate the string.

One key is to enable a variable via the webpage pushbuttons.  Each pushbutton assigns a different value to the variable which tells the program which servo to turn as well as the direction.  This line of code was important to understanding how web pages transmit user input:

found = strstr(linebuffer, "?servo="); // "?inputfield=" GET request

Printing out the line buffer in the serial window allows you to see exactly the info that your web page is sending out upon a button push.  You look for this information in the line buffer and then extract the useful part - in this case, the value to indicate which button was pushed.

Another slight glitch I found was that the baudrate had to be reduced from that used in ladyada's example.  I don't know why.

WEBPAGE SETUP

One more area of complication is accessing the webpages.  The Linksys webcam has its own internal webserver.  This is accessed remotely by enabling port forwarding on your router.  To access this remotely, make use of your computer's ip address (not your local network) followed by a colon, and then the port that the webcam uses.  For example (this is a fake ip addy):  http://208.168.201.37:1024 

The Linksys happens to use 1024 as its port.  You do exactly the same for the Xport, except it uses a different port which is specified during the Xport setup process (see the ladyada site for instructions).  In my case it was 10001.

Note that your ip provider probably provides a dynamic ip address to your computer, meaning that it may change over some time (days, weeks, months).  To deal with this situation you may want to make use of a DNS service provider.  I used dyndns.com which is a free service.  This allowed me to use a semi-custom name that is linked to your computer's ip address (for example, something like http://yourname.dnsdojo.net:10001/).  When your ip address changes, software on your computer updates the DNS service provider.

Here's an example of the router setup to enable the port forwarding:



ELECTRONICS

These are stupidly simple.  I merely plugged the servos into ground and pins 9 and 10.

MECHANICS

I was lucky to have access to aluminum scraps and a machine shop.  I have limited skills with the equipment but was able to fashion some workable parts.  You can easily use any material you like.  I happened to put the pan servo mechanism on the bottom and then attached the tilt mechanism to that.  

The entire mechanism was mounted on a tripod using a tapped 1/4" hole in the baseplate.  Here are some additional pics of the mechanism.