IoT Library for Android Developers. Inspired by Johnny-Five.
Android-Firmata is a client library of Firmata written in Kotlin. It allows controlling Arduino (or other boards, such as NodeMcu...) which runs Firmata Protocol from your Android Application.
🙈 WALL·E and my GUINEA PIG 🙉
-
Easy Peripherals with Kotlin Programming Language
-
Easy Remote Controling with Android UI Components and Android Animator API
-
Compatible with Android Things (Control Arduino Boards form Android Things)
In your build.gradle:
dependencies {
implementation 'com.xujiaao.android:android-firmata:${android_firmata_version}'
}
The ubiquitous "Hello World" program of the microcontroller is "Blink an LED". The following code demonstrates how this is done using the Android-Firmata Library.
class GetStartedActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
/**
* Connect board through Bluetooth transport.
*
* NOTICE: Make sure the name of Bluetooth device is "HC-06",
* and the device has already been bonded with your Android phone!!!
*/
connectBoardWithLifecycle("bt://HC-06".toTransport(this), lifecycle, {
onConnecting { toast("Connecting...") }
onConnected { board ->
toast("Connected")
val led = board.Led(13) // Create an Led on pin 13
led.blink(500) // Blink every half second
}
onDisconnected { error ->
if (error != null) {
toast("Disconnected: ${error.message}")
}
}
})
}
}
Note: The image above is copied from Johnny-Five.
Before programing with the Android-Firmata Library, you need to select in which way your Android Device and the Arduino Board being connected.
Currently, these communication modes are supported:
-
StandardFirmataPlus v2.5.0 or greater
- Arduino IDE > Examples > Firmata > StandardFirmataPlus
-
A Bluetooth Serial Port Module (such as HC-05, HC-06):
Since Firmata runs at 57600
baud, you'll need to configure the module
before making a connection with Android-Firmata.
Check out the Johnny-Five Bluetooth Guide for more information.
Update the Transport URI in your Android Application to let it know which device should be connected.
For Bluetooth Connection, the URI can be either of:
-
bt://<bluetooth_name>
(Matches the Bluetooth Name)- Make sure the Bluetooth device has been bonded before connecting
-
bt://<bluetooth_mac_address>
(Matches the Bluetooth Mac Address)- Please replace the
:
in your Mac Address with.
or-
(e.g.bt://00.11.22.33.44.55
)
- Please replace the
-
bt://<bluetooth_mac_address>?pin=<pin>
(For Android Things, Pairs with the Pin)- Please replace the
:
in your Mac Address with.
or-
(e.g.bt://00.11.22.33.44.55?pin=1234
)
- Please replace the
For example:
/**
* If the name of your Bluetooth device is "HC-06", then the URI should be:
*
* "bt://HC-06"
*/
connectBoard("bt://HC-06".toTransport(context), ...)
-
StandardFirmataWiFi v2.5.0 or greater
- Arduino IDE > Examples > Firmata > StandardFirmataWiFi
-
A NodeMcu (ESP8266) Board
Check out the NodeMcu Guide to learn about how to install StandardFirmataWiFi on the board.
For WiFi Connection, the Transport URI is:
tcp://<board_ip_address>:<board_port>
For example:
/**
* If the ip address is '192.168.4.1', and the port is '3030', then the URI should be:
*
* "tcp://192.168.4.1"
*/
connectBoard("tcp://192.168.4.1".toTransport(context), ...)
-
StandardFirmataPlus v2.5.0 or greater
- Arduino IDE > Examples > Firmata > StandardFirmataPlus
-
An Android Phone which supports OTG
For USB Connection, the Transport URI is:
-
usb
(Selects the First Supported USB Device) -
usb:/<device_name>
(Selects the USB Device with the Specified Device Name)- For example:
usb:/dev/bus/usb/001/006
- For example:
Note: USB Transport is based on FelHR85's UsbSerial Library.
Android-Firmata Library uses a Transport URI to identify how devices are being connected:
-
Bluetooth:
-
bt://<bluetooth_name>
(Matches the Bluetooth Name)- Make sure the Bluetooth device has been bonded before connecting
-
bt://<bluetooth_mac_address>
(Matches the Bluetooth Mac Address)- Please replace the
:
in your Mac Address with.
or-
(e.g.bt://00.11.22.33.44.55
)
- Please replace the
-
bt://<bluetooth_mac_address>?pin=<pin>
(For Android Things, Pairs with the Pin)- Please replace the
:
in your Mac Address with.
or-
(e.g.bt://00.11.22.33.44.55?pin=1234
)
- Please replace the
-
-
WiFi:
tcp://<board_ip_address>:<board_port>
-
USB:
-
usb
(Selects the First Supported USB Device) -
usb:/<device_name>
(Selects the USB Device with the Specified Device Name)- For example:
usb:/dev/bus/usb/001/006
- For example:
-
Sample Application (:link: Link)
Android-Firmata provides a Sample Application, it shows how to control peripheral devices with Android-Firmata.
Note: Images in the sample application are copied from Johnny-Five Examples Page.
-
Download and install the Sample Application on your Android Device.
-
Select a smaple
-
Follow the breadboard image in the Sample Page for wiring
-
Click the "Connect/Disconnect" button in the top right corner to connect your board
-
To edit the Transport URI, open the "Settings Menu" in the Home Page, select "Transport"
If you want to run the Sample Application on Android Things, try Vysor.
- Support BLE transport
Android-Firmata is distributed under the terms of the MIT License. See the LICENSE file.