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:

What we need is:
- Arduino Uno
- Ethernet shield
- Smartphone with Android
How to build an Arduino IoT project

#include <spi.h> #include <ethernet.h> 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' && 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.
You can read these other tutorials:
How to control LEDs connected to Arduino using an Android app
How to build an Android IoT app using Android Things tutorial: Step by step tutorial
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:

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.
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 :
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.
Any way to impliment this with Outlook. Say, whenever you have an unopened email in your inbox, the light goes on?
can anyone tell me why im getting an
error : #include expects “FILENAME” or
on the arduino sketch on line 2.
Hi, it was an error when copying the source code. I corrected it and now should be ok. Let me know if you experience any other issue. Thank you for your time.
The idea is the same, you should check, using some Mail Api, if there is an unopened email and contact arduino to turn on the light.
it's very simple and good project for starter.
so, If you don't mind, I would like to introduce on WIZnet museum (http://wiznetmuseum.com) for everyone.
WIZnet produce the W5100 chipset on Ethernet shield. Hopefully, you will allow this.
Thanks.~
can you please give me the android Source code ,please
as i’m facing some errors with my project .
thanks
Sir, i am getting error in the arduino code at line no 63 saying, “amp was not declared in its scope”, my question is what is amp, can you please help me remove this error. Thank You
Yes you are right the mistake is caused by escaping char. Please read & ;as &. Thank you for spotted it
Thank You for your reply, just to be sure you mean in line 63 instead of
if (c == ‘n’ && isLastLine)
we will use
if (c == ‘n’ && isLastLine)
right?
Is there any example about how to send an email by using Ethernet shield , like when motion sensor detects motion the arduino send me email , helping me will be appreciated
To send a message I used parse.com and Temboo. Give a look at How to send notification using Arduino
hi, senior thank you for helping me ,
sir i had a problem with the way you showed to me , that the parse .com they are no longer accepting new accounts and i do not have one , i’m winder if there is another solution < thank you
As far as I know i don’t know an alternative, you could search it and if you find please write it the link so you can help other readers! Thank you
hi, senior thank you for helping me ,
sir i had a problem with the way you showed to me , that the parse .com they are no longer accepting new accounts and i do not have one , i’m winder if there is another solution < thank you
Hi sir,
i am kannadhasan very interest to create android application development with internet of thing(IoT )
project.How to create .I need help sir
What kind of project would u like to create?
Hello Sir! I’m Vaishak. I have planned for an IoT project with agricultural applications. Will you help me out with it?
You should tell me more about this project so that i can have an idea.
Now I’ve decided to use humidity sensor, soil moisture sensor, and temperature sensor and a water pump which works depending on the criteria of these sensors and carries out drip irrigation. I’ll the Arduino microcontroller.
Hello vaishak, i loved your concept and did u finally succeed in doing this project? Could you help me in doing this project…. The code, the materials required and also the diagram….. My email is briansm96@gmail.com thanq
Guys if you want to share your project here you are welcome
The name of my project is “ENHANCEMENT OF AGRICULTURAL APPLICATIONS USING IoT”. The whole system is controlled through GSM or Wifi.
Hello sir,
I am vivek anand from india. I just want to know that which bluetooth module is better with Arduino for multiple command.
Thanks
Hi Vivek,
as far as i know you can use every bluetooth module you like. You could prefer a bluetooth shield that is connected on top of arduino board or a external module. It is up to you.
For my project i use an external module.
hello sir,
i’m usmar ismail from indonesia.
this is awesome,
i have project that i have to create program like this, can you give me a tutorial for android session in this project. i dont get it, please,
i apologize for my english
thankyouuuu
email :usmar69ismail@gmail.com
Hi Usmar,
the android part is in the tutorial. Are you looking for a complete android app source code? If so tell me, i will add it to Github repository.
nice tutorial link of your github repository
sir i’m using enc28j60 with uipethernet shield.and control lights via android apk.
but problem is that when i press button for led onn/off it will react after 2 to 3 second later.
if i use same apk with wi-fly module rn-171.it reacts within milisecond.
how can i improve speed in ethernet.
Did you check your Internet connection speed?
ok sir i got the problem.actually apk send hello to enc28j60 for hand shake
and suppose to return hello from Ethernet it doesn’t get it open the port for
few second.then rx another string and reply
I should read the code to answer….
hello sir. i want to create a combination of smart mirror and smart window for my iot project with my partner. we want to use the idea of smart mirror and put the idea on the window. the idea of smart window is still there and we just want upgrade the functionality of smart window become smart mirror as well. both of us are lack of knowledge of the arduino. our final year project coordinator wants us to use arduino and preferred the arduino mega. will u help us out with this? we really need help.
Hello, can you please send me the full code via github? I cant find it there. Thank you.
Hi, SIR
the android part is it on Github repositoryt? i’m looking for a complete android app source code?
Hello sir i want to do project with IOT in the AWS inftastructure…could you please help me to select the idea which king of thing is doable with in 6 weeks.??
There are several projects you could do with IoT for example:
– Read sensor data and use graphs
– Remote control your development board
– Arduino and Android integration
All this project are available at https://www.survivingwithandroid.com/internet-of-things. Give a look
Well there are several ideas you can build. there are tons of AWS tutorial bout it
hello , thanks for sharing , i try to compile your arduino code but i got this error
error: unknown type name ‘prog_char’
uint16_t fill_tcp_data_p(uint8_t *buf,uint16_t pos, const prog_char *progmem_s)
Utilisation de la bibliothèque SPI version 1.0 dans le dossier: C:Program Files (x86)ArduinohardwarearduinoavrlibrariesSPI
Utilisation de la bibliothèque Ethernet version 1.1.2 dans le dossier: C:Program Files (x86)ArduinolibrariesEthernet
exit status 1
Error compiling for board Arduino/Genuino Uno
can you help me fix it ?
HI it seems a prole related to your libraries. You can try to create a new empty sketch and check if it compiles. I sup pose it is a problem not related to the sketch in the post. Let me know the results
thank you sir ,i fixed the probleme , by downloading the zip file posted in this page, and it has successfuly compiled , the probleme is with posted code cus it s contained some syntaxe error for ex : library is declared : #include <spi.h> #include <ethernet.h>
and the instruction :
if (c == ‘n’ && isLastLin )
could you send me the android complete code , i need it for my final study
project ?
Hello sir , sorry it was my fault , i compiled the arduino code on the page not the code in the zip file , sir can you send me the full source android code i need it as a part of my final study project , thanks
There are a huge list of projects you can build, it depends on how complex your project must be
hi sir,
i am a last year eng student,i have a plan to do a project which uses a android application that control a electronic device through internet.i want to get data from the device and store in a server also eant to retrive the data in the app.
can you please help me with the android code and the other server side procedure that i need to follow to complete the project
Hello Sir!
I’m trying to develop an android application for my IoT project. The project is to sense temperature using LM35 temp sensor, send data to thinkspeak using Arduino UNO and ESP8266 wifi shield. The data is analysed at thinkspeak and I want to display it in the app too. Then the app’s user will determine whether a fan has to turned on or not depending on the temperature readings. So far I’m able to send data to thinkspeak and analyse it only the app development part is left. I’m completely new to Andriod app development.
Could you please help me in accomplishing the task?!
Hey i got a problem, my status just give “n” and i don’t know why, can you help me?
Hello Sir, i am rizal from indonesian, can you tell more about source code android app in this project.? i am confused because the code is not full. or the project is it on github repo?
i am looking complete android app source code.
thanks.
Hi,
Nice to meet you.
We are the WIZnet Team in India.
We have been searching some application references in which WIZnet solution is applied and found your project “Control RGB LED using Arduino and Android “. I. Your development looks very cool & smart.
Recently we have developed WIZnet Museum (http://wiznetmuseum.com) site. This is an academic-purposed collection of open projects, tutorials, articles and etc from our global customers.
If you are O.K. we would like to introduce your project and device in here. Hopefully, you will allow this.
Hopefully, keep contacting us for the friendship.
Thank you very much
Wiznet team, India
It’s really a nice and helpful piece of information. I’m happy that you simply shared this helpful info with us.
Please keep us up to date like this. Thank you for sharing.
please share source code of android app