---
title: "Internet of Things with Android and Arduino: IoT Arduino project"
url: "https://maker.wiznet.io/gemma/projects/Internet-of-Things-with-Android-and-Arduino-IoT-Arduino-project/"
markdown_url: "https://maker.wiznet.io/gemma/projects/Internet-of-Things-with-Android-and-Arduino-IoT-Arduino-project/md"
type: "UCC: User Created Content"
author: "gemma"
author_url: "https://maker.wiznet.io/gemma/"
original_author: "gemma"
original_url: "https://www.survivingwithandroid.com/internet-of-things-with-android-and-arduino/"
published: "2022-07-21"
language: "en"
hardware: ["Arduino UNO", "Ethernet shield", "smartphone with android"]
likes: 0
views: 155
comments: 0
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Internet of Things with Android and Arduino: IoT Arduino project

> Internet of Things with Android and Arduino: IoT Arduino project

Original author: gemma (source: https://www.survivingwithandroid.com/internet-of-things-with-android-and-arduino/)

## Components

- **Arduino UNO** x 1 ([docs](https://www.farnell.com/datasheets/1682209.pdf))
- **Ethernet shield** x 1
- **smartphone with android** x 1
- Software: **Android app**

## Article

This **IoT Arduino project**?describes how to connect?**Android and Arduino**. To make things simple, we will suppose that we want to control a LED connected to Arduino using an Android app. This is a common scenario that requires to integrate Android and Arduino in an IoT Arduino project. Nowadays?**Internet of Things**?(*IoT*) is a pervasive technology that is changing the way we live and how we interact with devices. In IoT project, all the physical objects (things) are connected together using internet infrastructure.?**Arduino**?board is one of the most important devices that enables us to prototype projects. This post explores how to integrate?**Android with Arduino**?making the first step in?**IoT**.

## Internet of Things with Android and Arduino: Arduino IoT Project Overview

The picture below shows the main objects involved in the Arduino?**IoT project**:

![Internet of Things with Android and Arduino](https://www.survivingwithandroid.com/wp-content/uploads/2015/06/arduino_android_iot-2.png)

What we need is:

- *Arduino Uno*

- *Ethernet shield*

- *Smartphone with Android*

These devices are on the same network for simplicity. The idea of this Arduino and Android IoT project is building an?**Android app that exchanges data with Arduino**. To this purpose, the Android app will communicate with Arduino using the HTTP protocol. A small and simple?*Web server runs on Arduino,*?accepting HTTP requests. For simplicity, the app sends JSON data that holds the led status. Below you can download the Arduino source code for this Arduino IoT project.

[Download the Arduino source code project .ino](https://www.survivingwithandroid.com/wp-content/uploads/2016/11/android_led.zip)

## How to build an Arduino IoT project

The first step in building an Arduino IoT project that connects to Android, therefore we are building an Arduino server that accepts HTTP connection. Moreover, considering that this project wants to control a simple LED using an Android app, it is necessary to connect Arduino to a LED. The schematic below shows how to do it

![How to connect Arduino to a LED and control it using Android app. Arduino IoT project](https://www.survivingwithandroid.com/wp-content/uploads/2015/06/arduino_android_led-3-1024x702.png)

As you can see the connection is very simple. The Arduino code below describes how to build a simple HTTP server on Arduino to accept connecting coming from the Android app.

```
#include &amp;lt;spi.h&amp;gt;
#include &amp;lt;ethernet.h&amp;gt;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 1, 130); // Arduino IP Add
EthernetServer server(80); // Web server
// Http data
String reqData; // Request from Smartphone
String header;
int contentSize = -1;
String CONTENT_LENGTH_TXT = "Content-Length: ";
void setup() {
  // put your setup code here, to run once:
  Serial.begin(9600);
  pinMode(3, OUTPUT); // Set Pin 3 to OUTPUT Mode
  Serial.print("Ready...");
  //
  Ethernet.begin(mac, ip);
  server.begin();
}
void loop() {
  // Is there a client (Our Android smartphone)
  EthernetClient client = server.available(); 
  if (client) {
    // Let's start reading
    boolean isLastLine = true;
    boolean isBody = false;
    header = "";
    reqData = "";
    int contentLen = 0;
    Serial.print("Client connected!");
    while (client.connected()) {
     if (client.available()) {
       // Read data
      char c = client.read();
     if (contentSize == contentLen) {
       int idx = reqData.indexOf(":");
       String status = reqData.substring(idx + 1, idx + 2);
       Serial.println("Status : " + status);
       if (status.equals("1")) {
         digitalWrite(3, HIGH);
       }
       else {
         digitalWrite(3, LOW);
      }
     client.println("HTTP/1.1 200 OK");
     client.println("Content-Type: text/html");
     client.println("Connection: close");
     client.println();
     // send web page
     client.println("");
     client.println("");
     delay(1);
     break;
   }
  if (c == 'n' &amp;amp;amp;&amp;amp;amp; isLastLine) {
   isBody = true;
   int pos = header.indexOf(CONTENT_LENGTH_TXT);
   String tmp = header.substring(pos, header.length());
   int pos1 = tmp.indexOf("rn");
   String size = tmp.substring(CONTENT_LENGTH_TXT.length(), pos1);
   Serial.println("Size ["+size+"]");
   contentSize = size.toInt();
  }
  if (isBody) {
   reqData += c;
   contentLen++;
  }
  else {
   header += c;
  }
  if (c == 'n' ) {
   isLastLine = true;
  }
  else if (c != 'r' ) {
    isLastLine = false;
  }
 }
}
// Close connection
Serial.println("Stop..");
client.stop();
}
}
```

Almost all the?*arduino source code*?is used to handle HTTP connections. Notice that at line 4 we set the?*MAC Address*?of the ethernet shield while at line 5 we set the IP address. The server runs on port 80, you can change this port if you want. Once we have created this server Arduino can exchange JSON requests with the external application including an app that runs on a smartphone. In this first part of Internet of things with Android and Arduino project, you gained the knowledge on how to handle HTTP requests in Arduino. If you like to build a more complex project, you can explore?[how to build a smart plant monitoring system](https://www.survivingwithandroid.com/iot-project-tutorial-smart-plant-system/).

You can read these other tutorials:

[How to control LEDs connected to Arduino using an Android app](https://www.survivingwithandroid.com/internet-of-things-with-android-and-arduino/) [How to build an Android IoT app using Android Things tutorial: Step by step tutorial](https://www.survivingwithandroid.com/2017/01/android-iot-app-using-android-things-tutorial.html)

## Android app client: Send HTTP request

To complete our Internet of things with Android and Arduino scenario, it is necessary to implement an Android app that sends JSON requests. On the Android code side, the things are much simpler; the Android UI is shown below:

![Android app to control Arduino](https://www.survivingwithandroid.com/wp-content/uploads/2015/06/android_arduino_led-2.png)

There is one simple button. When the user clicks on it, the?**app sends an HTTP request to Arduino, that runs the web server.** If you want to know more about HTTP look at?[making HTTP requests in Android](https://www.survivingwithandroid.com/android-apache-http-client/). The app layout is very simple and it is not covered here, the core of the app is where the button click is handled:

```
ledView = (ImageView) findViewById(R.id.ledImg);
// Set default image
ledView.setImageResource(R.drawable.white_circle);
// Init HTTP client
client = new HttpClient();
ledView.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
   client.doRrequest(status ? "1" : "0");
   status = !status;
   if (status)
    ledView.setImageResource(R.drawable.white_circle);
  else
   ledView.setImageResource(R.drawable.red_circle);
  }
});
```

When the user touches the button, the app sends an HTTP request using?**HTTP client**. In this case, this project uses?*OkHttp*. The HTTP client is very simple:

```
public void doRrequest(String status) {
  initClient();
  Log.d("AA", "Making request..["+status+"]");
  Request req = new Request.Builder()
    .url(URL)
    .post(RequestBody.
        create(JSON, createJSON(status)))
     .build();
   client.newCall(req).enqueue(new Callback() {
    @Override
     public void onFailure(Request request,
                           IOException e) { }
     @Override
     public void onResponse(Response response) throws IOException {
        Log.d("AA", "resp [" + response.body().string() + "]");
      }
  });
}
```

Below some images of my work showing Arduino in IoT environment?: ![Android and Arduino integration](https://www.survivingwithandroid.com/wp-content/uploads/2015/06/arduino_android_iot-2.jpg)![Arduino with Android to control a LED](https://www.survivingwithandroid.com/wp-content/uploads/2015/06/arduino_android_iot_led-2.jpg) This?*Internet of things project with Android and Arduino*?showed how to integrate two different ecosystems and make them work together. In this post, you gained a basic understanding of how to create Arduino webserver example and how to connect it to the android app. This project can be extended and could be used as a base for arduino home automation. It is possible to make even more complex project that uses?[Android app and Arduino to control an RGB led](https://www.survivingwithandroid.com/connect-android-arduino-using-http-web-server/).

---

Source: https://maker.wiznet.io/gemma/projects/Internet-of-Things-with-Android-and-Arduino-IoT-Arduino-project/
