Home

Awesome

Android-Firmata

Build Status Download 中文文档

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.

<br/>

<sup>:see_no_evil: WALL·E and my GUINEA PIG :hear_no_evil:</sup>

Benefits

Installation

In your build.gradle:

dependencies {
    implementation 'com.xujiaao.android:android-firmata:${android_firmata_version}'
}

Download

Get Started

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.

Guidance

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:


Connect via Bluetooth

Requirements

Setup the Bluetooth Serial Port Module

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.

Android Programing

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:

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), ...)

Connect via WiFi

Requirements

Setup NodeMcu

Check out the NodeMcu Guide to learn about how to install StandardFirmataWiFi on the board.

Android Programing

For WiFi Connection, the Transport URI is:

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), ...)

Connect via USB

Requirements

Android Programing

For USB Connection, the Transport URI is:

Note: USB Transport is based on FelHR85's UsbSerial Library.

Documentation

Transports

Android-Firmata Library uses a Transport URI to identify how devices are being connected:

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.

Usage

If you want to run the Sample Application on Android Things, try Vysor.

TODOs

License

Android-Firmata is distributed under the terms of the MIT License. See the LICENSE file.