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.

3 comments:

  1. I am reading your article and admire that you posted article which gives users useful information regarding particular topic.

    free cracked software download

    ReplyDelete