24 Nov 2015

IOT : Sensing & Controlling an Android Device through MQTT Messages

In two earlier posts, we had explored how to create (a) LightWatch - an IOT proof-of-concept  for an ambient light sensor based on Android and the XMPP chat protocol and (b) LightWatchMQ - which would essentially do the same thing but use the more popular MQTT protocol. In each case we were essentially trying to simulate an IOT system based on the Android platform that will use native (or external ) sensors attached to the Android device to communicate with a central "controller" machine. This machine could be a simple Android device with the XMPP or MQTT client or could be running a Python program that listens for messages, extracts data and stores the same in a SQL database.

In this application, we, in a sense close the loop! Not only does the Android device transmit data from its sensors but in term it can be controlled to perform a physical activity -- sound a bell or flash its LED light when "instructed" to do so by the central machine.

The sequence of events is as follows :
  1. A custom built app IOT7939 is started on the remote Android device. This measures the ambient light near the device and publishes two messages on the IOT79 topic of a publicly hosted MQTT webserver, eg http:/broker.hivemq.com:1883 or http://test.mosquitto.org:1883
  2. A central MQTT client subscribes to this topic, IOT79, using, for example a websockets client available at http://www.hivemq.com/demos/websocket-client/ or http://test.mosquitto.org/ws.html Both these clients can Publish or Subscribe to their respective brokers. However any third party MQTT client can also be used. Hence data from the Android machine is received here.
  3. If the central MQTT client wants to initiate an activity on the Android device, it publishes a message on the IOT39 topic. The remote Android machine has subscribed to this topic and listens for a message. When the message arrives, three things happen : 
    1. Another publishing activity is triggered on the Android App, as a kind of acknowledgment for the message received and also to transfer fresh sensor data.
    2. A bell mp3 file is played. If the message contains the world "siren" a different alarm mp3 file is played.
    3. The LED flash on the Android is activated for 5 seconds.
While the Publish and Subscribe activities are reasonable easy to build into the app, the flashing light constitutes a challenge. While there are many webposts that explain how to operate the flash on an Android device and even give the sample code,  almost all of them are based on the deprecated camera class of Android and samples with the new camera2 class are not easily available. What this means is that code that runs on one machine may not run on another. For example, the Flashlight code that was used in this application runs happily on my Motorola with Android 5.1 but did not run on my Micromax with Android 6!

Rather than breaking our head on this, what we did was to de-couple the LED-flashing code from the general MQTT Pub/Sub code and created two apps. The LED Flashlight app is a very simple app that "OnStart" switches on the LED, waits for 5 second, switches the LED off and then exits. This app can easily be replaced with any more rugged Flashlight app that has an inbuilt feature to turn-on itself during the start. This LED flashlight app can be called from the main App with a few lines of code, provided we know the package and the activity name.

    String ExternalAppPackage = "org.iothub.flashlight";
    String ExternalAppActivity = "org.iothub.flashlight.MainActivity";
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.setComponent(new ComponentName(ExternalAppPackage,ExternalAppActivity));

Another very interesting feature of this app is that each device on which this app is run, automatically gets its own id derived from the Android ID . This is Published. So messages Published by the central server can contain this Android ID and the app can be modified to parse subscribed message so that each machine ONLY responds to messages that contains its own Android ID. So in effect, the broadcast can be converted into a narrow-cast and target only specific machines.

This screen-shot shows the WebSockets client on the central machine that receives message on IOT79 topic and these messages contain the Android ID as the first field. Also note that when it Publishes on the IOT39 topic, the message is trapped on the Android app and published "back". Since the message can be trapped, it can be parsed and acted upon "intelligently" with a simple if-then-else construct.

In fact, any third party MQTT client can be used to subscribe to and publish messages on the IOT79, IOT39 topics as shown in this screen capture here

The entire code for the main MQTT Pub/Sub app IOT7939 and the auxiliary Flashlight app is available in GitHub code repository.

Android is one of the most widely used platforms in the world and it is very likely that The Smartphone and the Internet of Things will get very closely tied to each in the future. Hence it makes sense to learn and explore how the key features of IOT can be implemented with Android. This was the motivation for building this proof-of-concept. 

19 Nov 2015

LightWatchMQ : IOT with MQTT / PubSub and Android

The LightWatch application described in the previous post demonstrated a full cycle IOT application with an Android client picking up data from the built-in Android light sensor and then sending the data to a remote machine using an XMPP chat server as a message broker. However the Publish / Subscribe ( PubSub ) model of the MQTT message broker offers a potentially simpler and "cleaner" way of doing the same. One big advantage of the PubSub model is that that there is no need to create userid/passwords for each Android device that is being used as sensor / transmitter. This can become a bottleneck if the number of deployments is very large. 

Getting Started with MQTT on Android

There is no immediate need to download, install or otherwise implement an MQTT server. The easiest way is to use the free HiveMQ server available for testing applications. We also assume that you have a Linux based Android application development environment with Eclipse and the Android SDK already installed. If not, please read through this blog post on Android Programming with Eclipse.

To get started on the Android end, first clone this Git repository into your machine and then follow these instructions. If you are not familiar with Maven, take a deep breath and then go through this tutorial. Basically, you need to successfully execute just two steps, mvn clean install  for the two directories org.eclipse.paho.android.service and org.eclipse.paho.client.mqttv3 to create two critical jar files :  org.eclipse.paho.android.service-1.0.2.jar and org.eclipse.paho.client.mqttv3-1.0.2.jar. Then import the  org.eclipse.paho.android.service.sample application into Eclipse, drop in the two jar files into the lib and you should have a working MQTT client for Android that  will demonstrate all aspects of MQTT technology.

Building your own application

The sample application built in the previous step is full service MQTT client with lots of smart features but modifying it to meet the requirements of our IOT system is very complicated. A simpler sample is available at git dobermai / android-mqtt-push  where the Thermometer.java is a very useful sample to model our application on.

The LightWatchMQ application is basically a port of the original LightWatch2 application with the XMPP calls being replaced with corresponding MQTT calls. The current prototype also demonstrates all the essential components of an IOT system, namely
  • A physical sensor : In this case, the light sensor built into an Android device that measures the lux intensity of ambient light.
  • A "publish" function implemented with an Android App
  • A central message broking service : In this case the HiveMQ server that supports the MQTT pub/sub protocol.
  • Two "subscription" options : implemented through (a) WebSocket Client and (b) a Python listener program that reads data and pushes it into a SQLite database for future processing.

The entire code of the application, including the MQTT jars is available in the GitHub repository LightWatchMQ, as an Eclipse project export and can be downloaded and imported directly into the Eclipse workspace. If the Android SDK has been set up properly, it should compile and execute on any Android machine higher than 4.4.

In addition to the replacement of the XMPP calls with MQTT calls, there are few differences from the original LightWatch2 application and these are as follows :
  • Since there is no userid/password in the PubSub model of MQTT, we are using the Android_ID of the Android device to identify the source of data. This is being published into the broker and is available to the subscriber applications.
  • The two buttons for Start, Stop have been replaced by one single button that changes cleverly changes functionality and behave either as Start or as a Stop button depending on whether the Service is Stopped or already Running. This clever piece of code taken from  dobermai / android-mqtt-push.
  • There is an upper limit on the number of times data can be published by the app. This is more of a debugging feature designed to prevent run-away loops during the development process. In reality, this can be set to a very high number or deleted completely.

Testing the application

The application is designed to connect to the free MQTT broker available at http://broker.hivemq.com:1883 and publish on the topic iothub.

Once the app is loaded, the Publish activity can be started by pressing the Start Service button. To see the output, we login at HiveMQ websocket client and subscribe to the topic : iothub with Quality of Service set to 0 and see the data coming in :

On would notice that when the Android sensor device was covered with a piece of cloth, the lux value dropped from 74 to 13 and when the cloth was removed, the lux value went back to where it was. The Android_ID of the device is also being reported as the long alphanumeric string separated from the lux value with a semicolon.

The data can also be checked with a listener program written in Python, taken from the Paho sample library and then modified to store the data in a SQLite database.

Here again we see the Lux values reported by the client and we see that the intensity reported goes down ( to 2) when the device is covered by hand.

This test was carried out with the Android device [ A Motorola phone ] connected to the WiFi, but it has also been tested with Micromax phones and tablets working with a simple 2G data network. It works!

What have we achieved ?

We have shown how to build a simple Android app that can integrate an Android device into an IOT system. We demonstrate the use of the Light Sensor, but other sensors available on Android can be used as well, to measure the intensity of ambient light and "publish" the information into an MQTT broker. This information can be downloaded by any "subscriber" and displayed on a screen, stored in a database or processed further.

MQTT is one of the most popular protocols used in the IOT environment and Android devices with sensors are widely available. Using this combination helps us to get started with simple IOT projects without too much trouble.