Intro: Raspberry Web Server Sending GET Data to Arduino Nano Driving 6 Servos
This is a small project I did for a colleague who is building a 1/48 scale RC model of a Fletcher class Destroyer (yup...that makes the "model" about 8 feet long!) He asked if I could animate the 5 x MKIII 5"/38 gun turrets, complete with bearing and elevation, and also being able to individually control them, or control them in selectable groups, as per the real thing.
So after 3 days of researching, testing and fine tuning, I have: 1 x Raspberry Pi (B), running as a WAP + apache, php, python + CGI support 1 x Arduino Nano, receiving data from the Raspberry serial comms pins, 3 x pan/tilt units that represent the 3 fwd turrets 1 x web interface where I can set the bearing and elevation of any chosen gun (or guns) and a "go" button ...and using my Kindle to control them.
I'm passing the gun number, bearing and elevation as GET request variables, using a python CGI script to get those variables and send them to the arduino.
The video shows the servos moving at full speed, but I have since changed from the servo.h arduino library to the VarSpeedServo.h library and can set the speed to something a bit more realistic. It also makes the servos appear to move together rather than sequentially.
I've also (since the video) made up a proper circuit board to accommodate a 26 pin socket for the Raspberry ribbon cable, a basic voltage divider for a logic level converter, headers for plugging the nano into and header pins for the servo connections. It also appears to have cleaned up a lot of the servo jitter I was getting.
[Edit] - Due to popular demand...(both of them)...I have now written a more complete instructable...so here we go
Step 1: Things You'll Need...
- Raspberry Pi (Rev B) with 4-8Gb SD Card and micro USB cable.
- USB WIFI dongle - Something like the Edimax EW-7811Un 150 Mbps Wireless 11n, but try and get one using the RealTek or Atheros chipsets. It's less likely you will have driver problems...they also play nice with Linux and the aircrack suite ;-)
- A number of servos - This project used 6 x TowerPro 9g micro servos.
- Arduino Nano
- 3 x Pan/Tilt servo mount (about $4 each off ebay.)
- 1 x 3.3k and 1 x 1.6k 1/4 watt resistor - These are to make up the voltage divider for the logic level converter
If you make up a proto board for the nano, Raspberry connector header and servo headers, you'll also need:
- 26 pin double row (2 x 13) female/female ribbon cable,
- 26 pin (2 x 13) pin male header,
- 2 x 10 pin stackable female headers (for the nano to plug into),
- A 2 terminal screw connector block.
- Enough standard header pins for the number of servos. In my case, 3 rows of 6 pins arranged in an 18 pin block.
Step 2: Setting Up a RaspberryPi As a WAP, Web Server and JQuery Simple Slider Support..
I used putty to telnet into the Raspberry from my laptop via a cat5 cable, and then joined the Raspberry to my home wifi for updating and downloading the dependencies.
Install all the packages required to run a web server on the Raspberry..see here for a good guide:
Make sure you have java SE Runtime installed on the Pi...
- On the Pi terminal, type "java -showversion", and you should get some nice output about Java SE Runtime and version numbers
Update your raspberry:
- "sudo apt-get update",
- "sudo apt-get upgrade"
- Reboot, then log back in once it has booted
Change directory to /var/www/
Use "wget" to grab the jQuery Simple Slider from http://loopj.com/jquery-simple-slider/
Now..highlight all of the code in the file "gcs-php.txt", rightclick and copy.
In the Pi telnet terninal, open a new file called "gcs.php"...or whatever you want to call it
- "sudo nano gcs.php"
Rightclick in the putty terminal window and the clipboard will immediately paste into the nano editor.
CTRL+O to save, CTRL+X to close nano
You also need the form handler cgi script to send the signals to the Nano..
Make a new directory in /usr/lib/ called "cgi-bin"
Open a new file called "gcs.py" (sudo nano /usr/lib/cgi-bin/gcs.py)
Copy and paste the code from gcs-py.txt into the nano editor, then save and close like we did above.
Make the file executable with "sudo chomd 777 /usr/lib/cgi-bin/gcs.py"
The one part that could trip you up here is the connection device. Do a "ls /dev/tty*" and see what is listed. Then connect your USB cable between the raspberry and the Nano, and all going well, you should see "ttyAMA0" (that's a zero..not an 'oh'). If it shows something different, then change the python code to reflect the proper device number.
Step 3: More Apache Stuff...
You now need to allow Apache2 to run python scripts...and here's how.
Once all that is done you should be ready to go
On your computer, open a browser, and browse to http://[your Raspberry IP address]/gcs.php
If the browser complains it cannot find it:
Open a command prompt on your computer and try to ping your Pi "ping [pi IP address]" If you are successfully telnetting to it over putty, then there's something up with your browser settings such as a proxy. Don't ask me to help, I have no idea how your internet connection and browser is configured. Google is your friend.
Check to see that apache is running..
"pgrep apache"...should show a bunch of numbers, which is the process IDs (PIDs) for currently running apache processes, or
"sudo /etc/init.d/apache2 status"...should come back as "Apache2 is running (pid xxx)."
If it's not, then start it with:
"sudo /etc/init.d/apache2 start"
Failing all of that...google to the rescue again.
All going well, you should see the page as shown in this step.
Step 4: Time to Set Up the Pi As a WAP...
There are numerous tutorials on the interwebs for doing this, so I won't bore you by regurgitating what's already out there...
You can tweak the page code and customise the page and slider names/values to suit your needs.
At this stage, you can shut the Pi down and close any telnet/ssh/putty sessions you have open to it.
Step 5: Now for the Arduino Bit..
Add the VarSpeedServo library to your Arduino IDE using the usual library import method.(https://github.com/netlabtoolkit/VarSpeedServo).
This library is very good for being able to control the speed of the servo without having to mess about PWM timing, messing with the standard servo.h file or putting slow loops in your code. There are 3 parts to the servo method..angle, speed and "wait for move to finish". Check out the sketch, and you'll see it is pretty straight forward.
Copy and paste the sketch from the attached txt file, paste into a new sketch and upload to the Nano. I'm still learning this language, and it is probably a very bad, roundabout way of doing it, but it works, so don't be too harsh on me.
Now, you do have to make up a voltage divider for use between Pi (pin 4, Tx) and Nano (pin 16, D1/Tx). This is because the Nano uses 5v on the IC2 pins, and the Pi uses 3.3v.
Connect the 2 together directly..and the smoke will escape from the Pi (of course the more expensive one will burn first..perfectly logical!)
Once you have done that, then simply connect the signal wire from each servo to the Nano pins 3, 5, 6, 9, 10, 11 (all the PWN pins), all the servo + wires to a common power rail, and all the ground wires to a common ground rail. Make sure the Nano gnd pin, Pi GPIO gnd pin and servo grounds are all connected to the same rail.
Connect the servo + rail to a nice beefy 5V power (I'm using a 7.4V 1000mAh LiPo), battery negative to the ground rail.
Plug a USB/USBmini cable between a USB port on the Pi and the Nano. Plug a power supply into the Pi, and wait until everything is booted up. Connect to your Pi WAP, open the browser and navigate to your web server...playtime! :-)
Hope this is of some use to someone. It was interesting putting all the different parts together, and opens up some options for combining the power of the Pi to deliver a web based GUI for controlling an arduino back end.
I'm currently experimenting with the ATTiny84 20PU chip talking to and from a NRF24L01 wifi module for a web-less version of this. I may follow up with an instructable for that as well.
Head over to my youtube link for a video of the Raspiduino in action, and maybe stop by my A3 printer to CNC machine conversion video on the way past.