Skip to content

shokai/ArduinoFirmata-Android

Repository files navigation

ArduinoFirmata on Android

Arduino Firmata protocol (http://firmata.org) implementation on Android Java.

What is Firmata?

Firmata is a protocol to controll Arduino from Application on Android Phone. You can write Arduino-code in Android Java.

REQUIREMENTS:

SETUP:

  1. Copy arduino-firmata.jar and usb-serial-for-android.jar to "libs" directory
  2. Copy device_filter.xml to "res/xml" directory
  3. edit AndroidManifest.xml
<activity android:name="MainActivity" />
  <intent-filter>
    <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>

SYNOPSIS:

Setup

Connect

import org.shokai.firmata.ArduinoFirmata;

ArduinoFirmata arduino = new ArduinoFirmata();
try{
  arduino.connect();
}
catch(IOException e){
  e.printStackTrace();
}

Close

arduino.close();

I/O

Digital Write

arduino.digitalWrite(13, true); // on board LED
arduino.digitalWrite(13, false);

Digital Read

arduino.pinMode(7, ArduinoFirmata.INPUT);
boolean status = arduino.digitalRead(7);  // => true/false

Analog Write (PWM)

arduino.analogWrite(11, 230); // pinNumber, value(0~255)

Analog Read

int value = arduino.analogRead(0);  // => 0 ~ 1023

Servo Motor

arduino.servoWrite(9, 135); // pinNumber, angle(0~180)

Events

Event Handler

arduino.setEventHandler(
  new ArduinoFirmataEventHandler(){
    public void onError(String errorMessage){
      Log.e("ArduinoFirmata App", errorMessage);
    }
    public void onClose(){
      Log.v("ArduinoFirmata App", "arduino closed");
    }
  }
);

Data Handler

arduino.setDataHandler(
  new ArduinoFirmataDataHandler(){
    public void onSysex(byte command, byte[] data){
    }
  }
);

Sysex

Send

byte[] data = {13, 5, 2};
arduino.sysex((byte)0x01, data);  // command, data

Regist Receive Event

arduino.setDataHandler(
  new ArduinoFirmataDataHandler(){
    public void onSysex(byte command, byte[] data){
      Log.v("sysex command", new Integer(command).toString());
      String s = "";
      for(int i = 0; i < data.length; i++){
        s += new Integer(data[i]).toString() + ",";
      }
      Log.v("sysex data", s);
    }
  }
);

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Build arduino-firmata.jar

set Android SDK Path

% export CLASSPATH=$CLASSPATH:/usr/local/var/lib/android-sdk/platforms/android-13/android.jar
% export LC_ALL=en

build jar

% make init
% make

=> arduino-firmata.jar

About

Arduino Firmata protocol implementation on Android Java.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages