ESP8266: Serial.println prints old message!

Hey guys,
Ive just started to work with ESP8266-12 board. I am trying to perform the following AT commands inside the void setup(). But I am unable to get the response from ESP8266 after each command except for the first one, which does come after all Serial.println statements have been executed. Have a look at the screenshot attached!

My objective is to print the response of each AT command after its executed. Here's my code:

#include <AltSoftSerial.h>
AltSoftSerial ESPserial;
void setup() 
{
    Serial.begin(9600);     // communication with the host computer
    
    ESPserial.begin(9600);   // Start the software serial for communication with the ESP8266

        Serial.println("Starting ESP266.....!");
    do{
    ESPserial.println("AT+RST");
    delay(1000);
    if(ESPserial.find("Ready"))
    {
            Serial.println("Module Ready!");
            espStart = true;
           if (ESPserial.available()) {
     String msg = ESPserial.readString();
     Serial.println(msg);
  }
  Serial.println("==============================");
          
          Serial.println("Listing out Access points:...");
          ESPserial.println("AT+CWLAP");
          if(ESPserial.available())
         {
             delay(1000);
          Serial.write(ESPserial.read());
         }
      
         delay(1000);
      
         Serial.println("Joining Defined AP...");
         delay(2000);
         ESPserial.println("AT+CWJAP=" + ssid +"," + pass  );
         if(ESPserial.available())
         Serial.write(ESPserial.read());
      
         delay(1000);
      
         Serial.println("Setting CWMODE=1...");
         delay(1000);
         ESPserial.println("AT+CWMODE=1");
         if(ESPserial.available())
         Serial.write(ESPserial.read());
      
         delay(1000);
      
         Serial.println("Accepting multiple connections...");
         delay(1000);
         ESPserial.println("AT+CIPMUX=1");
         if(ESPserial.available())
         Serial.write(ESPserial.read());
      
         delay(1000);
      
         Serial.println("Setting server with port value as 80.....");
         delay(1000);
         ESPserial.println("AT+CIPSERVER=1,3333");
         if(ESPserial.available())
         Serial.write(ESPserial.read());
      
         delay(1000);
      
         Serial.println("Obtaining server IP address....");
         delay(1000);
         ESPserial.println("AT+CIFSR");
         if(ESPserial.available())
         Serial.write(ESPserial.read());
    }
     else
    {
      Serial.println("Module not ready!");
      espStart = false;    
    }
    }while(espStart==false);

I have attached the screenshot of my arduino serial window! Pls help me out!
Thanks in advance! :slight_smile:

 if (ESPserial.available())
      {
        delay(1000);
        Serial.write(ESPserial.read());
      }

This will only display one character. Perhaps you meant:

  delay(1000)
  while (ESPserial.available()) {
        Serial.write(ESPserial.read());
      }

This will display all characters received in the first second.

Here is an example function that will send an AT command and gather up the result. It will return the result string and display the interaction and how long the reply took to arrive so you can tune your timeouts.

String command(const char *toSend, unsigned long milliseconds) {
  String result;
  Serial.print("Sending: ");
  Serial.println(toSend);
  monitor.println(toSend);
  unsigned long startTime = millis();
  unsigned long lastCharTime = startTime;
  Serial.print("Received: ");
  while (millis() - startTime < milliseconds) {
    if (monitor.available()) {
      char c = monitor.read();
      lastCharTime = millis();
      Serial.write(c);
      result += c;  // append to the result string
    }
  }  // End timeout
  Serial.println();
  Serial.print(F("Received in "));
  Serial.print(lastReceivedTime - startTime);
  Serial.println(F(" milliseconds."));
  return result;
}

Thanks a lot @johnwasser, for the help! Worked like a charm! And the code to detect the response time of the AT commands was so helpful!