Arduino Platform (Part II)

Here is the pdf of the slides of my online talk on "Arduino Platform Part II" for asDevs Download a PDF of my talk (3.5 MB).

The session was recorded and you can listen and view it here.

Any questions on my talk just comment below or email me.

Here are the sample programs I went through.

Flex code (using as3Glue) to turn on or off a LED connected to an Arduino (running standard Firmata).

view plain print about
1<?xml version="1.0" encoding="utf-8"?>
2<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
3
             xmlns:s="
library://ns.adobe.com/flex/spark"
4
             xmlns:mx="library://ns.adobe.com/flex/mx"
5
             creationComplete="
init()">
6    <s:layout>
7        <s:VerticalLayout />
8    </s:layout>
9    
10    <fx:Script>
11        <![CDATA[
12            import net.eriksjodin.arduino.events.ArduinoEvent;
13            import net.eriksjodin.arduino.Arduino;
14            
15            private var led:int = 3;
16            private var arduino:Arduino;
17            
18            private function init():void {
19                arduino = new Arduino();
20                 arduino.requestFirmwareVersion();
21                arduino.addEventListener(ArduinoEvent.FIRMWARE_VERSION, initArduino);
22            }
23            
24            private function initArduino(event:ArduinoEvent):void {
25                arduino.enableDigitalPinReporting();
26                arduino.setPinMode(led, Arduino.OUTPUT);
27                arduino.writeDigitalPin(led, Arduino.LOW);
28            }
29            
30            private function turnOn():void {
31                arduino.writeDigitalPin(led, Arduino.HIGH);
32            }
33            
34            private function turnOff():void {
35                arduino.writeDigitalPin(led, Arduino.LOW);
36            }            
37        ]]>
38    </fx:Script>
39    
40    <s:Button label="
Turn ON" click="turnOn()" />
41    <s:Button label="
Turn OFF" click="turnOff()" />    
42</s:Application>

Flex code (using as3Glue) to graph in real time a light sensor connected to an Arduino (running standard Firmata).

view plain print about
1<?xml version="1.0" encoding="utf-8"?>
2<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
3
             xmlns:s="
library://ns.adobe.com/flex/spark"
4
             xmlns:mx="library://ns.adobe.com/flex/mx"
5
             creationComplete="
init()">
6    <s:layout>
7        <s:VerticalLayout/>
8    </s:layout>
9
10    <fx:Script>
11        <![CDATA[
12            import mx.collections.ArrayCollection;
13            
14            import net.eriksjodin.arduino.Arduino;
15            import net.eriksjodin.arduino.events.ArduinoEvent;
16            
17            [Bindable]
18            private var lightData:ArrayCollection = new ArrayCollection();
19            
20            private var arduino:Arduino;
21            private var sampleno:int = 0;
22            private var timer:Timer = new Timer(50);
23
24            private function init():void
25            {
26                arduino = new Arduino();
27                arduino.requestFirmwareVersion();
28                arduino.addEventListener(ArduinoEvent.FIRMWARE_VERSION, initArduino);
29            }
30
31            private function initArduino(event:ArduinoEvent):void
32            {
33                arduino.setAnalogPinReporting(0, Arduino.ON);
34                timer.addEventListener(TimerEvent.TIMER, getData);
35                timer.start();
36            }
37
38            private function getData(event:TimerEvent):void
39            {
40                var value:Object = arduino.getAnalogData(0);
41                lightData.addItem({light:value, sample:sampleno++});
42            }
43        ]]>
44    </fx:Script>
45
46    <mx:LineChart showDataTips="true" dataProvider="{lightData}">
47        <mx:series>

48            <mx:LineSeries xField="sample" yField="light" form="curve" displayName="Light" />
49        </mx:series>

50    </mx:LineChart>

51        
52</s:Application>

Arduino web server code to return all analog inputs in XML.

view plain print about
1#include <Ethernet.h>
2#include <SPI.h>
3
4byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
5byte ip[] = { 192,168,1,50 };
6
7Server server(80);
8
9void setup()
10{
11 Ethernet.begin(mac, ip);
12 server.begin();
13}
14
15void loop()
16{
17 Client client = server.available();
18
19 if (client) {
20 // an http request ends with a blank line
21
boolean blankline = true;
22
23 while (client.connected())
24 {
25 if (client.available())
26 {
27 char c = client.read();
28 // if line is blank, the http request has ended, so we can send a reply
29
if (c == '\n' && blankline) {
30 // send a standard http response header
31
client.println("HTTP/1.1 200 OK");
32 client.println("Content-Type: text/xml");
33 client.println();
34
35 // output the value of each analog input pin
36
client.print("<values>");
37 for (int i = 0; i < 6; i++) {
38 client.print("<analog pin='");
39 client.print(i);
40 client.print("
' value='");
41 client.print(analogRead(i));
42 client.println("
' />");
43 }
44 client.print("</values>");
45 break;
46 }
47 // we're starting a new line
48
if (c == '\n' || c == '\r') {
49 blankline = true;
50 }
51 else {
52 blankline = false;
53 }
54 }
55 }
56
57 client.flush();
58 client.stop();
59 }
60}

Flex code to graph in real time a light sensor connected to an Arduino running the above web server.

view plain print about
1<?xml version="1.0" encoding="utf-8"?>
2<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
3
             xmlns:s="
library://ns.adobe.com/flex/spark"
4
             xmlns:mx="library://ns.adobe.com/flex/mx"
5
             creationComplete="
init()">
6    <s:layout>
7        <s:VerticalLayout />
8    </s:layout>
9    
10    <fx:Declarations>
11        <s:HTTPService id="
arduino" url="http://192.168.1.50" resultFormat="e4x" />
12
    </fx:Declarations>
13    
14    <fx:Script>
15        <![CDATA[
16            import mx.collections.ArrayCollection;
17            import mx.rpc.events.ResultEvent;
18            
19            [Bindable] private var values:ArrayCollection = new ArrayCollection();
20            
21            private var timer:Timer = new Timer(20);
22            private var time:int = 0;
23            
24            private function init():void {
25                arduino.addEventListener(ResultEvent.RESULT, onResult);
26                timer.addEventListener(TimerEvent.TIMER, onTimer);
27                timer.start();
28            }
29            
30            private function onTimer(event:TimerEvent):void {
31                arduino.send();
32            }
33
34            private function onResult(event:ResultEvent):void {
35                var data:XML = event.result as XML;
36                values.addItem({time:time++, value:data.analog[4].@value.toString()});
37            }
38        ]]>
39    </fx:Script>
40    
41    <mx:LineChart dataProvider="{values}">
42        <mx:horizontalAxis>
43            <mx:CategoryAxis categoryField="time"/>
44        </mx:horizontalAxis>
45        
46        <mx:series>
47            <mx:LineSeries yField="value" form="curve" displayName="Light"/>
48        </mx:series>
49    </mx:LineChart>
50
51</s:Application>

Arduino web server code to be able to turn on or off a digital output.

view plain print about
1#include <Ethernet.h>
2#include <SPI.h>
3
4byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
5byte ip[] = { 192,168,1,50 };
6
7Server server(80);
8
9void setup()
10{
11 for (int i = 2; i <= 10; i++) {
12 pinMode(i, OUTPUT);
13 }
14 Ethernet.begin(mac, ip);
15 server.begin();
16 Serial.begin(9600);
17 Serial.println("Started");
18}
19
20void loop()
21{
22 Client client = server.available();
23
24 if (client) {
25 // an http request ends with a blank line
26
boolean blankline = true;
27 boolean parseGet = true;
28 boolean error = false;
29 int count = 0;
30 int pin = -1;
31
32 while (client.connected())
33 {
34 if (client.available())
35 {
36 char c = client.read();
37
38 Serial.print("char: ");
39 Serial.println(c);
40
41 // if line is blank, the http request has ended
42
if (c == '\n' && blankline) {
43 parseGet = true;
44 count = 0;
45 pin = -1;
46 Serial.println("Got request");
47 }
48
49 // parse GET request
50
if (parseGet) {
51 // check request is in form GET ?d00=0
52
if (count == 0 && c != 'G'
53 || count == 1 && c != 'E'
54 || count == 2 && c != 'T'
55 || count == 3 && c != ' '
56 || count == 4 && c != '/'
57 || count == 5 && c != '?'
58 || count == 6 && c != 'd'
59 || count == 7 && (c < '0' || c > '9')
60 || count == 8 && (c < '0' || c > '9')
61 || count == 9 && c != '='
62 || count == 10 && (c !='0' || c !='1')
63 ) {
64 error = true;
65 parseGet = false;
66 }
67
68 if (count == 7) {
69 pin = (c-'0')*10;
70 }
71 if (count == 8) {
72 pin = pin + (c-'0');
73 }
74
75 // Only pins 2-10, pins 11,12 + 13 used by ethernet
76
if (count == 10) {
77 if (pin >= 2 && pin <= 10) {
78 // set digital pin on or off
79
int onoff = int(c - '0');
80 if (onoff == 0) {
81 Serial.print(pin);
82 Serial.println(" LOW");
83 digitalWrite(pin, LOW);
84 }
85 else {
86 Serial.print(pin);
87 Serial.println(" HIGH");
88 digitalWrite(pin, HIGH);
89 }
90
91 Serial.println("Sending 200 OK");
92 // send a standard http response header
93
client.println("HTTP/1.1 200 OK");
94 client.println("Content-Type: text/xml");
95 client.println();
96 client.println("<result>");
97 client.print("<digital pin='");
98 client.print(pin);
99 client.print("
' value='");
100 client.print(onoff);
101 client.println("
' />");
102 client.println("</result>");
103 break;
104 }
105 else {
106 error = true;
107 }
108 }
109
110 if (error) {
111 Serial.println("Sending 400 Bad Request");
112 client.println("HTTP/1.1 400 Bad Request");
113 client.println("Content-Type: text/html");
114 client.println();
115 break;
116 }
117 count++;
118 }
119
120 // we're starting a new line
121
if (c == '\n' || c == '\r') {
122 blankline = true;
123 }
124 else {
125 blankline = false;
126 }
127 }
128 }
129
130 Serial.println("Closing");
131 client.flush();
132 client.stop();
133 }
134}

Flex code to turn on or off a LED connected to an Arduino running the above web server.

view plain print about
1<?xml version="1.0" encoding="utf-8"?>
2<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
3
             xmlns:s="
library://ns.adobe.com/flex/spark"
4
             xmlns:mx="library://ns.adobe.com/flex/mx"
5
             creationComplete="
init()">
6    <s:layout>
7        <s:VerticalLayout />
8    </s:layout>
9    
10    <fx:Declarations>
11        <s:HTTPService id="
arduino" url="http://192.168.1.50" resultFormat="e4x" />
12
    </fx:Declarations>
13    
14    <fx:Script>
15        <![CDATA[
16            import mx.collections.ArrayCollection;
17            import mx.controls.Alert;
18            import mx.formatters.NumberFormatter;
19            import mx.rpc.events.ResultEvent;
20            
21            private function init():void {
22                arduino.addEventListener(ResultEvent.RESULT, onResult);
23            }
24
25            
26            private function turnLed(led:int, on:Boolean):void {
27                var ledNo:String = led.toString();
28                var data:Object = {};
29                
30                if (ledNo.length == 1) {
31                    ledNo = 'd0' + ledNo;
32                }
33                else {
34                    ledNo = 'd' + ledNo;
35                }
36                
37                
38                if (on) {
39                    data[ledNo] = 1;
40                }
41                else {
42                    data[ledNo] = 0;
43                }
44                
45                arduino.send(data);
46            }
47            
48            private function onResult(event:ResultEvent):void {
49                var data:XML = event.result as XML;
50                trace(data.toString());
51            }            
52
53        ]]>
54    </fx:Script>
55
56    <s:Button label="on" click="turnLed(3, true)" />
57    <s:Button label="off" click="turnLed(3, false)" />
58
59</s:Application>

Related Blog Entries

TweetBacks
Comments (Comment Moderation is enabled. Your comment will not appear until approved.)