loading
here we have created an android application using ecllipse

this application turn on led on arduino.

this application also work in your LAN.

Hardware require:

1: arduino uno
2: arduino ethernet sheild
3: RJ45 cable
4: LED
5.Android Phone


Software require:

1:Ecllipse ide  setting up with  Android Developement Adroid SDK.
2: Arduino 2.0



Step 1 :Assigning MAc and IP to Arduino board
UPLOAD THIS PROGRAM TO  ARDUINO.
ALSO CHANGE YOUR MAC AND IP ACCORDING YOUR NETWORK.




/*
  UDPSendReceive.pde:
This sketch receives UDP message strings, prints them to the serial port
and sends an "acknowledge" string back to the sender

A Processing sketch is included at the end of file that can be used to send
and received messages for testing with a computer.

created 21 Aug 2010
by Michael Margolis

This code is in the public domain.
*/


#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <SoftwareSerial.h>

// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {0x90,0xA2,0xDA,0x0D,0x8B,0x8F};
IPAddress ip(192, 168, 1, 105);
IPAddress ip1(192, 168, 1, 101);
EthernetServer server(8032);
unsigned int localPort = 8032; 
boolean incoming = 0;
// local port to listen on

// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char  ReplyBuffer[] = "acknowledged";       // a string to send back

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

void setup() {
  // start the Ethernet and UDP:
  Ethernet.begin(mac,ip);
  Udp.begin(localPort);
  pinMode(2,OUTPUT);
  Serial.begin(9600);
}

void loop() {
/*
EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
      
        //reads URL string from $ to first blank space
      
        if(incoming && c == ' '){
          incoming = 0;
        }
        if(c == '$'){
          incoming = 1;
        }
      
        //Checks for the URL string $1 or $2
        if(incoming == 1){
          Serial.println(c);
        
          if(c == 'a'){
            Serial.println("ON");
            digitalWrite(2, HIGH);
             int rd=digitalRead(2);
             delay(10);
            server.write("ON");
            delay(10);
             Serial.println(rd);
          }
          if(c == 'b'){
            Serial.println("OFF");
            digitalWrite(2, LOW);
             int rd=digitalRead(2);
             delay(10);
            server.write("OFF");
             Serial.println(rd);
             delay(10);
          }
      
        }
      
     
      
     

        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        }
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(10);
    client.stop();
  }


  */
// if there's data available, read a packet
  int packetSize = Udp.parsePacket();

Serial.println(packetSize);
if(packetSize)
  {
    Serial.print("Received packet of size ");
    Serial.println(packetSize);
    Serial.print("From ");
    IPAddress remote = Udp.remoteIP();
    for (int i =0; i < 4; i++)
    {
      Serial.print(remote[i], DEC);
      if (i < 3)
      {
        Serial.print(".");
      }
    }
    Serial.print(", port ");
    Serial.println(Udp.remotePort());

    // read the packet into packetBufffer
    Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
    Serial.println("Contents:");
    Serial.println(packetBuffer);
    Serial.println(packetBuffer[0]);
  
    if(packetBuffer[0]=='1'){
      digitalWrite(2,HIGH);
    }else if(packetBuffer[0]=='2'){
       digitalWrite(2,LOW);
    }

    // send a reply, to the IP address and port that sent us the packet we received
    Udp.beginPacket(Udp.remoteIP(),Udp.remotePort());
    Udp.write("WAHh BHurA");
    Udp.endPacket();
  }
    /*Udp.beginPacket(ip1,8032);
    Udp.write(ReplyBuffer);
    Udp.endPacket();
  delay(400);
  client.stop();*/
}


/*
  Processing sketch to run with this example
=====================================================

// Processing UDP example to send and receive string data from Arduino
// press any key to send the "Hello Arduino" message


import hypermedia.net.*;

UDP udp;  // define the UDP object


void setup() {
udp = new UDP( this, 6000 );  // create a new datagram connection on port 6000
//udp.log( true );   // <-- printout the connection activity
udp.listen( true );           // and wait for incoming message
}

void draw()
{
}

void keyPressed() {
String ip       = "192.168.1.177"; // the remote IP address
int port        = 8888;  // the destination port

udp.send("Hello World", ip, port );   // the message to send

}

void receive( byte[] data ) {    // <-- default handler
//void receive( byte[] data, String ip, int port ) { // <-- extended handler

for(int i=0; i < data.length; i++)
print(char(data[i]));
println(); 
}
*/


Step 1: PING With Arduino in Command Prompt

You have assigned MAC and IP address to arduino board.
Now we have to check whether arduino is giving response or not..!!!

1.Setup bridge connection with your PC and Arduino ethernet board(LAN ).

2. if connection is created then we have to run PING command in CMD.

           ping 192.168.1.105     // assign here your arduino's ip address.

If ping command is working than output is look similar as image.

Step 2: Run an Android Application

We have provided .apk file source code in arduino. rar file.
Install it on your android phone and run it within your network.

Step 3: Contact Us

For further information send mail to our e-mail id: technoartgroup007@gmail.com



Presented By:

Techno-Art Group
<p>Q1. Will this work with WiFi instead of Ethernet?</p><p>Q2. Why use UDP and not TCP?</p>
<p>Can this work with Wi-FI instead of Eternet?</p>
<p>IT`S WORK! THANK YOU VERY MUCH</p>
<p>SIP</p>
<p>Hey Can you help me i want to make a setup such that i can pass current from battery to arduino board with android phone i want to make a door lock like thing with a servo.....plzzzz guide me </p>
<p>guys pls can anyone upload the images of the connections...i am havin problem with it..</p><p>Thanx in advance..</p>
<p>Hi, how do you take in data from arduino to android?Becoz right now I can only do Android to arduino.</p>
<p>Hi i need your help please :) my problem is when i create an android app the same that your are developed included : 2 code java: Connectivity + Arduino activity + layout XML with 2 button + AndroidManifest that included two permission :internet and ACCESS_NETWORK_STATE </p><p>the probleme is :IT DOESN'T WORK !! i send you 3 images pleas check it to help me</p>
cool! you should try controlling motors with this application!
correct,,we are trying to control many appliances also..!!!!
<p>So...How to control servo with this code?</p><p>What code could be added to control it?</p>
what is the code if ill use arduino wifi shield?tnx for the help
<p>Did you figure out the answer to this question? I am looking for a solution to the same thing, kindly reply if you can assist!</p>
<p>If you don't want to learn Android coding check out the pfodDesigner app (see this tutorial http://www.forward.com.au/pfod/pfodDesigner/index.html)</p><p>The pfodDesigner lets you interactively design custom android menus and then generates the Arduino sketch that servers the menu, via pfodApp, and handles the users commands. </p><p>Here is an example menu designed by pfodDesigner. No Android programming required.</p>
<p>Hello..! how to control more than 1 led.. I mean 8 LEDs or more</p><p>I'm try to make android application but still not work</p><p>can you help me?</p>
Hello! where can I download .apk file?? <br>Thanks in advance!!
<p>Hey guys, thanks a lot!</p>
thanks a lot. Its very help example
Thanks for the example// <br>As this program help in turning led on/off from same network.. but how to glow led using android app from the internet <br>
For all the way when the new application was created then it will having error said &quot;error android.os.networkonmainthreadexception&quot; message once created and press to on button. <br> <br>Under androidmanifest file: <br> <br>Original : <br>android:minSdkVersion=&quot;8&quot; <br>android:targetSdkVersion=&quot;15&quot; /&gt; <br> <br>Change to : <br>android:minSdkVersion=&quot;8&quot; /&gt; <br> <br>will work back to normal without error because this error will appear to those version 8 onward only. <br> <br>By the way this problem can actually solve by using AsyncTask and I modified the code to have liked this and working without manually modified manifest files's content. <br> <br>import java.net.DatagramPacket; <br>import java.net.DatagramSocket; <br>import java.net.InetAddress; <br> <br>import android.app.Activity; <br> <br>import android.content.Context; <br> <br>import android.net.ConnectivityManager; <br>import android.net.NetworkInfo; <br>import android.os.AsyncTask; <br>import android.os.Bundle; <br>import android.widget.CompoundButton; <br>import android.widget.CompoundButton.OnCheckedChangeListener; <br>import android.widget.Toast; <br>import android.widget.ToggleButton; <br> <br>public class ArduinoActivity extends Activity { <br>/** Called when the activity is first created. */ <br> <br>ToggleButton on; <br> <br>byte target; <br>String serverHostname1; <br>DatagramSocket d1; <br>InetAddress ip,retiip; <br>DatagramPacket send_on,rec,send_off; <br>String modifiedSentence; <br>String vst; <br> <br>OnCheckedChangeListener on123 = new OnCheckedChangeListener() { <br> <br>public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { <br>if (buttonView.getId() == R.id.on) { <br>new AsyncTask() { <br>protected Void doInBackground(Boolean... params) { <br>try { <br>TurnLight(target, params[0]); <br>} catch (Exception e) { <br>// TODO Auto-generated catch block <br>e.printStackTrace(); <br>} <br>return null; <br>} <br>}.execute(isChecked); <br>} <br> <br>} <br>}; <br> <br> <br>private Boolean isOnline() { <br>//ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); <br> <br>ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); <br>NetworkInfo ni = cm.getActiveNetworkInfo(); <br>if(ni != null &amp;&amp; ni.isConnected()) <br> <br> <br>return true; <br> <br> <br> <br>return false; <br> <br>} <br>/* <br>public void led(String s) throws Exception <br>{ <br> <br>byte[] b=(s.getBytes()); <br>if(isOnline()) <br>//if(ni.isConnected()) <br>{ <br> <br>// byte[] receiveData = new byte[1024]; <br> <br>serverHostname1 = new String (&quot;192.168.9.177&quot;); <br>//Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show(); <br>ip = InetAddress.getByName(serverHostname1); <br>//Toast.makeText(getApplicationContext(),&quot;ip&quot;,Toast.LENGTH_LONG).show(); <br> <br>//try{ <br> <br>d1 = new DatagramSocket();//} <br>//catch(Exception e){ <br>// e.printStackTrace(); <br>//Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show(); <br>//} <br> <br>//Toast.makeText(getApplicationContext(),&quot;socket&quot;,Toast.LENGTH_LONG).show(); <br>try{ <br>send = new DatagramPacket(b,b.length, ip, 8080); <br>}catch(Exception e){ <br>//Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show(); <br>} <br> <br>//Toast.makeText(getApplicationContext(),&quot;packet&quot;,Toast.LENGTH_LONG).show(); <br> <br>d1.send(send); <br>//Toast.makeText(getApplicationContext(),&quot;sent&quot;,Toast.LENGTH_LONG).show(); <br> <br>//rec = new DatagramPacket(receiveData, receiveData.length); <br>d1.setSoTimeout(10000); <br>d1.receive(rec); <br>modifiedSentence = new String(rec.getData()); <br>InetAddress returnIPAddress = rec.getAddress(); <br>//int port = rec.getPort(); <br>//Toast.makeText(getApplicationContext(),&quot;Reply:&quot;+modifiedSentence,Toast.LENGTH_LONG).show(); <br>Toast.makeText(getApplicationContext(),&quot;Reply from Server:&quot;+returnIPAddress,Toast.LENGTH_LONG).show(); <br> <br>d1.close(); <br>} <br>else <br>{ <br>Toast.makeText(getApplicationContext(),&quot;No network&quot;,Toast.LENGTH_LONG).show(); <br>} <br>} <br>*/ <br>public void onCreate(Bundle savedInstanceState) { <br> <br> <br>super.onCreate(savedInstanceState); <br>setContentView(R.layout.main); <br>on=(ToggleButton)findViewById(R.id.on); <br>on.setOnCheckedChangeListener(on123); <br>//off=(ToggleButton)findViewById(R.id.off); <br> <br> <br>/*on.setOnClickListener(new View.OnClickListener() { <br> <br>public void onClick(View v) { <br>// TODO Auto-generated method stub <br> <br>try { <br>//ArduinoActivity a=new ArduinoActivity(); <br> <br>led(&quot;1&quot;); <br>Toast.makeText(getApplicationContext(),&quot;ON&quot;,Toast.LENGTH_LONG).show(); <br> <br> <br>} catch (Exception e) { <br>// TODO Auto-generated catch block <br>System.out.println(&quot;Error::&quot;+e); <br>} <br> <br>} <br>});*/ <br> <br> <br> <br> <br> <br> <br>} <br> <br>/*public void TurnOnOff (View v){ <br>if (on.isChecked()){ <br> <br>try{ <br> <br>led(&quot;2&quot;); <br>Toast.makeText(getApplicationContext(),&quot;ON&quot;,Toast.LENGTH_LONG).show(); <br> <br>}catch (Exception e) { <br>//TODO Auto-generated catch block <br>System.out.println(&quot;Error::&quot;+e); <br>} <br>} <br>else <br>{ <br>try { <br>//ArduinoActivity b=new ArduinoActivity(); <br>led(&quot;1&quot;); <br>Toast.makeText(getApplicationContext(), &quot;OFF&quot;,Toast.LENGTH_LONG).show(); <br> <br>} catch (Exception e) { <br>//TODO Auto-generated catch block <br>System.out.println(&quot;Error::&quot;+e); <br>} <br> <br>} <br>}*/ <br> <br> <br>public void TurnLight (byte target, boolean isSwitchedOn) throws Exception{ <br> <br>d1 = new DatagramSocket(); <br>serverHostname1 = new String (&quot;192.168.9.177&quot;); <br>byte b[]; <br> <br>if (isOnline()){ <br> <br>if (isSwitchedOn){ <br> <br>try{ <br> <br> <br>vst=&quot;2&quot;; <br>b = (vst.getBytes()); <br> <br>ip = InetAddress.getByName(serverHostname1); <br> <br> <br>try{ <br>send_off = new DatagramPacket(b,b.length, ip, 8080); <br>}catch(Exception e){ <br> <br>} <br> <br> <br> <br>d1.send(send_off); <br> <br>d1.setSoTimeout(10000); <br>d1.receive(rec); <br>modifiedSentence = new String(rec.getData()); <br>InetAddress returnIPAddress = rec.getAddress(); <br> <br>Toast.makeText(getApplicationContext(),&quot;Reply from Server:&quot;+returnIPAddress,Toast.LENGTH_LONG).show(); <br> <br>d1.close(); <br> <br> <br> <br> <br> <br>}catch (Exception e) { <br>//TODO Auto-generated catch block <br>System.out.println(&quot;Error::&quot;+e); <br>} <br>} <br>else <br>{ <br>try { <br> <br>vst=&quot;1&quot;; <br>b = (vst.getBytes()); <br> <br>ip = InetAddress.getByName(serverHostname1); <br>//Toast.makeText(getApplicationContext(),&quot;socket&quot;,Toast.LENGTH_LONG).show(); <br> <br>try{ <br>send_on = new DatagramPacket(b,b.length, ip, 8080); <br>}catch(Exception e){ <br>//Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show(); <br>} <br> <br> <br> <br>d1.send(send_on); <br> <br>d1.setSoTimeout(10000); <br>d1.receive(rec); <br>modifiedSentence = new String(rec.getData()); <br>InetAddress returnIPAddress = rec.getAddress(); <br> <br>Toast.makeText(getApplicationContext(),&quot;Reply from Server:&quot;+returnIPAddress,Toast.LENGTH_LONG).show(); <br> <br>d1.close(); <br> <br>} catch (Exception e) { <br>//TODO Auto-generated catch block <br>System.out.println(&quot;Error::&quot;+e); <br>} <br> <br>} <br> <br>} <br>else { <br> <br>Toast.makeText(getApplicationContext(),&quot;No network&quot;,Toast.LENGTH_LONG).show(); <br> <br>} <br> <br>} <br> <br> <br> <br>} <br>
add internet access permission to your android manifest..!!

About This Instructable

110,021views

86favorites

License:

More by nviradia:Making an Android application for Arduino(LED Turn ON/OFF) Arduino desktop application on java in LAN LCD dispaly on Arduino UNO 
Add instructable to: