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));
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);


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. 

29 comments:

  1. Please continue this great work and I look forward to more of your awesome blog posts. UC mini app

    ReplyDelete
  2. Your work is very good and I appreciate you and hopping for some more informative posts upload to instagram from pc

    ReplyDelete
  3. That is really amazing. Thank author for it. it is rare articles. instagram online web viewer

    ReplyDelete
  4. I take pleasure in the way you’ve worded the materials within this publishing. I am just rather thrilled about this information and in addition you’ve made it possible to store of which exciting. instagram online web viewer

    ReplyDelete
  5. The site has excellent content, if you feel promoting the site would help, feel free to contact us. https://www.yttomp3.org

    ReplyDelete
  6. Great Information sharing .. I am very happy to read this article .. thanks for giving us go through info.Fantastic nice. I appreciate this post. ACMarket

    ReplyDelete
  7. The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface. Action Link Wireless

    ReplyDelete
  8. Thank you so much for sharing this great blog.Very inspiring and helpful too.Hope you continue to share more of your ideas.I will definitely love to read. Snapchat++

    ReplyDelete
  9. I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information. AirShou iOS 11

    ReplyDelete
  10. I know your expertise on this. I must say we should have an online discussion on this. Writing only comments will close the discussion straight away! And will restrict the benefits from this information. iFile Download

    ReplyDelete
  11. Most of the time I don’t make comments on websites, but I'd like to say that this article really forced me to do so. Really nice post! Download Cydia iOS 11.3

    ReplyDelete
  12. Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. iOS 11.3.1 Download

    ReplyDelete
  13. I really thank you for the valuable info on this great subject and look forward to more great posts. Thanks a lot for enjoying this beauty article with me. I am appreciating it very much! Looking forward to another great article. Good luck to the author! All the best! iphone screen recorder

    ReplyDelete
  14. Its a great pleasure reading your post.Its full of information I am looking for and I love to post a comment that "The content of your post is awesome" Great work. sword man monster hunter hack

    ReplyDelete
  15. Wow, cool post. I’d like to write like this too – taking time and real hard work to make a great article… but I put things off too much and never seem to get started. Thanks though. Visit our website

    ReplyDelete
  16. Thank you for helping people get the information they need. Great stuff as usual. Keep up the great work!!! download 9apps

    ReplyDelete
  17. Thank you very much. I think a lot of people are looking for such a content from you. hope you will have more share such like that.
    - Lenny Face

    ReplyDelete
  18. I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people. top android emulators

    ReplyDelete
  19. I like this post,And I guess that they having fun to read this post,they shall take a good site to make a information,thanks for sharing it to me. ipad cases supplier

    ReplyDelete
  20. The reason for this is the Nexus comes preloaded with Gingerbread (Android 2.3 Operating System - the latest to date) and has 4G connectivity. gerald winata gozali

    ReplyDelete
  21. Wonderful blog post and certainly will help with becoming familiar with the subject better. Visit us on our website!

    ReplyDelete
  22. Wow what a Great Information about World Day its very nice informative post. thanks for the post
    Emus4u

    ReplyDelete
  23. In the past peoples were not able to measure the temperature and other environmental conditions. From few decades technology has developed and peoples are able to do work with high accuracy and faster with internet of things (IoT). Thank you for sharing this amazing blog.

    ReplyDelete
  24. this is really nice to read..informative post is very good to read..thanks a lot! Please visit tiktok downloader to see more information

    ReplyDelete
  25. In year 2020, How to Create a website without knowing anything about web disigning and web development or any type of codings. Whether you want to create a website for your company business or for your own you can create it by doing right things. These 5 easy steps for beginners will help you to create a website without spending your money.
    How to create a website

    ReplyDelete
  26. Get huge discount on Home and Kitchen Appliances, Split and Window Air Conditioner, Mobiles & Laptops online , Television, Speakers & more electronics at best price.
    led tv

    ReplyDelete