Android Things — LED control via MQTT

Abhinav Tyagi
3 min readJan 10, 2017

--

MQTT Message Queue Telemetry Transport, is a popular protocol similar to HTTP HyperText Transfer Protocol, for communication between clients and servers. MQTT is more favored and suitable for IoT application because of its small header footprint and reliability. MQTT uses very small bytes of data to describe the content of the information being transported. It’s also very easy to integrate into different projects and there are a lot of libraries readily available for developing client and broker (server in simple terms of MQTT) code.

In this demo, I will be using one such open source library PAHO from Eclipse. Since Android Things extends the core Android framework with additional APIs, we can use same Java library from PAHO to make our MQTT client on a device running Android Things, which is, in our case Raspberry Pi3.

Once you have set your RPi with Android Things and set your Android Studio, we can start coding for out demo.

First, we set our Broker on a machine. I am using Mosquitto for Macbook for setting the Broker. With Brew already installed, type the following command on the terminal to install Mosquitto:

brew install mosquitto

The script finishes by providing startup instructions:

ln -sfv /usr/local/opt/mosquitto/*.plist ~/Library/LaunchAgents

launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist

That’s it. You can now test your installation by running following commands on two different tabs or terminal windows:

mosquitto_sub -t topic/state

mosquitto_pub -t topic/state -m “Hello World”

Second, we will integrate PAHO Java library to Android Things application. In the Android Studio project, we will add PAHO library to our project.

  1. Add the following repository path to build.gradle of the project
repositories {
jcenter()
maven {
url "https://repo.eclipse.org/content/repositories/paho-snapshots/"
}
}

2. Add the PAHO dependency to the build.gradle of the app

dependencies {
provided 'com.google.android.things:androidthings:0.1-devpreview'
compile 'org.eclipse.paho:org.eclipse.paho.client.mqttv3:1.0.2'
}

3. Make your activity implement to “MqttCallback” and add the following code in the onCreate() method of the Activity class

try {
MqttClient client = new MqttClient("tcp://192.168.1.7:1883", "AndroidThingSub", new MemoryPersistence());
client.setCallback(this);
client.connect();

String topic = "topic/led";
client.subscribe(topic);

} catch (MqttException e) {
e.printStackTrace();
}

Here 192.168.1.7 is the local IP address of my Macbook which is acting as the MQTT Broker. 1883 is the port at which Broker is listening. The client has subscribed to topic named “topic/led

4. Declare member variable

public static final String LED_PIN = "BCM22"; //physical pin #15
private Gpio ledPin;

Add the following code in the onCreate() method to initialize your GPIO pin and configure it as output pin:

PeripheralManagerService service = new PeripheralManagerService();
try {
// Create GPIO connection for LED.
ledPin = service.openGpio(LED_PIN);
// Configure as an output.
ledPin.setDirection(Gpio.DIRECTION_OUT_INITIALLY_LOW);
} catch (IOException e) {
Log.e(TAG, "Error on PeripheralIO API", e);
}

5. MqttCallback class will ask you to implement three methods. Add the following code to these methods to receive the input message and based on the data received, change the state of the GPIO pin.

@Override
public void connectionLost(Throwable cause) {
Log.d(TAG, "connectionLost....");
}

@Override
public void messageArrived(String topic, MqttMessage message) throws Exception {
String payload = new String(message.getPayload());
Log.d(TAG, payload);
switch (payload) {
case "ON":
Log.d(TAG, "LED ON");
ledPin.setValue(true);
break;
case "OFF":
Log.d(TAG, "LED OFF");
ledPin.setValue(false);
break;
default:
Log.d(TAG, "Message not supported!");
break;
}
}

@Override
public void deliveryComplete(IMqttDeliveryToken token) {
Log.d(TAG, "deliveryComplete....");
}

6. Now, set the breadboard and RPi3 with LED as per the image below:

powered by Fritzing

7. Connect the RPi3 with Android Studio by running the ADB command on terminal:

adb connect Android.local

and press Run.

8. Now, on your terminal, type following commands to control the state of the LED:

mosquitto_pub -h 127.0.0.1 -t topic/led -m "ON"
mosquitto_pub -h 127.0.0.1 -t topic/led -m "OFF"

As command suggest, ON will turn ON the LED and OFF will turn it OFF. The IP address 127.0.0.1 is for the MQTT Broker that is running on the Macbook.

The complete project can be found here on my GitHub repository.

--

--

Abhinav Tyagi

{ “designation”:”Principal Consultant”, “hobby”:[ “click”, “cook”, “colour“, “code”,”DIY” ], “interest”:[ “android”,”bots”,”IoT”, “food”, “future”, “space” ] }