Communication problem between Arduino and Android

Hi ,I created the kettle controlled over Wi-Fi. I used Wi_fi module ESP8266, Arduino , water sensor, relay and temperature sensor. Kettle works almost fine at first start, so everything I have in code for Arduino works properly. Only problem is when i need to get data to Android app over Wi-Fi. I used AsyncTask client in the application and sometimes i can’t connect to my kettle or when I want to get actual temperature it goes well for example for 20°C but then the actuall temperature stucks there. When this occurs I re-open the app connect again and then it works again. The kettle switches off correctly so the android is getting right data only the connection between ESP8266 and Android app is a bit buggy. Is there any other responsible possibility to establish communication between ESP and Android to send right data for full cycle ?

#include <OneWire.h>       
OneWire  ds(9);             
int nPinWaterState = 8;     
int nPinRelayState = 7;          
byte addr[8];              
bool bRelay=false;           
bool bWater=true;           
int nTemp=0;            
int nFinishTemp=-1;    
void setup()
void setup()
{
  Serial.begin(9600);                                     
  InitDS18B20();                                          
  pinMode(nPinWaterState, INPUT);                          
  pinMode(nPinRelayState, OUTPUT);                              
  digitalWrite(nPinRelayState,HIGH);                           

  sendData("AT+RST\r\n",1000);                            
  sendData("AT+CWSAP=\"Kettle\",\"123\",3,0\r\n",200);   
  sendData("AT+CWMODE=2\r\n",200);                        
  sendData("AT+CIPAP=\"192.168.0.100\"\r\n",200);         
  sendData("AT+CWDHCP=0,1\r\n",200);                      
  sendData("AT+CIPMUX=1\r\n",200);                        
  sendData("AT+CIPSERVER=1,8888\r\n",200);                
 }    
void loop()
{
  ReadSensors();                                                    
  if(!bWater || nTemp>=nFinishTemp)                           
  {
     bRelay=false;
  }

  if(Serial.available())                                            
  {
    if(Serial.find((char*)"+IPD,"))                                 
{

 String strRead =Serial.readStringUntil('\n');                  
 int connectionId=strRead[0]-48;                                

 if(Serial.find((char*)"AND:"))                                 
 {                                  
   strRead=Serial.readStringUntil('\n');                        
   nFinishTemp=strRead.substring(1,4).toInt();              
   if(strRead[0]=='T')                                          
   {
      if(bWater && nTemp<nFinishTemp)                     
      {
        bRelay=true;
      }
   }
   else
   {
      bZap=false;
   }
 }
 String strSend = "KET:";
    if(bRelay) 
{
strSend=strSend+'T';
}                                                              
         else
         {
            strSend=strSend+'F';
         }

 if(bWater)                                               
 {
    strSend=strSend+'T';
 }
 else
 {
    strSend=strSend+'F';
 }

 char strTemp[3];
 sprintf (strTemp, "%03d",nTemp);                 
 strSend=strSend+strTemp;
 String cipSend = "AT+CIPSEND=";
 cipSend += connectionId;
 cipSend += ",";
 cipSend +=strSend.length();
 cipSend +="\r\n";

 sendData(cipSend,20);
 sendData(strSend,20);
 String closeCommand = "AT+CIPCLOSE="; 
 closeCommand+=connectionId;                           
 closeCommand+="\r\n";
 sendData(closeCommand,20); 
}
}

   if (bRelay)                                               
   {
     digitalWrite(nPinRelayState,LOW);
   }
   else
   {
     digitalWrite(nPinRelayState,HIGH);      
   } 
}
    String sendData(String command, const int timeout)
{
    String response = "";
    Serial.print(command);                                                                             
    long int time = millis();
    while( (time+timeout) > millis())
{
  while(Serial.available())
  {                                             
    char c = Serial.read();                     
    response+=c;
  }  
}
return response;
}
void ReadSensors(void)
{                                    
  float tempC;
  tempC=ReadDS18B20();
  nTemp=(int)tempC;                                                 
  bWater = !digitalRead(nPinWaterState); 
}
    void InitDS18B20(void)
{

if ( !ds.search(addr)) {

ds.reset_search();
delay(100);
return;
  }

  if (OneWire::crc8(addr, 7) != addr[7]) {

      return;
  }
}
float ReadDS18B20(void)
{
  byte i;
  byte present = 0;                                          
  byte data[12];
  float celsius;

  ds.reset();
  ds.select(addr);  
  ds.write(0x44, 1);                        

  delay(100);                              


  present = ds.reset();
  ds.select(addr);    
  ds.write(0xBE);                          

  for ( i = 0; i < 9; i++) {               
  data[i] = ds.read();
  }
  int16_t raw = (data[1] << 8) | data[0];

  byte cfg = (data[4] & 0x60);

  if (cfg == 0x00) raw = raw & ~7;         
  else if (cfg == 0x20) raw = raw & ~3;   
  else if (cfg == 0x40) raw = raw & ~1;    


  celsius = (float)raw / 16.0;
  return(celsius);
}

You would make your code much less messy if you used one of the proper esp libraries, e.g. WiFiEsp.

With this for example you can use Serial0, Serial1, Serial2 or Serial3 to communicate with your esp8266 shield and you can also vary the baud rate until you get as fast as possible and reliable communication going on.

All of the write("AT....") sort of code can be replaced by this sort of thing WiFiEspClient client;........client.print("....");