Arduino, Android, and Processing

From Matt Bilsky

Overview

This project arose out of needing a way to send and receive data on an Arduino from an Android phone. For projects, I find that it is easiest to write apps using Processing as it gets rid of a lot of the coding nessary to write apps in Eclipse. We're not building the next Candy Crush here. I had explored using the headphone jack, but found it very difficult to implement directly, at least in Processing. Newer Android devices (3.1+), whose hardware supports it, have USB host capabilities using a USB OTG (On The Go) adapter. Some searching turned up USB Serial for Android which has functions for opening and controlling serial ports on Android devices. Unfortunately it hadn't been ported over to Processing...until now! Below I have written up my process for getting everything working. It is necessary to re-compile Android mode for Processing to allow it to use a newer version of Android Build Tools (I used version 19).

Re-Compiling Processing

Currently Processing Android mode only supports Android 2.3.3 (API 10). This is hard-coded so we need to update the source code with the version greater than 3.1 (API 12). This explanation assumes you have successfully installed Android mode for Processing in the past and have setup the SDK software accordingly.

First I followed the instructions here to get my Windows 7 system ready to compile. This includes installing JRE 7 and Apache Ant. I chose to install GitHub desktop to my computer to sync the Processing source (it'll also come in handy later).

One of the most important steps when installing the above is correctly setting the Environment Variables. You can access them from the System Properties window (sysdm.cpl from the run prompt). Under "Advanced" select Environment Variables. We need to set the following User Variabls:

  • ANDROID_SDK to C:\Users\<Your Account Name>\AppData\Local\Android\android-sdk\
  • ANT_HOME to C:\apache-ant-1.9.3\ (or wherever you extracted ANT)
  • JAVA_HOME to C:\Program Files\Java\jdk1.7.0_45 (the location of your Java install)

Under the Path variable add: %ANT_HOME%/bin;%JAVA_HOME%\bin (be sure to put a ; between existing and new entries). I also created a System Variable for ANDROID_SDK but can't remember if it is necessary or not.

It is also necessary to download the Processing-Android source from Git so we can modify it to use a newer version of the Android SDK.

As for the SDK I went with:

  • API 19 (Android 4.4.2)
  • Android SDK Tools: 22.6.3
  • Android SDK Platform-tools: 19.0.1
  • Android SDK Build-tools: 19.0.1

One mistake I made was installing the newer packages while running an ANT build. This caused problems and forced me to re-install the SDK packages. See more on that here.

Now we get to the fun part...

Located the source files for Android Mode that you downloaded using Git (for me they were locating in My Docs\GitHub\. We need to modify 2 files: \core\build.xml and \src\processing\mode\android\AndroidBuild.java.

  • build.xml: find and replace references to android-12 with your chosen version(such as: {env.ANDROID_SDK}/platforms/android-19/android.jar)
  • AndroidBuild.java: Set the following to your version
    • static final String sdkName = "4.4.2";
    • static final String sdkVersion = "19";

Once we have changed the files its time to compile using ANT. First change to the core directory and run "ant build". With the core built, go up one level and build the whole package again using "ant build". After successfully building use "ant dist" to create a zip file that can be used to update Processing. This will create a file AndroidMode.zip in the \release folder.

Simply replace the existing AndroidMode folder in your Processing mode folder (by default, located in My Docs\Processing\mode).

Installing Serial Library

Hardware Setup

Code Examples and Testing