Vous êtes sur la page 1sur 12

What are you looking for?

Menu

ANDROID TUTORIAL

INTERNET OF THINGS

50 ANDROID TUTORIALS

IOT PROJECT LIST

Home Android tutorial Internet of Things with Android and Arduino: Control remote Led

Internet of Things with Android and Arduino: Control remote


Led
Francesco Azzola 2 Years Ago 41 Comments

119 Like 149

Nowadays a new emerging technology is Internet of Things (IoT): this post describes how
to create Internet of things with Android and Arduino. In IoT project, all the physical objects
(things) are connected together using internet infrastructure. Arduino borard is one of the
most important object in this ecosystem. In this post, we will explore how to integrate
Android with Arduino making the first step in IoT .Even Google at io15 presented its new
IoT infrastructure called Brillo .
This tutorial describes how to create an Internet of things with Android and Arduino project:
android app to control a remote Led .
Internet of Things with Android and Arduino :
Overview
The picture below shows the main objects involved in the IoT project :

What we need is:

Arduino Uno
Ethernet shield
Smartphone with Android

All the objects are in the same network for simplicity. The idea is that the smartphone sends
an HTTP request to the Arduino. A very small and simple Web server runs on Arduino,
accepting HTTP request. For simplicity, the app sends JSON data that holds the led status.

Download the Arduino Project (.ino)


Please support us, use one of the buttons below to unlock the content.

like
Like 149 tweet +1
119 us

Arduino: Web server and connections


In the Internet of things context (IoT), on the Arduino side, we simply need to connect the led
to Arduino main board and control it using one of the Arduino output. The most complex part
is creating a Web server that handles HTTP request. The image below shows how Arduino is
connected to the led:

As you can see the connection is very simple. The Arduino sketch that implements the web
server is shown below:
1 # i n c l u d e & l t ; s p i . h & g t ;
2 # i n c l u d e & l t ; e t h e r n e t . h & g t ;
3 b y t e m a c [ ] = { 0 x D E , 0 x A D , 0 x B E ,
4 I P A d d r e s s /i / p ( A1 r 9 d 2 u , i n1 o6 8 I , P 1 A , d d1 3 0 ) ;
5 E t h e r n e t S/ e/ r W
v ee rb s s ee r r v v ee r r ( 8 0 ) ;
6
7 / / H t t p d a t a
8 S t r i n /g / r Re eq qD ua et sa t; f r o m S m a r t p h o n e
9 S t r i n g h e a d e r ;
10 i nc to n t e n t S i z e = - 1 ;
11 S t r i n g C O N T " EC No Tn _t e L; n
E tN - G L T e Hn _g Tt Xh T
: "=
12
13 v os i ed t u p ( ) {
14 / / p u t y o u r s e t u p c o d e h e r e , t o
15 S e r i a l . b e g i n ( 9 6 0 0 ) ;
16 p i n M o d e /( /3 , S eO t U TP Pi U
n T 3) ; t o O U T P U T M
17 S e r i " a R l e. )ap ;dr iy n. t. (. "
18 / /
19 E t h e r n e t . b e g i n ( m a c , i p ) ;
20 s e r v e r . b e g i n ( ) ;
21 }
22
23 v o l i o do p ( ) {
24 / / I s t h e r e a c l i e n t ( O u r A n d r o i d
25 E t h e r n e t C l i e n t c l i e n t = s e r v e r . a
26
27 i (f c l i e n t ) {
28 / / L e t ' s s t a r t r e a d i n g
29 b o o l e a n t r ;i u s e L a s t L i n e =
30 b o o l e a f na ; l i s s e B o d y =
31 h e a " d; " e r =
32 r e q D" ; a" t a =
33 i nc to n t e n t L e n = 0 ;
34 S e r i " a C l l . i p e r ni )tn ; t c( o n n e c t e d ! "
35 w h ( i cl le i e n t . c o n n e c t e d ( ) ) {
36 i (f c l i e n t . a v a i l a b l e ( ) ) {
37 / / R e a d d a t a
38 c h c a r= c l i e n t . r e a d ( ) ;
39
40 i (f c o n t e n t S i z e = = c o n t e n t L e n ) {
41 i ni dt x = r e" )q: ; "D a t a . i n d e x O f (
42 S t r i n g s t a t u s = r e q D a t a . s u b s t r i
43 S e r i a" lS . t p a +r t i u sn s tt al : nt u( " s ) ;
44 i (f s t a t " u )1s )" . e { q u a l s (
45 d i g i t a l W r i t e ( 3 , H I G H ) ;
46 }
47 e l { s e
48 d i g i t a l W r i t e ( 3 , L O W ) ;
49 }
50
51 c l i e n" tH . T p T r Pi /n ) 1t ; .l 1 n ( 2 0 0 O K "
52 c l i e n" tC . o p n r t i e n n t t l ) -n ; T( y p e : t e x t / h t m l "
53 c l i e n" tC . o p n r n i en ) ct ; tl in o( n : c l o s e "
54 c l i e n t . p r i n t l n ( ) ;
55 / / s e n d w e b p a g e
56 c l i e n" ) t" ; . p r i n t l n (
57 c l i e n" ) t" ; . p r i n t l n (
58 d e l a y ( 1 ) ;
59 b r ;e a k
60 }
61
62 i (f c ' &n= 'a= m p ; & a m p ; i s L a s t L i n e ) {
63 i s B to rd; uy e =
64 i np to s = h
e a d e r . i n d e x O f ( C O N T E N T _
65 S t r i n g tm p = h e a d e r . s u b s t r i n g ( p o
66 i np to s 1 = " tr) m n; "p . i n d e x O f (
67 S t r i n g s i z e = t m p . s u b s t r i n g ( C O N
68 S e r i a" lS . i +pz sr e "ii )z]n [ ; "et " l+ n (
69 c o n t e n t S i z e = s i z e . t o I n t ( ) ;
70 }
71
72 i (f i s B o d y ) {
73 r e q D a t a + = c ;
74 c o n t e n t L e n + + ;
75 }
76 e l { s e
77 h e a d e r + = c ;
78 }
79 i (f c ' )n= ' = {
80 i s L a ts rt ; uL ei n e =
81 }
82 e l i s( f ce ' )r ! ' = {
83 i s L a fs at ; lL si ne e =
84 }
85 }
86 }
87
88 / / C l o s e c o n n e c t i o n
89 S e r i a" lS . t ) p o; r p i . n . t " l n (
90 c l i e n t . s t o p ( ) ;
91 }
92 }

Almost all the arduino source code is used to handle HTTP connection. Notice that at line 4 we
set the MAC Address of the ethernet shield while at line 5 we set the IP address.
In this way Arduino is ready to be integrated inside our internet of things world, it can
exchange JSON request with 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 how to handle HTTP request in Arduino.
You can use other IoT protocols to exchange data between Arduino and Android app.

Android 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 request.
On Android code side, the things are much more 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 request 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:

1 l e d V i e w = ( I m a g e V i e w ) f i n d V i e w B y
2 / / S e t d e f a u l t i m a g e
3 l e d V i e w . s e t I m a g e R e s o u r c e ( R . d r a w
4
5 / / I n i t H T T P c l i e n t
6 c l i ne enH wt t t =p C l i e n t ( ) ;
7
8 l e d V i e w . s n e eV t wO
i en wC . l O
i cn kC Ll ii sc tk eL ni es rt (e n e r ( )
9 @ O v e r r i d e
10 p u v b o
l o ii ncd C l i c k ( V i e w v ) {
11 c l i e n t . d o " R 1: "r " )0e ; "q u e s t ( s t a t u s ?
12 s t a t u s = ! s t a t u s ;
13 i (f s t a t u s )
14 l e d V i e w . s e t I m a g e R e s o u r c e ( R . d r a w
15 e l s e
16 l e d V i e w . s e t I m a g e R e s o u r c e ( R . d r a w
17
18 }
19 } ) ;

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:

1 p u b
v o
l d ii ocd R r e q u e s t ( S t r i n g s t a t u s ) {
2 i n i t C l i e n t ( ) ;
3 L o "g A. , "A
d M(" a k i n +g s tr " ae )] tq; " uu se +s t . . [ "
4 R e q u en s eRt we qr eu qe s =t . B u i l d e r ( )
5 . u r l ( U R L )
6 . p o s t ( R e q u e s t B o d y . c r e a t e ( J S O N , c
7 . b u i l d ( ) ;
8
9 c l i e n t . n e nw eCC waa l l l l (b r a e c q k ) ( . ) e n{ q u e u e (
10 @ O v e r r i d e
11 p u vb o
l o ii ncd F a i l u r e ( R e q u e s t r e q u e s t , I
12
13 @ O v e r r i d e
14 p u v b ol o ii ncd R e s p o n s e t( hR reI oO s wpE sox nc se ep t r i eo sn p o{ n s
15 L o "g A. , "A
d r (" e + s p r e [ s " p o n s " e )] . ; " b o d y ( ) . s t r i n g (
16 }
17 } ) ;
18
19 }

Create your first Internet of things project integrating


#Arduino and #Android. #IoT CLICK TO TWEET

Below some images of my work showing arduino in IoT enviroment :

In this post you gained a basic understanding about how to create arduino webserver example
and how to connect it to 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.

This Internet of things project with Android and Arduino showed how to integrate two different
ecosystems and make them working together.

119 Like 149

IoT DIY Project


with...

Android
Internet of...

IoT projects
tutorials list....

How to use
Arduino...

Arduino Led:
Control...

Internet of
things...

Internet of
things...

IoT project
Arduino...

Rapid
prototyping...

IoT project
tutorial:...

Comparing
IoT...

Internet of
things...

Build IoT app


using...
Tags: Arduino tutorial

RELATED POSTS

Android Google map: Add weather data tile


Francesco Azzola March 30, 2015

How to use Arduino MKR1000 in IoT projects: Arduino tutorial


Francesco Azzola May 9, 2016

Arduino Alternative IDE: PlatformIO IoT integrated platform


Francesco Azzola July 26, 2016
Android Weather App: Google play store
Francesco Azzola September 24, 2013

Android Actionbar Drop Down navigation


Francesco Azzola July 17, 2013

IoT Project: Integrate Arduino with Yahoo! using Temboo


Francesco Azzola February 16, 2016

ABOUT THE AUTHOR


Francesco Azzola

I'm a senior software engineer with more than 15 yrs old experience
in JEE architecture. I'm SCEA certified (Sun Certified Enterprise
Architect), SCWCD, SCJP. I'm an android enthusiast and I've worked
for a long time in the mobile development field. I've created a blog
called www.survivingwithandroid.com where I share posts about
coding in Android and IoT ecosystem. I'm the technical reviewer of the book Internet of
Things with Arduino Blueprints(published by PacktPub). Follow me: Google+ , LinkedIn

Sorry, the browser you are using is not currently supported. Disqus actively supports the following
browsers:

Firefox
Chrome
Internet Explorer 11+
Safari

Ihsene Omari 4 months ago

Hi, SIR
the android part is it on Github repositoryt? i'm looking for a complete android app source code?

Imane Tchantchane 2 months ago

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 bibliothque SPI version 1.0 dans le dossier: C:\Program Files
(x86)\Arduino\hardware\arduino\avr\libraries\SPI
Utilisation de la bibliothque Ethernet version 1.1.2 dans le dossier: C:\Program Files

POPULAR POSTS
Build IoT project with
Arduino sensor in

Making it Easier to Get


Started with

IoT MQTT protocol tutorial:


Control remote peripherals

Radio of the future Audials


Radio

GET MORE STUFF LIKE


THIS
IN YOUR INBOX

Enter your email here

SIGN UP NOW

we respect your privacy and take protecting it


seriously

LAST UPDATED

IoT project tutorial: Smart plant


system
First steps with NFC in Android -
Android NFC App tutorial
IoT MQTT protocol tutorial: Control
remote peripherals in IoT project
Getting started with IoT DIY
project
Building IoT app for Android
Things in 3 Steps


Surviving with Android Copyright 2017. This content is published under the
Creative Commons Attribution-NonCommercial-ShareAlike 4.0
International
By continuing to use the site, license.
you agree to the use of cookies. more information Accept

Policy Privacy About Me Advertise Contact me Guest Posting