title: sending and receiving data using android application, arduino, and esp8266 wifi module
body: <p>I am quite stuck with these for a few days, I am using the android application to control the motors attached to arduino uno, I can establish the connection and send data but the problem is that it has limitation on how much it can respond to the button being tapped, and sometimes it cannot execute the command accurately. 
  This is the codes for arduino </p>
  
  <pre><code>    void loop()
      {
      if(esp8266.available()) // check if the esp is sending a message 
      {
      Serial.println("Something received"); 
      delay(50);
      if(esp8266.find("+IPD,"))
      {
          String action;
          Serial.println("+IPD, found");
          int connectionId = esp8266.read()-48; // subtract 48 because the read() function returns 
                                             // the ASCII decimal value and 0 (the first decimal number) starts at 48
          Serial.println("connectionId: " + String(connectionId));
  
          esp8266.find("motor=");
          char s = esp8266.read();
          delay(50);
          //right
          if(s=='1'){
            action = "Motor is turning right";
            digitalWrite(5,HIGH);
            digitalWrite(6,LOW);
            digitalWrite(9,LOW);
            digitalWrite(10,HIGH);
          }
          else if(s=='0'){
            action = "Motor is stopping";
            digitalWrite(5,LOW);
            digitalWrite(6,LOW);
            digitalWrite(9,LOW);
            digitalWrite(10,LOW);
          }
  
          //left
           else if(s=='2'){
            action = "Motor is turning left";
            digitalWrite(5,LOW);
            digitalWrite(6,HIGH);
            digitalWrite(9,HIGH);
            digitalWrite(10,LOW);
          }
  
          //forward
           else if(s=='3'){
            action = "Motor is moving forward";
            digitalWrite(5,HIGH);
            digitalWrite(6,LOW);
            digitalWrite(9,HIGH);
            digitalWrite(10,LOW);
          }
  
          //backward
            else if(s=='4'){
            action = "Motor is moving backward";
            digitalWrite(5,LOW);
            digitalWrite(6,HIGH);
            digitalWrite(9,LOW);
            digitalWrite(10,HIGH);
          }
  
          else{
            action = "Try another command";
          }
          Serial.println(action);
        sendHTTPResponse(connectionId, action);
  
    }
  }
  }
  void sendHTTPResponse(int id, String content)
  {
    String response;
    response = "HTTP/1.1 200 OK\r\n";
    response += "Content-Type: text/html; charset=UTF-8\r\n"; 
    response += "Content-Length: ";
    response += content.length();
    response += "\r\n";
    response +="Connection: close\r\n\r\n";
    response += content;
  
    String cmd = "AT+CIPSEND=";
    cmd += id;
    cmd += ",";
    cmd += response.length();
  
   Serial.println("--- AT+CIPSEND ---");
   sendESP8266Cmdln(cmd, 50);
  
   Serial.println("--- data ---");
   sendESP8266Data(response, 50);
  
  }
  void sendESP8266Cmdln(String cmd, int waitTime)
  {
    esp8266.println(cmd);
    delay(waitTime);
    clearESP8266SerialBuffer();
  }
  
  void sendESP8266Data(String data, int waitTime)
  {
    esp8266.print(data);
    delay(waitTime);
    clearESP8266SerialBuffer();
  }
  
  void clearESP8266SerialBuffer()
  {
    Serial.println("= clearESP8266SerialBuffer() =");
    while (esp8266.available() &gt; 0) {
      char a = esp8266.read();
      Serial.write(a);
  
    }
    Serial.println("==============================");
  }
  </code></pre>
  
  <p>This is the codes for android  </p>
  
  <pre><code>public class FeedTask extends AsyncTask&lt;String, Void, String&gt; {
  
       @Override
          protected String doInBackground(String... params) {
              try {
                  //change IP to the IP you set in the ARDUINO
                  URL url = new URL("http://192.168.254.101/?" + 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;
          }
      }
  
      public void Motion() {
  
          Button right = (Button) findViewById(R.id.button_right);
          Button left = (Button) findViewById(R.id.button_left);
          Button forward = (Button) findViewById(R.id.button_forward);
          Button backward = (Button) findViewById(R.id.button_backward);
          Button led_on = (Button) findViewById(R.id.led_on);
          Button led_off = (Button) findViewById(R.id.led_off);
  
  
          right.setOnTouchListener(new OnTouchListener() {
  
              @Override
              public boolean onTouch(View v, MotionEvent event) {
                  if (event.getAction() == MotionEvent.ACTION_DOWN) {
                      TextView text = (TextView) findViewById(R.id.textView);
                      text.setText("Right");
                      new FeedTask().execute("motor=1");
  
                  } else if (event.getAction() == MotionEvent.ACTION_UP) {
                      TextView text = (TextView) findViewById(R.id.textView);
                      text.setText("Tap again");
                      new FeedTask().execute("motor=0");
                  }
  
                  return true;
              }
          });
  </code></pre>
  
  <p>I am also working on sending data using arduino back to android, but with these errors occuring i cannot proceed. I've read that there is a limitation on how much a html can only handle or so, i do not dig too deep with that. The errors that I am having through viewing serial windows is "Accept-ecoding gzip" and sometimes being stuck with "something received" and do not respond to any buttons tapped later on.</p>