Get request using ESP8266 01 with arduino uno

Hi,
I am using ESP8266 01 with Arduino UNO to get data from web API using AT Commands.

I need to omit the header from the response. And the data returned is mixed and not correct.

My code is

#include <SoftwareSerial.h>
#define RX 6
#define TX 7
String AP ="AndroidAP"; 
String PASS ="mvvu9640"; 
String HOST = "smart-dress-hanger.brain.lk";
String PORT = "80";
String field = "Id";
int countTrueCommand;
int countTimeCommand; 
boolean found = false; 
SoftwareSerial esp8266(RX,TX); 
 
String Data ="";
void setup() {
  Serial.begin(9600);
  esp8266.begin(115200);
  sendCommand("AT",5,"OK",false);
  esp8266.println("AT+UART_DEF=9600,8,1,0,0");
  delay(1000);
  esp8266.end();
  esp8266.begin(9600);
  ConnectToWifi();
}
void loop() {
  
String uri="/api/ip-addresses/18787/weather";

String getData=
"GET " + uri + " HTTP/1.0\n" +
"Host: " + HOST + "\n" +
"Accept: application/json\n" +
"Content-Type: application/json\n" +
"Connection: Keep-Alive\n" +
"\n";



 sendCommand("AT+CIPMUX=1",5,"OK",false);
 sendCommand("AT+CIPSTART=4,\"TCP\",\""+ HOST +"\","+ PORT,15,"OK",false);
 sendCommand("AT+CIPSEND=4," +String(getData.length()+4),4,">",false);


 sendCommand(getData,20,"OK",true);
 delay(1500);
 countTrueCommand++;
 sendCommand("AT+CIPCLOSE=0",5,"OK",false);
}

bool ConnectToWifi(){
  for (int a=0; a<15; a++)
  {
    sendCommand("AT",5,"OK",false);
    sendCommand("AT+CWMODE=1",5,"OK",false);
    boolean isConnected = sendCommand("AT+CWJAP=\""+ AP +"\",\""+ PASS +"\"",20,"OK",false);
    if(isConnected)
    {
      return true;
    }
  }
}

bool sendCommand(String command, int maxTime, char readReplay[],boolean isGetData) {
  boolean result=false;

  //Test Purpose
  Serial.print(countTrueCommand);
  Serial.print(". at command => ");
  Serial.print(command);
  Serial.print(" ");
  while(countTimeCommand < (maxTime*1))
  {
    esp8266.println(command);
    if(esp8266.find(readReplay))//ok
    {   
      if(isGetData)
      {      
        if(esp8266.find(readReplay))
        {
          Serial.println("Success : Request is taken from the server");
        }
        while(esp8266.available())
        {
            char character = esp8266.read()
            Data.concat(character); /
            if (character == '\n')
             {
             Serial.print("Received: ");
             Serial.println(Data);
             delay(50);
             Data = "";
        } 
        }         
      }
      result = true;
      break;
    }
    countTimeCommand++;
  }
  
  if(result == true)
  {
    Serial.println("Success");
    countTrueCommand++;
    countTimeCommand = 0;
  }
  
  if(result == false)
  {
    Serial.println("Fail");
    countTrueCommand = 0;
    countTimeCommand = 0;
  }
  
  found = false;
  return result;
 }

The Result is as Below

Please help me out of this problem.

The JSON result does look garbled. It should be this from http://smart-dress-hanger.brain.lk/api/ip-addresses/18787/weather :

{"data":{"id":2,"ip_address_id":2,"temperature":"35.00","humidity":"40.00","created_at":"2019-02-18 12:58:03","updated_at":"2019-03-09 00:22:05","ip_address":{"id":2,"ip":"18787","created_at":"2019-02-18 12:46:04","updated_at":"2019-02-18 12:46:04"}}}

You are making intense use of Strings which is known to cause storage fragmentation of the small AVR based devices (UNO etc.)

Here, for example, just print the character found immediately, instead of concatenating to a String first.

        while(esp8266.available())
        {
            char character = esp8266.read()
            Data.concat(character); /
            if (character == '\n')
             {
             Serial.print("Received: ");
             Serial.println(Data);
             delay(50);
             Data = "";
        }
        }

If that works or improves the situation, use the reserve() method of the String object to save enough space for your big Strings and define them as global.

Certainly, don't do this in the loop():

String getData=
"GET " + uri + " HTTP/1.0\n" +
"Host: " + HOST + "\n" +
"Accept: application/json\n" +
"Content-Type: application/json\n" +
"Connection: Keep-Alive\n" +
"\n";

instead, define getData as a global and set its value in setup().

esp8266.begin(115200); swSerial is not reliable at that speed ! definitely not for reception, either use the hwSerial to communicate with the ESP or flash the firmware to a version that uses 9600 BAUD.
And even better... drop this method, go for letting the ESP do all the WIFI format and everything and use the Arduino IDE to upload a sketch that does all you want to do (including any communication with an arduino)

I also learned something. He is using ultimately using 9600 to talk to the ESP by this construct:

  esp8266.begin(115200);
  sendCommand("AT",5,"OK",false);
  esp8266.println("AT+UART_DEF=9600,8,1,0,0");
  delay(1000);
  esp8266.end();
  esp8266.begin(9600);  // *********************

6v6gt:
I also learned something. He is using ultimately using 9600 to talk to the ESP by this construct:

Oh yeah !! He may get away with that, swSerial is reliable for sending at 115200.

But still, using the AT commands is a very clumsy way (compiling a program to give commands to an interpreter) of doing it, and those 'String' variables will get you into trouble at some point