Firmata protocol implementation on Android Java
Firmata is a protocol to controll Arduino from Application on Android Phone. You can write Arduino-code in Android Java.
Install sample application.
<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>
import org.shokai.firmata.ArduinoFirmata;
ArduinoFirmata arduino = new ArduinoFirmata();
try{
arduino.connect();
}
catch(IOException e){
e.printStackTrace();
}
arduino.digitalWrite(13, true); // on board LED
arduino.digitalWrite(13, false);
arduino.pinMode(7, ArduinoFirmata.INPUT);
boolean status = arduino.digitalRead(7); // => true/false
arduino.analogWrite(11, 230); // pinNumber, value(0~255)
Digital 11 PIN -> LED -> GND
int value = arduino.analogRead(0); // => 0 ~ 1023
GND & 5V -> CdS & 10kΩ -> Analog 0 PIN
arduino.servoWrite(9, 135); // pinNumber, angle(0~180)
arduino.close();
import org.shokai.firmata.ArduinoFirmataEventHandler;
arduino.setEventHandler(
new ArduinoFirmataEventHandler(){
public void onError(String errorMessage){
Log.e("ArduinoFirmata App", errorMessage);
}
public void onClose(){
Log.v("ArduinoFirmata App", "arduino closed");
}
}
);