Wednesday, March 23, 2011

Chumby Widget Control of Arduino

So here is a proof-of-concept of using a Chumby Widget to send commands using XMLsockets to a Python program running on the Chumby, which in turns controls the Arduino over USB serial using Firmata. Note the LED on pin 13 turning on and off when I hit the orange rectangle on the widget:



Tuesday, March 22, 2011

xmlsockets in Chumby Widget

This took me a while to figure out. I don't think there are a lot of people using ActionScript xmlsockets in a Chumby widget to talk directly with the Chumby - they are usually hitting external websites.

If you want to talk to a python program running on the Chumby itself, you need something like:

from socket import *
s
=socket(AF_INET,SOCK_STREAM)
s.bind((
"localhost",6666))
s.listen(
1)
policyFile
= '<?xml version="1.0"?><!DOCTYPE cross-domain-policy SYSTEM "http://www.adobe.com/xml/dtds/cross-domain-policy.dtd"><!-- Policy file for xmlsocket://socks.mysite.com --><cross-domain-policy><allow-access-from domain="*" to-ports="6666" /></cross-domain-policy>'
while 1:
print "waiting for connection..."
conn,addr
=s.accept()
print "connection made"
data
=conn.recv(2000)
print "data: ",data
if("policy-file-request" in data):
conn.send(policyFile)
conn.close()
print "connection closed."

Note the weird XML. You to send that back to the Flash Player in response to a policy-file-request to assure the Player that you have the right to access that socket. After that, your ActionScript can talk freely using xmlsockets with the python program running on the Chumby.

Saturday, March 19, 2011

My First Chumby Widget

So here is my first Chumby Widget:


This is some pretty simple Actionscript. Now, to try to use XML sockets, this Actionscript appears to work:

var xmlSocket:XMLSocket=new XMLSocket();
xmlSocket.onConnect=function() {
xmlSocket.send(new XML("Hello, World!"));
}
xmlSocket.onXML=function(myXML) {
trace(myXML.firstChild.childNodes[2].firstChild.nodeValue);
xmlSocket.close();
}
xmlSocket.connect("localhost",6666);

And on the python side:

>>> from socket import *
>>> s=socket(AF_INET,SOCK_STREAM)
>>> s.bind(('localhost',6666))
>>> s.listen(1)
>>> conn,addr=s.accept()
>>> conn.recv(1024)
'Hello, World!\x00'

Now I just need to have the Chumby widget XML socket messages control Firmata transmissions to contol the Arduino.

Followup: Well, the Actionscript and Python worked fine on my laptop, but a Chumby Widget needs a proper "crossdomain.xml" served out for the pair to work...see the next post!

Monday, March 07, 2011

Firmatization

Firmata is based (mainly) on a three-byte MIDI-like message. If you read the protocol and have no background, it won't make any sense. Let's start with a digital output message.

import serial
# find your own usb serial port by "ls /dev" and note I changed the Firmata on my Arduino to use 9600 bps
ser=serial.Serial('/dev/ttyUSB0',9600,timeout=1)
# 0x91 is the digital write message (0x90) + "port" 1 (on larger board, there might be more ports), the second two bytes are a bitmask of the digital I/O pins in terms of if they should be on or off
#
turn on all digital outputs
ser.write(chr(0x91)+chr(0x7F)+chr(0x7F))
# turn off all digital outputs
ser.write(chr(0x91)+chr(0x00)+chr(0x00))
If that isn't exciting enough, let's enable this over a TCP socket using tcp_serial_redirect.py. One on terminal:

python ./tcp_serial_redirect.py -p /dev/tty.usbserial-A4000QBg -P 6666
--- TCP/IP to Serial redirector --- type Ctrl-C / BREAK to quit
--- /dev/tty.usbserial-A4000QBg 9600,8,N,1 ---
Waiting for connection on 6666...

On the other:

from socket import *
s=socket(AF_INET, SOCK_STREAM)
s.connect(('127.0.0.1',6666))

And you know it is working because terminal 1 will say:

Connected by ('127.0.0.1', 50961)

Then on terminal 2, you can turn the Arduino pin 13 LED on/off with:

s.send(chr(0x91)+chr(0x20)+chr(0))
s.send(chr(0x91)+chr(0x00)+chr(0))

(I should note that for now on I am using BlogTrog CodeWindow for my Python)


Chumbuino and Firmata

My scheme to establish a simple method for web-enabled / widget-enabled control of an Arduino from a Chumby (aka "Chumbuino") moves forward. Theron Trowbridge suggested the use of Firmata, a MIDI-like protocol, as a standard serial control protocol between the Chumby and the Arduino. I grabbed PDuino (Firmata for PD) and Pyduino (Firmata for Python), loaded up "OldStandardFirmata" on my aging Arduino NG, and was able to get the Pin 13 LED to flip on and off reliably through both.

Now I need to 1) create a Python program to hit a web service to implement Firmata and 2) then create the web service using Google App Engine

Follow-on projects include 3) socket-to-serial gateway in Python and 4) tweak Pduino to output to a socket and 5) then xmlsocket-to-serial gateway in Python with 6) Chumby Widget with xmlsockets to control Arduino.

[it turns out the Chumby widget might be easier than I think, as there already is a Flash Arduino Firmata controller.]