Monitor Arduino Sensor using Smartphone and ESB: IoT Tutorial

This post is the application of the IoT reference architecture shown in the previous post about how to monitor Arduino Sensor using WSO2 ESB. The previous post described Internet of things project based on Arduino Android and ESB.
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:
WSO2 ESB Custom 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 an JSON response.This is the proxy code:
<proxy name="AndroidService"
 transports="https http"
 <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:

WSO2 ESB Proxy list

The WSO2 Proxy details are shown below:

ESB Proxy detail

The proxy is ready now to accept incoming connection.

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 web server and temperature sensor

The arduino code is very simple:

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

// 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' &amp;&amp; currentLineIsBlank) {
       // send a standard http response header
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/xml");
      client.println("Connection: close"); // the connection will be closed after completion of the response

      // 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.

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:

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



The interesting part in the app, is the java code that makes the HTTP request:
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:

how to code android app that makes HTTP request

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