Arduino, Flex 3 and XMLSocket class
As Adobe Developer I’m trying a lot of solutions to control external devices from my Flash, Flex and AIR applications but using Arduino Microcontroller (my favourite one in this period ; ) I got some issues and limitations.
In this article I have extended the ActionScript 3.0 XML Socket Class to send messages from Flex 3 to Arduino and viceversa and even if it works well for simple stuff it seems far from the final solution.
Naturally I have already tested Firmata 2.0, AS3 Glue Library and other stuff I have found around the web (you can read something about this topic in this article I wrote) but they have big limits with Servo and I think they are not ready to use in combination with LCD, Bluetooth, GPS and so on. I only used them to turn on/off digital and analog pins.
It’s nice but I was thinking about a custom solution. Probably it won’t be flexible like a standard library but enough for me to accomplish all the tasks I need.
So, this simple Flex application (usable on Flash CS3/4 too) allows me to turn On/Off two Leds too, connected to pin 2 and 13 but if you read the whole article you will know that i’m not so happy about this solution.
I hope to get inspiration from this and you will help me to get new ideas (please write me in private emails or post a comment).
This is the actionscript 3.0 class:
package arduino{ import flash.display.Sprite; import flash.events.*; import flash.net.XMLSocket; public class FBXMLArduino extends Sprite { private var hostName:String = "127.0.0.1"; private var port:uint = 5335; private var socket:XMLSocket; public function FBXMLArduino() { socket = new XMLSocket(); configureListeners(socket); if (hostName && port) { socket.connect(hostName, port); } } public function send(data:Object):void { socket.send(data); } private function configureListeners(dispatcher:IEventDispatcher):void { dispatcher.addEventListener(Event.CLOSE, closeHandler); dispatcher.addEventListener(Event.CONNECT, connectHandler); dispatcher.addEventListener(DataEvent.DATA, dataHandler); dispatcher.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler); dispatcher.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); } private function dataHandler(event:DataEvent):void { trace("From Arduino --->: " + event.data); } private function closeHandler(event:Event):void { trace("closeHandler: " + event); } private function connectHandler(event:Event):void { trace("connectHandler: " + event); } private function ioErrorHandler(event:IOErrorEvent):void { trace("ioErrorHandler: " + event); } private function progressHandler(event:ProgressEvent):void { trace("progressHandler loaded:" + event.bytesLoaded + " total: " + event.bytesTotal); } private function securityErrorHandler(event:SecurityErrorEvent):void { trace("securityErrorHandler: " + event); } } }
This is the Flex MXML Application file:
<?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="init()"> <mx:Script> <![CDATA[ import arduino.FBXMLArduino; private var socket:FBXMLArduino; private function init():void { socket = new FBXMLArduino(); } ]]> </mx:Script> <mx:Button label="Disable LEDS" click="socket.send(1);" /> <mx:Button label="Turn On LED 2" click="socket.send(2);" /> <mx:Button label="Turn On LED 13" click="socket.send(3);" /> </mx:Application>
Following the Arduino Sketch:
char message ; void setup(){ Serial.begin(9600); //setup serial conversation at 19200 bauds pinMode(2, OUTPUT); // sets the digital pins 2 as output } void loop () { // checks if a serial message has arrived to the board if(Serial.available()) { // Get data from Flex message = Serial.read(); // Check ASCII TABLE // http://web.cs.mun.ca/~michael/c/ascii-table.html switch(message) { // Digit 2 case 50: digitalWrite(2, HIGH); // send this sentence to Flex Serial.println("Turn On LED2"); break; // Digit 3 case 51: digitalWrite(13, HIGH); break; case 49: digitalWrite(2, LOW); digitalWrite(13, LOW); break; } } delay(150); }
NOTE: remember that you need to use a serial proxy application to allow communication between Arduino and Flash.
You can get more info about it on Arduino or in the AS3 glue official web sites.
CONSIDERATIONS:
This script works well but following some considerations:
1) To get results in the Arduino code, I would like to use decimal values and not ascii code but i’m not able to make a comparison like:
if (message == 2) and i have to use its ascii value (50). Suggestions?
2) If i send a number > 9 or a string with more than 2 chars I’m not able to get an intere value but it’s splitted.
For example, if i send 134 i will get “1″, after 150ms, “2″ and after 300ms the last value, “3″ so I’m not able to get 134.
Naturally I think there could be workarounds to merge these values but I also think there could be best way to do it.
I’m not so inside electronic stuff and neither inside C language.
So i have shared this script hoping that somebody will give me some suggestions.











For the first problem i think you can do:
switch(message) {
// Digit 2
case ‘2′:
For the second problem:
buf char*;
while (Serial.available>0){
buf[i++]=Serial.read();
}
so buf will contains all chars read.
wow thank you for posting this, i’ll be using as a reference plus
maybe if I figure out I can help too.
Leave your response!
Archives
Categories
Blogs
3D ActionScript Adobe AIR Arduino Binding Bitmap Chat Chrome Cocomo ComboBox drawHighlightIndicator drawSelectionIndicator Effects Emitter FLARToolkit Flash Flex Font FullScreen Gmap Google Map HSlider HTTPService Infrared itemRenderer Joystick Loader Papervision Particle PHP rawChildren Servo singleton Skin Skinning Slider Snippet Repo Browser Static TextField TileList Tween URLLoader URLRequest VSlider
WP Cumulus Flash tag cloud by Roy Tanck requires Flash Player 9 or better.