Arduino Firmata protocol (http://firmata.org) implementation on Android Java.
Firmata is a protocol to controll Arduino from Application on Android Phone. You can write Arduino-code in Android Java.
- Arduino Standard Firmata v2.2
- Arduino IDE -> [File] -> [Examples] -> [Firmata] -> [StandardFirmata]
- Android SDK 13+ (Android OS 3.2+)
- usb-serial-for-android
- USB Host Cable
- Copy arduino-firmata.jar and usb-serial-for-android.jar to "libs" directory
- Copy device_filter.xml to "res/xml" directory
- 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>
Connect
import org.shokai.firmata.ArduinoFirmata;
ArduinoFirmata arduino = new ArduinoFirmata();
try{
arduino.connect();
}
catch(IOException e){
e.printStackTrace();
}
Close
arduino.close();
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)
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){
}
}
);
- http://firmata.org/wiki/V2.1ProtocolDetails#Sysex_Message_Format
- https://github.com/shokai/ArduinoFirmata-Android/tree/master/samples/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);
}
}
);
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
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