Returning data from AT+CIPSTART from Arduino Uno

I’m trying to get data from a GET request when I do this directly using the serial monitor (from Arduino ) everything is working as expected. However, when I try and do the same thing from the Arduino sketch everything is great apart from getting a response from the GET request. I have tried different things to get all the response but it just looks like it gives up part way through getting the request!

Any help and I mean any help would be greatly appreciated!

Note: Everything between the # I have changed for the reasons

Serial Monitor Input:

AT+RST
AT+CWMODE_CUR=3
AT+CIPSTA_CUR="192.168.1.160","192.168.1.1","255.255.255.0"
AT+CIPSTAMAC_CUR="80:7D:3A:33:6C:53"
AT+CWJAP_CUR="#MYWIFI#","#MYWIFIPASS#"
AT+CIPSTART="TCP","#99.999.99.999#",80
AT+CIPSEND=101
GET /api/#Method# HTTP/1.1
Host:#hostname#
Connection:close
*HIT ENTER KEY*

Serial Monitor Output:

AT+RST

OK

 ets Jan  8 2013,rst cause:2, boot mode:(3,7)

load 0x40100000, len 2592, room 16 
tail 0
chksum 0xf3
load 0x3ffe8000, len 764, room 8 
tail 4
chksum 0x92
load 0x3ffe82fc, len 676, room 4 
tail 0
chksum 0x22
csum 0x22

2nd boot version : 1.7(5d6f877)
SPI Speed : 40MHz
SPI Mode : QIO
SPI Flash Size & Map: 16Mbit(1024KB+1024KB)
jump to run user1 @ 1000

⸮⸮⸮⸮o⸮;⸮⸮g|⸮l⸮⸮alc⸮⸮|s⸮d⸮g⸮⸮g⸮
ready
AT+CWMODE_CUR=3

OK
AT+CIPSTA_CUR="192.168.1.160","192.168.1.1","255.255.255.0"

OK
AT+CIPSTAMAC_CUR="80:7D:3A:33:6C:53"

OK
AT+CWJAP_CUR="#MYWIFI#","#MYWIFIPASS#"
WIFI CONNECTED
WIFI GOT IP

OK
AT+CIPSTART="TCP","#99.999.99.999#",80
CONNECT

OK
AT+CIPSEND=101

OK
> 
Recv 101 bytes

SEND OK

+IPD,223:HTTP/1.1 200 OK
Content-Length: 36
Content-Type: text/plain; charset=utf-8
Server: Microsoft-IIS/10.0
X-Powered-By: ASP.NET
Date: Fri, 22 Mar 2019 07:21:54 GMT
Connection: close

Its working baby 3/22/2019 - 7:21 AMCLOSED

Arduino Sketch:

#include <SoftwareSerial.h>


#define RX 10
#define TX 11

SoftwareSerial esp8266(RX,TX);

String wifiSSID = "#MYWIFI#";
String wifiPass = "#MYWIFIPASS#";

String hostIp = "#99.999.99.999#";
String port = "80";

int countTrueCommand;
int countTimeCommand; 

int wifiStep = 0;

void setup() {
  // put your setup code here, to run once:

  Serial.begin(115200);
  Serial.println("Starting...");

  esp8266.begin(115200);
  
  delay(2000);

}

void loop() {

  switch(wifiStep)
  {
    case 0:
      //Basic AT check    
      //sendBasicCommand("AT",5,"OK", 1);
      wifiStep = 1;
    break;

    case 1:
      //Reset
      sendBasicCommand("AT+RST",10,"OK", 2);
      
      //wifiStep = 2;
    break;

    case 2:
      //Set Mode
      sendBasicCommand("AT+CWMODE_CUR=1",5,"OK", 3);
      
    break;

    case 3:
      //Set IP address range
      sendBasicCommand("AT+CIPSTA_CUR=\"192.168.1.160\",\"192.168.1.1\",\"255.255.255.0\"",5,"OK", 4);
    break;

    case 4:
      //Set Mac address
      sendBasicCommand("AT+CIPSTAMAC_CUR=\"80:7D:3A:33:6C:53\"",5,"OK", 5);
    break;

    case 5:
      //Connect to router
      sendBasicCommand("AT+CWJAP_CUR=\""+ wifiSSID + "\",\"" + wifiPass + "\"",15,"OK", 6);
    break;

    case 6:
      //Create Connection Connection
      sendBasicCommand("AT+CIPSTART=\"TCP\",\"" + hostIp + "\"," + port + "",5,"OK", 7);
    break;

    case 7:
      //Setup call
      sendBasicCommand("AT+CIPSEND=101", 5, ">", 8);
      delay(500);
    break;

    case 8:
      //Call API
      Serial.println("CallingAPI");
      callBuildApi();
      delay(5000);
      wifiStep = 9;
    break;
    case 9:
      //Close Connection
      //sendBasicCommand("AT+CIPCLOSE",5,"OK", 6);
      wifiStep = 6;
      delay(5000);      
    break;
    
    default:
      wifiStep = 0;
    break;
  }

  delay(500);
}

void sendBasicCommand(String command, int maxTime, char readReplay[], int nextStep) {
   Serial.print("command ");
   Serial.print(command);

    boolean found = false;
  
  while(countTimeCommand < (maxTime*1))
  {
    char data = esp8266.read();
    esp8266.println(command);
    if(esp8266.find(readReplay))
    {
      found = true;
      break;
    }
  
    countTimeCommand++;
  }
  
  if(found == true)
  {
    Serial.print(" (good)");
    countTrueCommand++;
    countTimeCommand = 0;

    if(nextStep != -1){
      wifiStep = nextStep;
    }
  }
  
  if(found == false)
  {
    Serial.print(" (fail)!!!!!!!!!");
    countTrueCommand = 0;

    //if failed then reset the wifi status to 0 so start the process again from the beginning
    wifiStep = 0;
  }
  
  Serial.println("");

 }

 void callBuildApi(){

  esp8266.println("GET /api/LastestBuildInfo HTTP/1.1");
  esp8266.println("Host:#HOST#");
  esp8266.println("Connection:close");
  esp8266.println("");
  
  while (esp8266.available() > 0)
  {
    esp8266.read();
    delay(1);
  }
  String data = esp8266.readStringUntil("CLOSE");

  Serial.println(data);
 }

Arduino Sketch Serial Output:

command AT+RST (good)
command AT+CWMODE_CUR=1 (good)
command AT+CIPSTA_CUR="192.168.1.160","192.168.1.1","255.255.255.0" (good)
command AT+CIPSTAMAC_CUR="80:7D:3A:33:6C:53" (good)
command AT+CWJAP_CUR="#MYWIFI#","#MYWIFIPASS#" (good)
command AT+CIPSTART="TCP","#99.999.99.999#",80 (good)
command AT+CIPSEND=101 (good)
CallingAPI

SEND OK

+IPD,223:HTTP/1.1 200 OK
Content-Length: 36
Content-Tytcfeo
BT,9Mo
 0M

Sorry just to be clear this is using a ESP8266

I found the tutorial WiFi Module ESP8266 – 2. TCP CLIENT /Server mode | alselectro to be helpful

The AT+CIPSEND command requires two parameters.
That’s what I use in my Arduino sketch.

Software serial doesn't work reliably at 115200 baud. You need to use the appropriate AT command to configure the ESP8266 AT firmware to communicate at a lower speed (19200 would be reasonable).

while (esp8266.available() > 0) stops at first gap in data stream

// print response
if (esp8266.find("+IPD,")) { // response received
 int l = esp8266.parseInt();
 while (l > 0) {
   if (esp8266.available()) {
     Serial.write(esp8266.read());
     l--;
  }
}

pert: Software serial doesn't work reliably at 115200 baud. You need to use the appropriate AT command to configure the ESP8266 AT firmware to communicate at a lower speed (19200 would be reasonable).

Thank you for this info I now have two SoftwareSerial instances for the ESP8266 one at 115200 and another at 19200. The 115200 one just calls the

AT+UART_DEF=19200,8,1,0,0

command and then this rest is done under 19200.

It is a little annoying as the docs do say 115200 is supported but this workaround is working a treat

MasterGecko: Thank you for this info I now have two SoftwareSerial instances for the ESP8266 one at 115200 and another at 19200. The 115200 one just calls the

AT+UART_DEF=19200,8,1,0,0

command and then this rest is done under 19200.

It is a little annoying as the docs do say 115200 is supported but this workaround is working a treat

_DEF sets the default and it is remembered by esp8266 on flash.

Juraj: _DEF sets the default and it is remembered by esp8266 on flash.

Yes it does until you call AT+RST (which i do when things are in a pickle) and then it reset the baud rate back to 115200

MasterGecko: Yes it does until you call AT+RST (which i do when things are in a pickle) and then it reset the baud rate back to 115200

AT+RESTORE deletes settings. RST is simple reset

MasterGecko: It is a little annoying as the docs do say 115200 is supported but this workaround is working a treat

I don't know what the story is with that number in the documentation. Maybe that's under ideal conditions? I did a crude test with Serial Monitor and started seeing corruption of the received data at anything above 38400, though there were no issues with the transmitted data at 115200.