Monitor Arduino Sensor using Smartphone and ESB: IoT Tutorial

This project is an example of Internet of things: Arduino, Android, and ESB. Internet of things is one of the most emerging topics and one of the most discussed lately. What is Internet of things? When talking about IoT, we mean a set of smart objects that are connected together and can exchange information.
In this way, it is possible, for example, to create a sensor network and we get information from them using our smartphones. In this world, Arduino plays an important role, because it is a low-cost system that can be used in several contexts.
In the previous post, we talked about a simple project that uses Arduino and Android to control a remote led. This post shows a different approach and the Android smartphone does not connect directly to the Arduino and its sensors but uses a component that decouples the Android client and the source of information (Arduino).

Internet of things: Arduino, Android, ESB

Internet of things: Arduino, Android, ESB: Architecture

Recently i came across an interesting article explaining the IoT architecture. This architecture is quite complex and general purpose, i want to simplify it using just some components.

Instead of connecting directly the Android smartphone to the Arduino, i used a component called ESB (Enterprise Service Bus). In this case, my preference is for WSO2 ESB because it is an full open source project and it fits perfectly in the IoT ecosystem. Moreover, WSO2 proposed the reference architecture.

iot android

Why should we use an ESB in IoT?

First of all, if you don’t know the ESB feature i suggest you to give a look here. There are several reasons why we should use an Enterprise Service Bus:

  • We can connect to several Arduino board and we want to have a single entry point that can route the request to the right board.
  • We might require some data transformation from Arduino data to other formats so that we can connect other devices than smartphone
  • We want to implement some authentication logic so that only authenticated client can access the data
  • We want to implement some business logic before send back the data from Arduino to the client
  • We don’t want to expose Arduino board directly on the internet
Using an ESB, we can move some logic from Arduino board to the ESB, leaving to the Arduino only the “electronic” tasks. For example, we could use Arduino to monitor the indoor temperature but we don’t want to add authentication logic or some other controls.

The Project: Android and Arduino with ESB

As test project, we want to monitor the indoor temperature using a sensor connected to Arduino. In this first example, the Android smartphone connect to the ESB and requests the current temperature. The general IoT context is shown below:

esb proxy android

In the picture, inside the ESB it is implemented a custom proxy. A proxy is a component that lives inside the ESB and hanldes incoming request from external resources.
Using a proxy, it is possible to transform the incoming request send it to the destination and then read the response and transform it back.
In our case, the source that creates the request is the Android smartphone that sends the requests to the Proxy (ESB) and the destination is the Arduino board.

In the next post, we will start to analyze how to create all the components and how we can make them talking.
By now, if you want to set up WSO2 ESB you can go here and install the app.

In this post, you get a basic knowloedge of the internet of things definition and how to start coding applications of internet of things.

This post describes how to monitor Arduino sensor using the application of the IoT reference architecture.
What if we want to read a remote sensor connected to Arduino board using our smartphone? As explained previously, we can connect directly our Android smartphone to arduino and read the sensor data or we can use a different approach where there is an Enterprise Service Bus (ESB) that stands in the middle between the arduino board and the smartphone.
Even if the system is a little bit more complex as stated in the previous post using ESB we have a lot of advantages.

It is time to show things work:

  •  an ESB proxy using WSO2 ESB that sends HTTP request to Arduino board and transforms response from Arduino board to our smartphone
  • Arduino Webserver that handles HTTP connection and read the temperature sensor
  • Android app that shows the temperature value in our smartphone
Connect Arduino to ESB and send data to Android: Monitor arduino sensor

How to implement an WSO2 ESB Proxy

The first thing we do is implementing a proxy that lives in our ESB that handles the incoming connection from the Android smartphone on one side and on the other side handles HTTP connection toward Arduino board.
The ESB proxy is very simple, the picture below shows how it works:esb proxy
As said, the proxy accept incoming connection and forward it to the Arduino web server that reads the environment temperature and returns a simple XML response. This response is transformed by the proxy and becomes a JSON response.This is the proxy code:
[xml] <proxy name="AndroidService" transports="https http" startOnLoad="true" trace="disable" statistics="enable">
<property name="Message Flow" value="Arduino API"/>

<address uri=""/>
<payloadFactory media-type="json">
<format> {"temp":$1} </format>
<arg evaluator="xml" expression="//temp"/>
<property name="messageType" value="application/json" scope="axis2"/>
</proxy >

At line 14, the proxy forwards the request to arduino web server, while in the line 19-24 the proxy transforms the response to JSON.The WSO2 ESB Proxy is now available in the service list:

esb service

The WSO2 Proxy details are shown below:

wso2 esb proxy service

The proxy is ready now to accept an incoming connection and we will monitor Arduino sensor.

Arduino Web server and Temperature sensor

It is time to implement the Arduino sketch that reads the temperature sensor and makes the data available using a web server. Of course, to make the project working it is necessary to have an ethernet shield.
The code is similar to the Arduino starter book. The sketch is very simple:

arduino webserver

The Arduino code is very simple:

[java]void loop() {
// Is there a client (Our Android smartphone)
EthernetClient client = server.available();

// Read temp
int sensorVal = analogRead(3);
float vSens = (sensorVal /1024.0) * 5.0;
float temp = (vSens – 0.5) * 100;

if (client) {
// Let’s start reading
boolean isLastLine = true;
boolean isBody = false;
header = "";
reqData = "";
int contentLen = 0;

Serial.print("Client connected!");
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c =;
if (c == ‘n’ && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/xml");
// the connection will be closed after completion of the response
client.println("Connection: close");
// output the value of each analog input pin
// give the web browser time to receive the data
// close the connection:
Serial.println("client disconnected");

The code is very simple, arduino board reads the temperature sensor using the analog PIN and when the arduino web server receives an HTTP request, it responds with the temperature sensor value in an XML format.

[bctt tweet=”Implement an #IoT Architecture using #ESB #Android and #Arduino” username=”survivingwithan”]

Monitor Arduino sensor using Android app

Finally, it is time to implement the android app that makes an HTTP request toward ESB and read the temperature sensor data using arduino board.
The android app is very simple, it is implemented using material design so it as a Toolbar and the temperature value.
The layout is shown below:

[xml] <RelativeLayout xmlns:android="" xmlns:tools="" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

< xmlns:android="" xmlns:app="" android:id="@+id/toolbar" android:layout_height="wrap_content" android:layout_width="match_parent" android:background="@color/primaryColor" android:elevation="4dp" app:theme="@style/ThemeOverlay.AppCompat.Dark" />

<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:id="@+id/temp" android:text="–" style="@style/tempText"/>


The interesting part in the app, is the java code that makes the HTTP request:
[java] public void doRequest() {
Request req = new Request.Builder()

client.newCall(req).enqueue(new Callback() {
public void onFailure(Request request, IOException e) {

public void onResponse(Response response) throws IOException {
try {
String body = response.body().string();
Log.d("AA", "resp [" + body + "]");

JSONObject obj = new JSONObject(body);
float data = (float) obj.getDouble("temp");
((DataListener) ctx).notifyData(data);
catch(Throwable t) {

Running the android app the result is shown below:

android esb temp

At the end of this post, you know how to monitor Arduino sensor from your smartphone using an ESB proxy.