+ All Categories
Home > Engineering > Android Implementation using MQTT Protocol

Android Implementation using MQTT Protocol

Date post: 20-Aug-2015
Category:
Upload: fatih-oezlue
View: 2,123 times
Download: 16 times
Share this document with a friend
Popular Tags:
18
IS 748 MOBILE PERVASIVE COMPUTING Midterm Project MQTT ANDROID APPLICATION IMPLEMENTATION Fatih Özlü 1777762 Information Systems Middle East Technical University 06.01.2013 [email protected]
Transcript
Page 1: Android Implementation using MQTT Protocol

IS 748 – MOBILE PERVASIVE COMPUTING

Midterm Project

MQTT ANDROID APPLICATION IMPLEMENTATION

Fatih Özlü 1777762

Information Systems

Middle East Technical University

06.01.2013

[email protected]

Page 2: Android Implementation using MQTT Protocol

2

TABLE OF CONTENTS 1. MQTT ANDROID APPLICATION ..................................................................................... 3

1.1. Overview ........................................................................................................... 3

1.2. Definitions, Abbreviations and Acronyms ...................................................................... 3

1.3. Design of the Application ......................................................................................... 3

1.4. Platform ............................................................................................................ 7

1.5. Testing the Application ........................................................................................... 8

2. TECHNOLOGIES USED IN THE PROJECT ......................................................................... 14

2.1. AChartEngine .................................................................................................... 14

2.2. MQTT from PAHO ............................................................................................... 14

2.3. ActiveMQ Broker Configuration ............................................................................... 17

3. REFERENCES ........................................................................................................ 18

Page 3: Android Implementation using MQTT Protocol

3

1. MQTT ANDROID APPLICATION

1.1. Overview

This document is prepared as a project for IS 748 lecture. The document is based on the

implementation project of the lecture. In this project, an Android application is implemented

by using MQTT for communication. It is thought to be client of a CEP (Complex Event

Processing) engine developed for IS 589 Master Project, which is based on collecting sensor

data from classroom environment and extracting complex events. This Android project is the

high-level application part of the Master Project to visualize sensor data and inform the

users.

1.2. Definitions, Abbreviations and Acronyms

CEP Complex Event Processing

JMS Java Messaging Service

M2M Machine-to-Machine

MQTT MQ Telemetry Transport

1.3. Design of the Application

In this implementation project, Android client will be informed about condition of a smart

classroom. The condition contains information like ambient temperature and light status,

motion detection, etc. Phone-Server communication will be based on MQTT (a topic based

publish-subscribe model). According to the proposed design, a mobile client could be able to

subscribe several pre-defined topics and will be notified whenever the server publishes a

message on any of the subscribed topics. Mobile application will use MQTT protocol for

communicating with the server (Figure 1).

In addition, mobile clients will show meaningful information rules defined in the CEP engine

to inform users to trigger events like opening air conditioner, or opening the windows in the

classroom as well as statistics about the server data that is collected from sensors. So the

application will subscribe predefined topics to be informed, whenever event occurs, the

message about the event will be published to the user. The user can be able to monitor

these messages/events.

Page 4: Android Implementation using MQTT Protocol

4

The application is implemented in Eclipse environment by using Java programming language

in Android platform. The application is composed from four packages each of which is

separated for a different purpose (Figure 2). graphics package is used for visual purposes

to show sensor data by using charts on time axis. The data package is used for storing the

sensor data on the application side to retrieve in case of needs from other packages. In

mqtt package, communication with the broker is implemented to get messages over a topic

based messaging system. client package is used for showing context information on a

tabbed structure by using Android Activity classes. This is the starting point of the

application and it contains the user interface tabs for making several operations.

publish /subscribe

publish /subscribe

sensor events

complex events

AC

TIV

EM

Q

BR

OK

ER

(S

ER

VER

)

Android MQTT Client

MQTT Protocol

Classroom Setups

Collecting sensor data

CE

P

En

gin

e

Figure 1 System design

Page 5: Android Implementation using MQTT Protocol

5

Figure 2 Package Diagram

1.3.1. client Package

In this package, main operations are managed by using Activity classes on Tabbed layout.

For each operation of the application, there are several Activity classes started from the

MainActivity class. MainActivity is the container class of the other Activity classes

Figure 3 client Package Class Diagram

Page 6: Android Implementation using MQTT Protocol

6

In addition, MainActivity takes published messages from MQTTConnectorTask class that

deals with receiving messages from subscribed messages from the broker. The MainActivity

forwards the relevant message to the active Activity class. By this way, active Activity class

can update the display to show new messages on the screen. Each derived concrete Activity

class is used for different purpose. DoorInActiviy, DoorOutActivity, WallTeacherActivity,

WallWindowActivity, WindowActiviy is used for demonstrating the classroom setups to

provide monitoring the sensors deployed to these places. SettingsActivity is responsible for

starting/stopping the application or exiting from the application and setting the broker

server url of the application, since the broker can be interchangeable with respect to needs.

GraphicsActivity is used for visualizing the sensor data on charts and graphics. It is designed

as extendable as possible; so the classroom temperature of the inside and the outside is

used for the present, but other sensors can be added to the system easily to show on

charts, as well. CEPInfoActivity is used to monitor complex events extracted from the CEP

engine on the broker side.

1.3.2. data Package

This package contains classes of the sensors used in the classroom. Each sensor value is

stored on the SensorDataFactory to use in charts or for monitoring, etc.

Figure 4 data Package Class Diagram

1.3.3. graphics Package

This package contains classes for showing the sensor data on the charts. The design is

flexible and extendable to add new types of the sensors to visualize. Each sensor data is

inserted to the dataset of the SensorChart of the current sensor type.

Page 7: Android Implementation using MQTT Protocol

7

Figure 5 graphics Package Class Diagram

1.4. Platform

The properties of the each platform and library used in the project is given in Table 1.

Table 1.4-1 Platforms and Libraries used in the Project

Apache ActiveMQ Version 5.7.0

Eclipse SDK Version: 3.7.1

Android Platform Version: 2.3.3

achartengine-1.0.0.jar for charting the sensor data

org.eclipse.paho.client.mqttv3.jar for communicating and getting messages

from the broker

Page 8: Android Implementation using MQTT Protocol

8

1.5. Testing the Application

For testing the application, CEP on the broker is simulated the real time environment of the

classroom. To this simulation, the stored sensor data is retrieved from the database and

sent to the rule machine, as if the sensors are working in the real time. The CEP engine on

the broker side is even run to process messages and extract high level information. In

addition, the sensor data which is read from the database is also published to the topics that

the Android client listens. Therefore, the Android application can receive messages from

both sensor data topics and the CEP machine topics.

Listened Topic Source: Clasroom Setup /

CEP

Hall Effect – 0 Door Setup Out

Infrared Proximity – 0 Door Setup Out

Optical – 0 Door Setup Out

Motion - 0 Door Setup Out

Infrared Proximity – 1 Door Setup In

Temp - 1 Outside(Window) Setup

Light - 1 Outside(Window) Setup

Electret - 0 Wall Setup Teacher

Temp - 0 Wall Setup Teacher

Light - 0 Wall Setup Teacher

Motion - 1 Wall Setup Window

Light – 2 Wall Setup Window

Esper CEP Engine

1.5.1. Starting ActiveMQ Broker

Apcahe ActiveMQ Broker is started from the batch file activemq.bat located in the activemq

installation folder. When the batch file is called from the command line, the broker is

executed on the machine (Figure 6). After the broker is started, the publishers and

subscribers get ready for communicating over publish-subscribe topics. This messaging is

based on JMS (Java Messaging Service), but in this project, the broker is configured to

support MQTT Connections to receive and send MQTT messages, which are, some kind of

publish/subscribe messaging service. The configuration is mentioned, in the next sections.

Page 9: Android Implementation using MQTT Protocol

9

Figure 6 Starting ActiveMQ Broker

1.5.2. Starting Simulator and CEP Engine

JMSToEsper Java application is implemented for simulating the classroom environment. This

application reads data from database and both publishes messages to corresponding topics

and starts the CEP engine to take events for extracting complex events. When the

application is executed, the sensor data which is stored on the database with time stamp is

published in the real time sequence as the order and duration of the real time. If the user

wants, the simulation can be speeded up to 2x, 4x, or faster to see messages more fast

moving from the real time.

Page 10: Android Implementation using MQTT Protocol

10

Figure 7 Starting the Simulator - CEP Engine

1.5.3. Running Android Application

After starting both the Simulator with CEP Engine and the broker, the mobile application can

be run. When the application is executed, Settings menu is opened by default. From this

menu, users can start the application to receive messages or can update the url of broker.

Figure 8 Settings Menu and Updating the Broker URL

Page 11: Android Implementation using MQTT Protocol

11

The application can be started or stopped, whenever the user wants. The example screens of

the application is given in Figure 9.

After starting, the application will display a message about connecting to the topics. The

messages comes from the server side could be monitored from the Tabs located on the

upper side of the screen. The tabs are located corresponsding to the classroom sensor

setups. There are 4 four setups: DoorIn, DoorOut, WallTeacher, WallWindow and Window

setups. Door setups are located to the door of the classroom; Wall setups are located on the

inside wall of the classroom; Window setup is located to the outside of the classroom’s

window. The complex events extracted from CEP Rule Machine Esper are given in tab CEP

Info. Example screens from these setups are given in below Figures 10-11.

Figure 9 Starting(left)/Stopping(right) the Application

Page 12: Android Implementation using MQTT Protocol

12

Figure 10 Screens from DoorOut and Window Setups

Figure 11 Screens from Extracted CEPInformation and WallTeacher Setup

Page 13: Android Implementation using MQTT Protocol

13

Lastly, user can see the chart of the temperature of the inside and the outside of the

classroom, until the present time from the Graphics tab (Figure 12).

Figure 12 Monitoring the Classroom Inside and Outside Temperature on Chart

Page 14: Android Implementation using MQTT Protocol

14

2. TECHNOLOGIES USED IN THE PROJECT

2.1. AChartEngine

AChartEngine is a charting library for Android applications [1]. It is used for several kinds of

chart types, as given in [1]:

line chart

area chart

scatter chart

time chart

bar chart

pie chart

bubble chart

doughnut chart

range (high-low) bar chart

dial chart / gauge

combined (any combination of line, cubic line, scatter, bar, range bar, bubble) chart

cubic line chart

In this project, I used the line chart for two series (inside and outside temperature) with X

time axis. The collected data could be also shown in other charts, since the design is flexible

to use other charts.

AChartEngine is currently at the 1.0.0 release and it is free to use. It is licensed under

Apache License 2.0. This library is also open-source; the source codes can be reachable from

its repository on the Web.

2.2. MQTT from PAHO

MQTT [2], which is a lightweight publish/subscribe messaging protocol and widely used in

sensors, home automation, mobile applications and such small devices which also is used in

this study. MQTT is a (M2M) machine-to-machine publish/subscribe messaging protocol

based on topic structure. The topic structure is used for messaging between subscribers and

publishers. The publisher send messages over a predefined topic, the consumers

(subscribers) receive messages from the topics to which they subscribe.

Page 15: Android Implementation using MQTT Protocol

15

There are several open source and free MQTT Clients and Brokers (Servers) to be used for

this type messaging for different platforms and programming languages such as C, C++,

Java, Python, Delphi, PHP etc. as given below:

Servers / Brokers for MQTT (taken from [3])

IBM WebSphere MQ Telemetry It provides full MQTT v3.1 support with WebSphere MQ. Supports Java (MIDP and above) and C clients.

IBM Lotus Expeditor micro broker

Part of the Lotus Expeditor product, micro broker is an OSGi Java implementation of MQTT

server. It provides Java (including JMS), C# and C clients.

Really Small Message Broker

It provides complete MQTT v3.1 support, and a C client API.

Mosquitto

An Open Source MQTT server with C, C++, Python and JavaScript clients.

Eclipse Paho

The Eclipse Paho project hosts a version of the mosquitto broker for the Machine-to-Machine

Industry Working Group [4].

MQTT.js

A node.js MQTT server.

eMQTT

m2m.io

webMethods Nirvana Messaging

A messaging engine with support for many different transports. It also supports MQTT.

RabbitMQ

An AMQP message broker – with an MQTT plugin.

Apache ActiveMQ

ActiveMQ has a support for MQTT with configuration.

Apache Apollo

Moquette

A Java MQTT broker.

IBM WebSphere Message Broker

It supports MQTT v3.

Page 16: Android Implementation using MQTT Protocol

16

Clients for MQTT (taken from [3]): Only C,C++ and Java Clients are given:

ActionScript

C

Eclipse Paho – a C client (based on the IBM WebSphere MQ Telemetry C client)

libmosquitto – a C client library provided with the mosquitto server

IBM WebSphere MQ Telemetry provides a C client API (largely identical to Eclipse Paho)

IBM Lotus Expeditor micro broker includes a C client API

liblwmqtt - a very lightweight C client undeveloped – use Eclipse Paho instead

C++

libmosquittopp – a C++ client library provided with the mosquitto server

Delphi

Erlang

Java

Eclipse Paho - a Java client (based on the IBM WebSphere MQ Telemetry Java client)

MeQanTT

mqtt-client – a Fusesource Java MQTT client with a variety of API styles

moquette-mqtt – a Java client

IBM WebSphere MQ Telemetry provides a Java client API

IBM Lotus Expeditor micro broker includes two Java client APIs

Javascript / node.js

LotusScript

.NET

Objective-C

Perl

PHP

Python

Ruby

Page 17: Android Implementation using MQTT Protocol

17

In this project, as a broker, ActiveMQ Broker [5] is used and as a client, Eclipse Paho [6]

MQTT Client is used. Paho Client is developed under Eclipse Technology Project as open

source. It is given a solution as Machine-to-Machine (M2M) from Eclipse Project. First

releases are aimed to be developed for MQTT Java and C client code. The client library is

used in the mobile application.

2.3. ActiveMQ Broker Configuration

To make integrity of MQTT with ActiveMQ, which is broker of the publish/subscribe

messaging and supports MQTT protocol connection, configuration is done on the broker side.

To enable MQTT on ActiveMQ [5], given xml code is inserted in to the configuration file of

broker ActiveMQ:

<transportConnectors>

<transportConnector name="mqtt"

uri="mqtt://localhost:1883?transport.defaultKeepAlive=60000"/>

</transportConnectors>

This enables ActiveMQ to support connection comes from MQTT clients for 60 seconds. After

60 seconds, the broker stops hanging inactive connections. The default port of MQTT is 1883

for TCP/IP, for secure connections, MQTT could be able to use TCP/IP port 8883 over SSL.

Page 18: Android Implementation using MQTT Protocol

18

3. REFERENCES

[1] "AChartEngine Web Site," [Online]. Available: http://www.achartengine.org/. [Accessed

2 1 2013].

[2] "MQ Telemetry Transport," [Online]. Available: http://mqtt.org/. [Accessed 25 12 2012].

[3] "MQTT Softwares," [Online]. Available: http://mqtt.org/software. [Accessed 2 1 2013].

[4] "Machine-to-Machine Industry Working Group," [Online]. Available:

http://m2m.eclipse.org/. [Accessed 1 1 2013].

[5] "MQTT on Apache ActiveMQ," [Online]. Available: http://activemq.apache.org/mqtt.html.

[Accessed 26 12 2012].

[6] "Paho Project," [Online]. Available: http://www.eclipse.org/paho/. [Accessed 28 12

2012].


Recommended