30 Oct 2015

Lightwatch : An IOT prototype using XMPP and Android

In two earlier posts, we have shown how to build (a) an XMPP chat client in Python that can listen for incoming messages and, where applicable, store the data in an SQLite database and (b) a XMPP chat client for Android that can transmit data from an Android device. With these two components we are now in a position to build a realistic prototype of an IOT system that will continuously transmit information about ambient light conditions from an Android device.

This prototype will demonstrate all the essential components of an IOT system namely
  • A  physical sensor - in this the light sensor built into an Android device that measures the lux intensity of ambient light
  • A central message broking service -- in this case an Ignite Openfire server that runs an XMPP protocol based messaging service. This is commonly known as an Instant Messaging or Chat server. XMPP is one of the standard protocols used in IOT.
  • A way to securely pass information across multiple devices. Security is established through the userid and password of the logged in users
  • A SQL compliant RDBMS that receives the light intensity data and stores it for future processing.
[ What is IOT ? Ooops ... sorry, please check out this or this :-) to understand why we are doing any of this ! ]

The overall architecture is as follows :

Any Android device has a set of physical sensors, that can be accessed from a program and these can be identified through any standard utility app, like Sensor Box. In this example, we build a sensor app that accesses the light sensor, one of the most widely available sensors, to determine the level of ambient light near the device.

The sensor app opens an XMPP chat dialogue with a controller device, which is any device that supports a generic XMPP chat client, and waits for a signal to transmit data. Once this signal is received, the sensor app opens a second XMPP chat dialogue with the Python listener and keeps sending ambient light data every 5 seconds, in the format that the listener will accept and store in the SQLite database. This continues until the controller device sends a hold signal, when the data transmission is paused. The transmission can be resumed or paused as and when required. Finally, when the controller sends a quit message, the sensor app shuts down.

To build this app, we broadly follow the instructions given in the earlier post on how to build an XMPP chat client for Android. In addition, the code to handle the light sensor is adapted from this page. In this case, the application is called LightWatch and the key files that need to be changed / modified are as follows 
  1. src/org/iothub/lightwatch/LightWatch.java
  2. res/layout/activity_light_watch.xml
  3. res/values/strings.xml
  4. res/drawable/iothub0.9.png
  5. AndroidManifest.xml
All these files are available in GitHub

To test out the application we need three Internet connected devices
  1. A Unix machine running the ChatIOT.py application as described in this post. This needs to be started with a XMPP userid / password [ referred to as the ReporterID in the LightWatch.java file]
  2. A controller device (Android, Unix, Windows whatever ) that has a chat client that is logged in with another XMPP userid / password [ referred to as the Controller ID]
  3. An Android device where the LightWatch application is installed. This application needs a third userid / password.
All three userids should be in the same XMPP server, which in our case is adastra.re and should be defined as each others chat "contacts". Otherwise messages will never be delivered or accepted.

If everything works well, the sensor app will send a message to the controller device announcing its presence. From the controller device, the message "xmit" needs to be sent back. This will initiate the transmission of a set of messages like "PUSH <lux value>" to the data store. The data store should accept these and store them in the SQLite database. If you cover the Android device with the LightWatch app with a piece of paper, you will notice a sharp fall in the lux value that is being transmitted. This was the humble goal of this application.

Everything has been tested on a Motorola phone and an Xioami tablet and it all works. 

One problem that happens is when the LightWatch gets paused, the flow of data stops! To overcome this, the app was redesigned with the bulk of the activity migrated into an Android Service. This is the LightWatch2 application that can be built with the following files -- that are available in Github. [ Actually, the entire project itself can be downloaded from here.]
  1. src/org/iothub/lightwatch/LightWatch2.java
  2. src/org/iothub/lightwatch/LightWatchService.java
  3. res/layout/activity_light_watch2.xml
  4. res/values/strings.xml
  5. res/drawable/iothub0.9.png
  6. AndroidManifest.xml
  7. res/drawable/pmbutton.xml
Both the applications work very similarly except that in the case of LightWatch2, the service has to started and stopped by pressing buttons on the main screen. After that the behaviour is similar to the LightWatch.

Building a trivial app for Android is easy but one that involves using XMPP libraries and accessing sensor data is a little more complicated. The initial effort required in setting up the process is significant, but once this done, the rest follows relatively smoothly. If you want to set up a small IOT system that is described here, follow the sequence of posts given in this blog.

  1. Build the python chat listener - chatiot.py
  2. Set up your Eclipse environment for Android
  3. Build a basic XMPP client app for Android
  4. Then do what is explained in this blog!
Where can this technology be used ?

One obvious application is in smart cities, where we can measure parameters like air pollution on a continuous basis using tools like Aircast as explained in this blog post.


  1. This is absolutely great Prithwis da! Shall keep watching out for more. ..Sanjay GT.

  2. I want to thank you for writing this article.This is great Article for me. It also more very informative & awesome. I expect more articles from you in future.
    Awesome information. Great Contribution to blog. I
    expect more articles from you in future. Keep it up!