Processing + Android + Arduino Tutorial
Do you want to use Processing to get your Android handset talking to you Arduino UNO? This post will detail how to do it. I decided to write this because it took me days of searching and scratching my head to get it working and I though somebody else may find it useful.
Some background first…
I was putting together a internet connected installation that listened to twitter and I was very surprised by the responsivness of the twitter stream API. This got me to thinking that this would be a simple robust way to remotely control almost anything. The problem is my preferred toolchain (Processing + Arduino) required a computer to run. I wondered if there was a way to make a cheap platform that didn’t require a full computer to run. The problem is the Arduino is quite limited by itself and it is expensive to add a wifi shield. The key to this platform is that it had the ability to connect wirelessly. It was around this time that I discovered that there was device coming out of China that was essentially a computer on a stick (MK802), they are marketed as Android powered media centres for your TV. They come with 1.5 GHz A10 processor, 1 gig of RAM, 2 USB ports, HDMI, wifi and cost about $50 (more about this device in a follow up post).
If I could connect this device to an Arduino and run Processing apps it would be a awesome inexpensive way to create a internet of things platform that I could integrate into a sculpture or installation. But before purchasing one of these devices I wanted to validate that Processing + Android + Arduino would actually work. After all it looked like you might need to purchase one of these to do this and I didn’t even know if it was possible to talk to the Arduino via Processing at all. I had a Android phone handy so I thought if I could get it working on the phone I could most likely get it working on a MK802.
This is what I was aware of going in…
- Processing can export apps to Android
- I would need an unusual connector to connect that Android to the Arduino
That was about it. What I didn’t know anything about was OTG, manifests and the other hardware related issues peculiar to Android. These would provide the greatest roadblock.
To get started you will need the following:
- Arduino Uno
- Newish Android (I’m using a Galaxy Nexus Android 4.2.1)
- Processing (It must be 2.0.3 or higher) https://processing.org/download/
- The Android SDK. You can find it here.
- An On The Go cable (OTG). micro USB male to USB female
- Download the processing-android-serial library and install it . https://github.com/inventit/processing-android-serial
Steps
1) Purchase an OTG (On The Go) cable
Essentially you have to trick the Android into thinking the Arduino is an accessory. To do that you need a special cable that will allow the android device act as a host and the Arduino as a slave.
- This will allow the Android to work as a host device
- It will power the Arduino
- More about OTG and how to make one: http://makezine.com/projects/usb-otg-cable/
2) Set up your Processing IDE for Android
You need to install the Android Development Kit to export Processing apps to Android.
Follow the instructions here: http://wiki.processing.org/w/Android
3) Install processing-android-serial in your library folder
To communicate over a serial connection (this is what you are doing over USB from the Android to the Arduino) you need to use the serial library. Unfortunately the built in Processing library has not been ported to Android. Luckily Inventit ported a Java library to work with Processing on Android.
4) Create a new Android project in Processing
Make sure you click that button on the top right corner to change mode from Java to Android.
5) Create device_filter.xml and a project AndroidManifest.xml
Now you need create a couple of files to let the Android device know two things… one that the Arduino is an accessory that can be used and two what should the Android device do when it is plugged in.
device_filter.xml
In your Processing project folder create a folder structure as follows:
res/xml/
Create a XML file with the following contents and save it into the xml directory with the file name “device_filter.xml”
<?xml version="1.0" encoding="utf-8"?> <resources> <usb-device vendor-id="9025"/> </resources>
Note: This gives the Android device permission to access this Arduino UNO. If you have a different device you would have to enter the vendor information specific to that device.
To figure out your vendor number on a Mac open System Information and look for USB. You will find the hex value for the VendorID (in this case 0x2341) you will need to convert it to decimal for device_filter.xml, you can convert from hex to decimal with this tool.
http://www.binaryhexconverter.com/hex-to-decimal-converter.
To find the hex value of your device on a Mac look at your system information app…
AndroidManifest.xml
Processing creates a manifest by default but the content needs to be replaced with the following…
Note: This allows Android to launch your application when plugged in to the Arduino.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="10" /> <application android:label="" android:icon="@drawable/icon" android:debuggable="true"> <activity android:name=""> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" /> </intent-filter> <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter" /> </activity> </application> </manifest>
6) Install this script on your Arduino
This is a script I found at Arduino.cc that reads data off the serial to drive a LED. (I would credit this but I can’t find the original)
/** * Reads commands coming from serial interface to drive an LED on/off * Also prints led status back */ #define LEDPIN 13 int state = LOW; char incomingByte = 0; void setup() { Serial.begin(9600); pinMode(LEDPIN, OUTPUT); } void loop() { // send data only when you receive data: if (Serial.available() > 0) { // we receive a char representing an integer. let's converto to int int incomingState = (Serial.read() == '1'); // say what you got: //Serial.print("I received: "); //Serial.println(incomingState, DEC); if(incomingState != state) { state = incomingState; digitalWrite(LEDPIN, state); // turns the LED on or off Serial.print("Setting LED as: "); Serial.println(state); } else { Serial.print("Doing nothing. LED already: "); Serial.println(state); } } }
7) Put your Android in developer mode
This will give you the ability to put an application on your device.
http://developer.android.com/tools/device.html
8) Compile the following Processing sketch
This is a basic script I cobbled together. It presents two buttons one to turn on the LED and one to turn it off.
Note: This will crash while your running Processing because it is attempting to communicate through the USB to your computer. Don’t worry this is expected behavior. Hit stop on Processing and unplug the device.
import com.yourinventit.processing.android.serial.*; import controlP5.*; // The serial port: Serial myPort; ControlP5 cp5; void setup() { background(150); orientation(PORTRAIT); // Fixes the screen size to portrait in Android size(displayWidth, displayHeight, P3D); // Sets it to fill the screen // Start Button stuff cp5 = new ControlP5(this); // create a new button with name 'ledOn' cp5.addButton("ledOn") .setValue(0) .setPosition(100,200) .setSize(200,60) .setLabel("turn LED on") ; // create a new button with name 'ledOff' cp5.addButton("ledOff") .setValue(0) .setPosition(100,300) .setSize(200,60) .setLabel("turn LED off") ; PFont pfont = createFont("Arial",20,false); // use true/false for smooth/no-smooth ControlFont font = new ControlFont(pfont,241); cp5.getController("ledOn") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(24) ; cp5.getController("ledOff") .getCaptionLabel() .setFont(font) .toUpperCase(false) .setSize(24) ; // End button stuff myPort = new Serial(this, Serial.list(this)[0], 9600); // Set up serial connection } void draw() { } public void ledOn(int theValue) { background(150); myPort.write('1'); // write 1 to the serial connection } public void ledOff(int theValue) { background(150); myPort.write('0'); // write 0 to the serial connection }
9) Connect the Arduino to Android
This step is very important if Android does not recognize the Arduino as an accessory this will not work.
Important: Do NOT power the Arduino from the adaptor… let the power come from the phone.
Important: If you have already run your application on the Android you must kill it completely. It is not enough to remove it from your running apps… you need to go into settings and stop it as well.
Wait a few seconds and if you’ve done everything right you should see a screen that asks you what application to open this device with… choose your android app.
Now you should be able to light the pin 13 LED on your Arduino from your Android device!
As you can see… now that we can send data to the Arduino we can do practically anything. Without too much work you should be able to read the accelerometer, gps etc. and then trigger an event on the Arduino.
The only drawback I can see with using this with your handset is power… you can’t charge the Android and power the Arduino.
My next post will detail how to get things working on the MK802.
——————————————–
Notes:
If you are having trouble connecting to your device you may want to try these commands in the terminal on your computer while connected to your handset…
adb devices // shows a list of android devices – if you don’t see your device try the next step
adb kill-server // this kills the server
adb start-server // This starts the server… look for devices again and you should see your device
adb help // adb is pretty awesome, to see a list of commands type help
Pingback: Getting a MK802 talking to your Arduino - Art + Science | Art + Science