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
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).
Skills and tools:
1.Soldering iron, solder, soder wick and soldering skills
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.
Step 2: The candy grabber
You can try to build your own candy grabber but this is not the scope of this project. I’m focusing on connecting and controlling the toy.
So in order to connect the arduino and the toy we have to open it up and take it’s guts out. Use a screwdriver and unscrew the bottom and top of the toy. Looking at the bottom you’ll see a bunch of wires connected to a PCB. Now the multimeter comes to play. Use it to find out which wire is for what. You can find a great tutorial on how to use a multimeter here (http://www.ladyada.net/learn/multimeter/ ).
I made a diagram how the motor and the limit switches are connected(picture 12). The thing is that the joysticks(potentiometer) changes the polarity on the motor and limit switches as you push it from one side to the other(In fact I’m not completely sure that the joysticks are potentiometers because I couldn’t find any data sheets of this kind of potentiometer. If anybody knows what they are drop me a comment). Later will have to separate the motor from the switches by cutting the wires that go from the switches to the motor(picture 1). For now lets just find what is what.
Power it up, insert a coin and push the first joystick ether way and hold it. Now start probing the wires in pairs for voltage. Start with the ones closest to the joystick. Take note that when it reaches the limit switch for the particular axis it will cut the power to the motor. If you are probing the X axis find it’s limit switches(picture 8 & 9). On the PCB the switch wires should be close to the joystick(picture 7). There should be three wires for the 2 switches of the axis. One is +5V and the other two GND. When you find the corresponding switch wires turn the toy off and start probing the wires for continuity. Start with two wires and then press the limit switch for the corresponding axis from the top. If you loose the continuity that’s one of your switches. Label it as SWITCH_1 . Labeling things will make it much easier later. Now continue and find the opposite switch and label it as SWITCH_2.
Now repeat the process for the other axis labeling the wires SWITCH_3, SWITCH_4.
The last motor that controls the up-down motion of the grabbing hand doesn’t have a limit switch so you just have to find 2 wires of the motor.
For more detail: Arduino Candygrabber
This Post / Project can also be found using search terms:
- Arduino Candy Grabber
- arduino limit switch
- arduino switch
- i2c arduino data grabber