I've seen a lot of ways to communicate to arduino over the net, but none of them could handle pushing back the messages from arduino to the client(you) in real time and vice versa.

In this instructable you will learn how to connect to your arduino and control it over the net, set up a video stream, and how to control stuff with your arduino all in realtime. I'll try to show you on a concrete example how this could be done, but the code I used and wrote is going to be generic so you can use it for your projects. Note that I haven't discovered anything new but rather used code that I found lying around the net, built from it and changed it fit my needs.

In this example my arduino is going to control a candy grabbing machine. Do you remember when you were a kid and went to the carnival and there where those machines in which you put money, and it let you play with the crane, grabbing stuffed toys and all kinds of things, then if you were skilled enough you could grab the toy, drop it in a hole and go home with new furry friend?
The candy grabber is a desktop version of this carnival machine.

So how should it work? The idea is that there is a Flash AIR app on my home computer that when a remote client connects to it starts the video broadcast. The communication between the client and the AIR app would be through a PHP socket because it can instantly push messages from one to the other. The socket will handle all the clients and the queuing. The Red5 server is used to handle the video broadcast, stream the video and send the arduino commands from the client that is first in the queue to the AIR app (although it could do so much more... we'll talk about that in a later step). Finally TinkerProxy is used to send commands from the AIR app to the arduino that is connected to the same computer.

I'll try to keep it online as long possible but my internet provider is a little quirky so if you can't connect please come back later. Also my upload speed is quite slow so the video is relativly small but if your connection is faster you can easily adjust it to be bigger.

Seems complicated? Don't worry, I'll try to explain everything in detail on the next steps. 

I decided if I win the MakerBot to give it away to my Arduino comunity that started in Croatia. I think that a afordable way to make parts is crutial to for the evolution of any kind of project, and as prototype fabrication in Croatia is absurdly expensive I think this would be of great help to all my friends.
Also I removed the 6 LEDs that were used for the light source. As I didn't make a LED driver I fried them. :-( That's the price for wanting to do something without first thinking how to get about it.
Of course the crapy chinese candygrabber mechanism broke down, so unfortunately the grabbing hand gets jamed and won't go up or down except if I jank it out. The MakerBot would be wery usefull to fabricate the broken part :-)

Step 1: What you'll need

So here is the list of things you need for this project.

1.Candy grabber toy(or anything you would like to control) – you can get them online for around 30$ if I'm not mistaken
2.Arduino / Arduino Mega – bought mine online. I used the Mega because it had more free pins when I connected the motor controller
3.Motor controller – you need some way to control the motors in the candygrabber. I use Adafruit Motor/Stepper/Servo Shield for Arduino 20$ (http://www.adafruit.com/index.php?main_page=product_info&cPath=17_21&products_id=81 )
5.IR LED – I scavenged mine from an old remote
6.Photointerupter – took my out of an old PS/2 ball mouse. (It's used for the X and Y axis movement recognition of the mouse. Each axis has a one)
7.1kΩ resistors for the limit switches(4 pcs.)
8.670k Ω resistor for the Photointerupter
9.410 Ω resistor for the IR LED
10.Bunch of wires and jumper wires(they are practical as you can easily plug them in the arduino pins).
11.Shrink tube

Skills and tools:
1.Soldering iron, solder, soder wick and soldering skills
3.Dremel tool
4.Hot glue gun
5.Multimeter – Trust me, if you don't have one go and buy one. No seriously GO! If you're familiar with programming think of it as a kind of debugger for your circuitry. I wouldn't have finished this project if I didn't have it. Try to find out which of the 20 something wires is for what, and you will know what I'm talking about.
6.A fair knowledge of programing in ActionScript 3 (Flash)

1.Flash CS3 or higher
2.Some kind of Apache/PHP server installed on your computer(I use MAMP as it's free and easy to setup and use) - http://www.mamp.info/en/index.html
3.Red5 server – you need it to broadcast your video from the flash AIR and stream it to the flash client (free if you have your server, else hosting plans range from 30$ up, I used www.videowhisper.com/ ). You can find out more at http://www.red5.org/
4.NO-IP.ORG – as my service provider dosn't give me a static IP so I have to use it to connect to the PHP socket on my computer(also free, need to register) – www.no-ip.org
5.TinkerProxy – so flash can communicate with arduino - http://code.google.com/p/tinkerit/

Here you can download all the source codes:

I had some bugs with the queue in the PHP socket - SOLVED
Added some sounds to the client flash so the user is notified when it's his turn.
A few modifications to the XMLSocketAppController.as , Red5Broadcast.as and Red5SharedObject.as classes - it timeouts when there are no more users. Also this way I addresed the issue of the changing IP(I don't know why but my internet provider does that a few times a day) and you save bandwith.
Also I added  6 LEDs in the top cover to get rid of the annoying light reflection. 

<p>where do i get the candy grabber?</p>
<p>hi ! great tutorial, </p><p>I do it as soon as possible </p><p>thank you</p>
Hey odvratno.zgodan. I wanted to say: hvala ti za dokumentaciju tog projekta! A great tutorial but do you know if there is a way to realize it with a eg. RaspberryPi instead of a full fledged computer? Do you have some tips which way would be best to get it working with a raspberrypi? So I could use html/php and the GPIO Ports from the Raspberry and a Webcam to get the stream out? I think I do not need any Flash or Red5!?<br> <br> Hvala i pozdrav!
<strong>Oh WOW!</strong> <em>This is exceptional.</em><br> Did I miss where to buy commercial coin slot mechanisms to make this a money maker?
Oh my gosh. That is awesome!
G8 Work
Hi!<br><br>Thanks for awesome and detailed tutorial. <br><br>I think i'm already finished, but smth is not working:<br><br>http://clip2net.com/s/1xb7j<br><br>i'm trying to run everything on Windows 7, Adobe Air app shows each status: Connected. <br><br>But not on client side.<br><br>This screen from server (on wich Air app is running and arduino connected) http://clip2net.com/s/1xb7j<br><br>From screen you see that i'm receiving some errors in command promt:<br>&quot;Notice: Undefined index: queuePosition in socketShell.php on line 214&quot;<br><br>This message i get when i run client.swf on other machine. RED5 - connected, Shared obejct: connected, SocketConnection - nothing.<br><br>Please take a look on a screen below https://skitch.com/neskin/gh3aa/client<br><br>Thanks in advance!
It seems that the PHP socket isn't working properly. As a result the Shared object isn't going to work properly. It's because the PHP socket works as a queue and tells which client to connect to the AIR app. There could be a few causes and ways to debug the app:<br>1. I'm not sure which server you installed. On my mac I installed MAMP but I had to enable socket connections. I'm not sure anymore how to do it but you can google it.<br>2. Do you have a video feed in the AIR app and client? The Red5 connection should handle the video. The video is captured with the AIR app --&gt; sent to the Red5 server --&gt; Client <br>3. If the sliders don't work it is probably cause the socket connection doesn't work. Remember that the socket connection handles the clients queue. So if it doesn't work your sliders won't work.<br>4. I checked the PHP code at line 236. Not sure why the error occurred here. I use a low tech solution to debug my code. On the beginning of every IF statement put ECHO &quot;something&quot; and replace the text &quot;something&quot; with different text so when you run the code you can find out at which line exactly the error occurred.<br>5. Where do you host the socket connection? Locally? Did you put the cross-domain policy files in the root folder of your php server?<br><br>To summarize, to me it seems that the error is located somewhere in the SocketConnection.php . I'm not sure where and why as this is the same file that I used for the project and it worked for me. If you'll have anymore questions do not hesitate to contact me. Also I would love to see you project, so if it's possible send me a link or pic.<br><br>Good luck,<br>Bye
hi there,<br>I'm testing this project out locally on MAMP, all status's are read 'connected' . the video streaming works, the arduino connection works. however i keep getting this area, <br><br>Error: Error #2134: Cannot create SharedObject.<br> at flash.net::SharedObject$/getRemote()<br> at com.Red5Link::Red5SharedObject/startSharedObject()[/Users/camoconnell/Documents/folder/Source/com/Red5Link/Red5SharedObject.as:140]<br><br>which means the client sliders aren't working. Have you experienced this problem with remote SharedObjects?<br><br>in the air app i call;<br> xmlSocketAppController.sharedObject=new Array(&quot;rtmp://camoconnell.local&quot;,&quot;motors&quot;,sharedStatusTxt);<br><br>and the client;<br> xmlSocketController.sharedObject=new Array(&quot;rtmp://camoconnell.local&quot;,&quot;motors&quot;,sharedStatusTxt);<br><br>thanks cam
Hi,<br>I haven't had this error before. It seems by the error message that the error is located in the in the class Red5Link. Do the sliders in the client document work or are they unresponsive in both the client and AIR app? In which document the error occurs?<br>If you don't find the bug in the class, could you send a url where I can check your progress? When I get home from work I'll take a closer look.<br><br>Bye
Thanks for your reply,<br><br>Correct, <br>the error is located in the class Red5SharedObject.as on the line where getRemote() method is being called. I get it when i run the ArduinoAir.swf.<br><br>sharedObject=SharedObject.getRemote(sharedObjectUrl,netConnection.uri,false);<br><br>The client sliders are moving but they have no effect on the AIR app sliders. I guess its because the sharedObject aren't being shared.. <br><br>I wonder if it has anything to do with where the files are housed in the directories. the socketShell.php is running on my local MAMP server;<br>http://camoconnell.local/socketShell.php<br><br>the Client.swf is running on my local red5 server;<br>http://camoconnell.local:5080/arduino/Client.html<br><br>and the air app is being published to local folder, not on any local server.<br><br>im testing it locally on MAMP &amp; Red5 servers, are you able to view it remotely?<br><br>thanks again,<br>Cam<br>
Hi,<br>I can't view it remotely. You could set up no-ip.org and then I could view it.<br>When I tested my files the Client.swf was located on my MAMP server not Red5. Try that. <br>Could you post the whole code for the Client.swf? Have you changed it any way from what you have downloaded from the instructable?
Really cool! I could see lots of uses for this. Are there any free online servers, so you wouldn't have to pay $30 for the hosting plan?<br><br>P.S. I got one of the black things with a star on the end. :)
hey odvratno, amazing project! bravo <br><br>I found a portal site which rates free hosting;<br>http://www.free-webhosts.com/reviews/<br>however no red5<br><br>I'm already paying monthly for reseller hosting, &amp; my hosting provider won't upload red5 citing potential load issues.<br><br>if anyone has a solution pls post it!! i had thought of maybe ustream or skype API as a long shot instead.<br><br>- if needed, how would i install red5 locally? for use with MAMP. and Would other computers sharing the same WLAN be able to connect?<br><br>thanks,<br>Cam<br>
I couldn't find any free hosting plans with red5. You can install it on your computer though. If you find any free hosting please let me know.
never seen this on the newsletter!, definitely worth to be mentioned <br><br>LOVE it :)
Uh, why can't I get my prize?
Cool! Will vote.
This and the Portal turret are my favorite i'ables EVER!!!
im using the arduino uno and the code knob to turn a servo it works fine im gessing
This is really cool! Are you going to fix the grabber motor or are you going to leave it as it is? Really cool instructable though.
Hi, I'm glad that you like it. I already tried to fix the grabber but the gears already wore out so they slip and get stuck. The only thing that I can do is jank it out. Sorry :(<br>I'm thinking of scaling up the project. Of course the makerbot could help me a lot :)
Cool. I&rsquo;ll vote for you
I played it a ton! this is so much fun, i tried to get all of the canisters, but the thing that you drop it into was overflowing with them. 5*
I actually own one of these candy grabbers. I got it awhile back and have not done anything with it. What a great idea!
Awesome, I played while u was smoking :-D
<em>Oooohh,</em> <u><strong>THE CLAW!</strong></u>
Hi odvranto, <br><br>cool project! For a simple and scalable way to push data directly to an Intenret-connected Arduino you might want to check our project http://yaler.org/ and https://www.instructables.com/id/Arduino-Web-LED/<br><br>Cheers,<br>tamberg
Wow! Thats so cool and so simple to use. But I couldn't have integrated a queue in it(though I'm not shure), and the video streaming through justin.tv is delayed a few seconds. Using flash and red5 I get a resonable delay on the video and controls, so I opted for it because I'm Flash developer and was curious of red5.<br>That said, if I saw this instructable before I would have probably given up from my aproach.<br><br>Thanks for pointing it out to me,<br>Bye
Hi odvratno,<br><br>Yes, the video streaming lags. It's hosted on a PC completely separate from the Arduino and not relayed through the Yaler relay infrastructure.<br><br>Queuing of messages would have to be done on the Arduino (or your PC, depending on the setup). The relay infrastructure itself does not have any state and just forwards the HTTP request/response back and forth.<br><br>Cheers,<br>tamberg
Yiiieeehhaaaaa! <br>I Got one of the white thingies:-D How will I receive my price?<br>Just kidding;-)<br><br>Really impressive Work, and a very detailed Instructable! Thumbs up!

About This Instructable




More by odvratno.zgodan:Arduino Candygrabber 
Add instructable to: