title: Communication between Arduino and android via ethernet shield
body: <p>I'm using Android to toggle my led light with Arduino and ethernet shield. It didn't work despite having no problems during compilation neither on Android nor Arduino. I connected the ethernet shield with my computer and I gave it the same address as my computer.</p>
  
  <p>This is my Android code:</p>
  
  <pre><code>import android.os.AsyncTask;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.MotionEvent;
  import android.view.View;
  import android.widget.Button;
  import android.widget.CompoundButton;
  import android.widget.Switch;
  import android.widget.ToggleButton;
  import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  import java.net.HttpURLConnection;
  import java.net.URL;
  
  public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      /********************************/
       /*    Define all the buttons    */
      /********************************/
      Switch led1 = (Switch) findViewById(R.id.Led1);
      /*******************************************************/
       /*  Set an onclick/onchange listener for every button  */
      /*******************************************************/
      led1.setOnCheckedChangeListener(new        CompoundButton.OnCheckedChangeListener() {
          public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
              if (isChecked) {
                  /* Switch is led 1 */
                  new Background_get().execute("led1=1");
              } else {
                  new Background_get().execute("led1=0");
              }
          }
      });
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
      return super.onOptionsItemSelected(item);
  }
  
  /*****************************************************/
  /*  This is a background process for connecting      */
  /*   to the arduino server and sending               */
  /*    the GET request withe the added data           */
  /*****************************************************/
  
  private class Background_get extends AsyncTask&lt;String, Void, String&gt; {
      @Override
      protected String doInBackground(String... params) {
          try {
              /* Change the IP to the IP you set in the arduino sketch */
              URL url = new URL("http://169, 254, 128, 184/?" + params[0]);
              HttpURLConnection connection = (HttpURLConnection)     url.openConnection();
              BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              StringBuilder result = new StringBuilder();
              String inputLine;
              while ((inputLine = in.readLine()) != null)
                  result.append(inputLine).append("\n");
              in.close();
              connection.disconnect();
              return result.toString();
          } catch (IOException e) {
              e.printStackTrace();
          }
          return null;
      }
  }
  }
  </code></pre>
  
  <p>And this is the Arduino code:</p>
  
  <pre><code>#include &lt;SPI.h&gt;
  #include &lt;Ethernet.h&gt;
  
  // Enter a MAC address and IP address for your controller below.
  // The IP address will be dependent on your local network:
  byte mac[] = {
    0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
  };
  IPAddress ip(169, 254, 128, 184);
  
  // Initialize the Ethernet server library
  // with the IP address and port you want to use
  // (port 80 is default for HTTP):
  EthernetServer server(80);
  
  void setup() {
    // Open serial communications and wait for port to open:
    Serial.begin(9600);
    while (!Serial) {
      ; // wait for serial port to connect. Needed for native USB port only
    }
    // start the Ethernet connection and the server:
    Ethernet.begin(mac, ip);
    server.begin();
    Serial.print("server is at ");
    Serial.println(Ethernet.localIP());
    // Pin 5
    pinMode(5, OUTPUT);
  }
  
  void loop() {
    // listen for incoming clients
    EthernetClient client = server.available();
    if (client) {
      Serial.println("new client");
      // an http request ends with a blank line
      boolean currentLineIsBlank = true;
      String buffer = "";
      while (client.connected()) {
        if (client.available()) {
          char c = client.read();
          Serial.write(c);
          buffer += c;
          // 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
          if (c == '\n' &amp;&amp; currentLineIsBlank) {
            // send a standard http response header
            client.println("HTTP/1.1 200 OK");
            client.println("Content-Type: text/html");
            client.println("Connection: close");
            // the connection will be closed after completion of the response
            client.println("Refresh: 5");
            // refresh the page automatically every 5 sec
            client.println();
            break;
          }
          if (c == '\n') {
            // you're starting a new line
            currentLineIsBlank = true;
            buffer = "";
          } else if (c != '\r') {
            if (buffer.indexOf("GET /?led1=1") &gt;= 0) { // If led1 = 1
              digitalWrite(5, HIGH); // led 1 &gt; on
            }
            if (buffer.indexOf("GET /?led1=0") &gt;= 0) { // If led1 = 0
              digitalWrite(5, LOW); // led 1 &gt; off
            }
          } else {
            // you've gotten a character on the current line
            currentLineIsBlank = false;
          }
        }
      }
      // give the web browser time to receive the data
      delay(1);
      // close the connection:
      client.stop();
      Serial.println("client disconnected");
    }
  }
  </code></pre>