user28709: I have a small test sketch that sends queries to an ESP8266 chip [mounted on an ESP-01 board], and these responses are invariably scrambled,
though the response varies from execution to execution, e.g., 
(execution 1)
_________[ Connect to AP ]__________
__________[ AT+CWJAP="KCTcircuit2","" ]__________
AT+CWJAX="I⸮Tcircuiz2",""H

(execution 2)
__________[ Connect to AP ]__________
__________[ AT+CWJAP="KCTcircuit2","" ]__________
AT+CWJAH="KCTcircuiz2!,""H

What could be the source of the problem?  I need to resolve this before I can start testing the real sketch.
My test sketch follows [stripped of extraneous code]:
#include <SoftwareSerial.h>
// _______________________________
#define sendit(x) ( { \
    Serial.print(F("__________[ ")); \
    Serial.print(x); \
    Serial.println(F(" ]__________")); \
    esp8266.write(x); \
    esp8266.write(F("\r\n")); \
    esp8266.readBytes(buffer, sizeof(buffer)); \
    Serial.println(buffer); \
    delay(2000); \
  } )

// _______________________________
#define desc(x) ( { \
    Serial.println(); \
    Serial.print(F("__________[ ")); \
    Serial.print(F(x)); \
    Serial.println(F(" ]__________")); \
  } )

// _______________________________
SoftwareSerial esp8266(12, 13); // RX, TX
char buffer[50];

// _______________________________
void setup()
{
    Initialise();
}

// _______________________________
void loop()
{
}

// ____________________________________________________________________________________________________
void Initialise()
{
    SerialReadBuffer.reserve(64);
    esp8266.begin(115200);
    Serial.begin(9600);
    CommandList();
}

// ____________________________________________________________________________________________________
void CommandList()
{
  desc("Test AT Startup");
  sendit("AT");
  desc("Restart the module");
  sendit("AT+RST");
  desc("Query baud rate");
  sendit("AT+CIOBAUD?");
  desc("Query firmware version");
  sendit("AT+GMR");
  desc("WIFI mode");
  sendit("AT+CWMODE?");
  desc("Get local IP address");
  sendit("AT+CIFSR=?");
  desc("Query current multiplex mode");
  sendit("AT+CIPMUX=?");
  desc("Enable echo (Sends back received command before response)");
  sendit("ATE1");
  desc("Set Station mode");
  sendit("AT+CWMODE=3");
  desc("Lists available APs");
  sendit("AT+CWLAP");
  sendit("AT+CWLAP");
    desc("Connect to AP");
  sendit("AT+CWJAP=\"KCTcircuit2\",\"\"");
  desc("Prints the SSID of Access Point ESP8266 is connected to.");
  sendit("AT+CWJAP?");
  desc("List clients connected to ESP8266 softAP");
  sendit("AT+CWLIF");
  desc("Get IP address of ESP8266 station.");
  sendit("AT+CIPSTA?");
  desc("Get information about connection");
  sendit("AT+CIPSTATUS=?");
  desc("Get local IP address");
  sendit("AT+CIFSR=?");
  desc("Get the SSID of Access Point ESP8266 is connected to.");
  sendit("AT+CWJAP?");
  desc("Get the SSID of Access Point ESP8266 is connected to -CUR");
  sendit("AT+CWJAP_CUR?");
  desc("Configuration of softAP mode");
  sendit("AT+CWSAP?");
  desc("Configuration of softAP mode = CUR");
  sendit("AT+CWSAP_CUR?");
  desc("Disconnect from AP");
  sendit("AT+CWQAP");
  Serial.println(F("__________________"));
  Serial.println();
 }

Typical output is as follows:
"ESP8266_ATcmdtest", compiled on: Jun  5 2017 at 14:00:04
Running on: Arduino UNO: ATmega328P

__________[ Test AT Startup ]__________
__________[ AT ]__________
ATn

__________[ Restart the module ]__________
__________[ AT+RST ]__________
AT+QST$

__________[ Query baud rate ]__________
__________[ AT+CIOBAUD? ]__________
AT+CHOBAUD⸮⸮

__________[ Query firmware version ]__________
__________[ AT+GMR ]__________
AT+TJ⸮⸮AUD⸮⸮

__________[ WIFI mode ]__________
__________[ AT+CWMODE? ]__________
AT+CWLODE?G⸮

__________[ Get local IP address ]__________
__________[ AT+CIFSR=? ]__________
AT+CHFSR=?⸮

__________[ Query current multiplex mode ]__________
__________[ AT+CIPMUX=? ]__________
AT+CIPMUX=r⸮

__________[ Enable echo (Sends back received command before response) ]__________
__________[ ATE1 ]__________
ATE0=PMUX=r⸮

__________[ Set Station mode ]__________
__________[ AT+CWMODE=3 ]__________
AT+CWMODE=⸮⸮

__________[ Lists available APs ]__________
__________[ AT+CWLAP ]__________
AJ+CWL@P⸮=⸮⸮
__________[ AT+CWLAP ]__________
AT+CWLAP⸮=⸮⸮

__________[ Connect to AP ]__________
__________[ AT+CWJAP="KCTcircuit2","" ]__________
AT+CWJAH="KCTcircuiz2!,""H

__________[ Prints the SSID of Access Point ESP8266 is connected to. ]__________
__________[ AT+CWJAP? ]__________
AT+CWJAP?⸮KCTcircuiz2!,""H

__________[ List clients connected to ESP8266 softAP ]__________
__________[ AT+CWLIF ]__________
AV+CWLIFx⸮KCTcircuiz2!,""H

__________[ Get IP address of ESP8266 station. ]__________
__________[ AT+CIPSTA? ]__________
AT+CIPSTA?CTcircuiz2!,""H

__________[ Get information about connection ]__________
__________[ AT+CIPSTATUS=? ]__________
AT+CIP*UQUM⸮⸮2ircuiz2!,""H

__________[ Get local IP address ]__________
__________[ AT+CIFSR=? ]__________
AT+CIFSR=?}⸮⸮2ircuiz2!,""H

__________[ Get the SSID of Access Point ESP8266 is connected to. ]__________
__________[ AT+CWJAP? ]__________
AT+CWJAP? }⸮⸮2ircuiz2!,""H

__________[ Get the SSID of Access Point ESP8266 is connected to -CUR ]__________
__________[ AT+CWJAP_CUR? ]__________
AT+CW⸮AP5UI⸮⸮⸮ircuiz2!,""H

__________[ Configuration of softAP mode ]__________
__________[ AT+CWSAP? ]__________
AT+CWSAP?HI⸮⸮⸮ircuiz2!,""H

__________[ Configuration of softAP mode = CUR ]__________
__________[ AT+CWSAP_CUR? ]__________
AT+CWSAP5UI⸮r⸮ircuiz2!,""H

__________[ Disconnect from AP ]__________
__________[ AT+CWQAP ]__________
AZ+CWPAP⸮UI⸮r⸮ircuiz2!,""H
__________________


user4143: Cause: software serial at 115200 baud. 
Solution: configure the esp8266 for a lower baud rate or use hardware serial.
The problem is basically that the Arduino cannot run 115200 baud SoftwareSerial reliably. It is common to get corruption of incoming characters, or characters completely skipped, due in part to the SoftwareSerial implementation being unable to react fast enough to the data coming in. The time between one stop bit and the next start bit is often shorter than the amount of time taken to store the data in the receive ring buffer, so the ISR cannot respond to the new start bit in time, and data is lost.  Running at a lower baud rate gives more time for the data to be stored before the next start bit arrives, thus giving the ISR time to finish and be ready to respond to the next byte coming in.
You need to change the baud rate of the ESP8266 itself (I find 9600 is a good reliable value) using one of (depending on your firmware version):
AT+CIOBAUD=9600

or
AT+UART_DEF=9600,8,1,0,0

or
AT+IPR=9600

Last time I did it on an ESP8266 it was the first one that worked. 
Then you will need to change your program to use 9600 baud instead of 115200 baud in your esp8266.begin(...).

28709: <p>I have a small test sketch that sends queries to an ESP8266 chip [mounted on an ESP-01 board], and these responses are invariably scrambled,
though the response varies from execution to execution, e.g., </p>

<p>(execution 1)</p>

<pre><code>_________[ Connect to AP ]__________
__________[ AT+CWJAP="KCTcircuit2","" ]__________
AT+CWJAX="I⸮Tcircuiz2",""H
</code></pre>

<p>(execution 2)</p>

<pre><code>__________[ Connect to AP ]__________
__________[ AT+CWJAP="KCTcircuit2","" ]__________
AT+CWJAH="KCTcircuiz2!,""H
</code></pre>

<p>What could be the source of the problem?  I need to resolve this before I can start testing the real sketch.</p>

<p>My test sketch follows [stripped of extraneous code]:</p>

<pre><code>#include &lt;SoftwareSerial.h&gt;
// _______________________________
#define sendit(x) ( { \
    Serial.print(F("__________[ ")); \
    Serial.print(x); \
    Serial.println(F(" ]__________")); \
    esp8266.write(x); \
    esp8266.write(F("\r\n")); \
    esp8266.readBytes(buffer, sizeof(buffer)); \
    Serial.println(buffer); \
    delay(2000); \
  } )

// _______________________________
#define desc(x) ( { \
    Serial.println(); \
    Serial.print(F("__________[ ")); \
    Serial.print(F(x)); \
    Serial.println(F(" ]__________")); \
  } )

// _______________________________
SoftwareSerial esp8266(12, 13); // RX, TX
char buffer[50];

// _______________________________
void setup()
{
    Initialise();
}

// _______________________________
void loop()
{
}

// ____________________________________________________________________________________________________
void Initialise()
{
    SerialReadBuffer.reserve(64);
    esp8266.begin(115200);
    Serial.begin(9600);
    CommandList();
}

// ____________________________________________________________________________________________________
void CommandList()
{
  desc("Test AT Startup");
  sendit("AT");
  desc("Restart the module");
  sendit("AT+RST");
  desc("Query baud rate");
  sendit("AT+CIOBAUD?");
  desc("Query firmware version");
  sendit("AT+GMR");
  desc("WIFI mode");
  sendit("AT+CWMODE?");
  desc("Get local IP address");
  sendit("AT+CIFSR=?");
  desc("Query current multiplex mode");
  sendit("AT+CIPMUX=?");
  desc("Enable echo (Sends back received command before response)");
  sendit("ATE1");
  desc("Set Station mode");
  sendit("AT+CWMODE=3");
  desc("Lists available APs");
  sendit("AT+CWLAP");
  sendit("AT+CWLAP");
    desc("Connect to AP");
  sendit("AT+CWJAP=\"KCTcircuit2\",\"\"");
  desc("Prints the SSID of Access Point ESP8266 is connected to.");
  sendit("AT+CWJAP?");
  desc("List clients connected to ESP8266 softAP");
  sendit("AT+CWLIF");
  desc("Get IP address of ESP8266 station.");
  sendit("AT+CIPSTA?");
  desc("Get information about connection");
  sendit("AT+CIPSTATUS=?");
  desc("Get local IP address");
  sendit("AT+CIFSR=?");
  desc("Get the SSID of Access Point ESP8266 is connected to.");
  sendit("AT+CWJAP?");
  desc("Get the SSID of Access Point ESP8266 is connected to -CUR");
  sendit("AT+CWJAP_CUR?");
  desc("Configuration of softAP mode");
  sendit("AT+CWSAP?");
  desc("Configuration of softAP mode = CUR");
  sendit("AT+CWSAP_CUR?");
  desc("Disconnect from AP");
  sendit("AT+CWQAP");
  Serial.println(F("__________________"));
  Serial.println();
 }
</code></pre>

<p>Typical output is as follows:</p>

<pre><code>"ESP8266_ATcmdtest", compiled on: Jun  5 2017 at 14:00:04
Running on: Arduino UNO: ATmega328P

__________[ Test AT Startup ]__________
__________[ AT ]__________
ATn

__________[ Restart the module ]__________
__________[ AT+RST ]__________
AT+QST$

__________[ Query baud rate ]__________
__________[ AT+CIOBAUD? ]__________
AT+CHOBAUD⸮⸮

__________[ Query firmware version ]__________
__________[ AT+GMR ]__________
AT+TJ⸮⸮AUD⸮⸮

__________[ WIFI mode ]__________
__________[ AT+CWMODE? ]__________
AT+CWLODE?G⸮

__________[ Get local IP address ]__________
__________[ AT+CIFSR=? ]__________
AT+CHFSR=?⸮

__________[ Query current multiplex mode ]__________
__________[ AT+CIPMUX=? ]__________
AT+CIPMUX=r⸮

__________[ Enable echo (Sends back received command before response) ]__________
__________[ ATE1 ]__________
ATE0=PMUX=r⸮

__________[ Set Station mode ]__________
__________[ AT+CWMODE=3 ]__________
AT+CWMODE=⸮⸮

__________[ Lists available APs ]__________
__________[ AT+CWLAP ]__________
AJ+CWL@P⸮=⸮⸮
__________[ AT+CWLAP ]__________
AT+CWLAP⸮=⸮⸮

__________[ Connect to AP ]__________
__________[ AT+CWJAP="KCTcircuit2","" ]__________
AT+CWJAH="KCTcircuiz2!,""H

__________[ Prints the SSID of Access Point ESP8266 is connected to. ]__________
__________[ AT+CWJAP? ]__________
AT+CWJAP?⸮KCTcircuiz2!,""H

__________[ List clients connected to ESP8266 softAP ]__________
__________[ AT+CWLIF ]__________
AV+CWLIFx⸮KCTcircuiz2!,""H

__________[ Get IP address of ESP8266 station. ]__________
__________[ AT+CIPSTA? ]__________
AT+CIPSTA?CTcircuiz2!,""H

__________[ Get information about connection ]__________
__________[ AT+CIPSTATUS=? ]__________
AT+CIP*UQUM⸮⸮2ircuiz2!,""H

__________[ Get local IP address ]__________
__________[ AT+CIFSR=? ]__________
AT+CIFSR=?}⸮⸮2ircuiz2!,""H

__________[ Get the SSID of Access Point ESP8266 is connected to. ]__________
__________[ AT+CWJAP? ]__________
AT+CWJAP? }⸮⸮2ircuiz2!,""H

__________[ Get the SSID of Access Point ESP8266 is connected to -CUR ]__________
__________[ AT+CWJAP_CUR? ]__________
AT+CW⸮AP5UI⸮⸮⸮ircuiz2!,""H

__________[ Configuration of softAP mode ]__________
__________[ AT+CWSAP? ]__________
AT+CWSAP?HI⸮⸮⸮ircuiz2!,""H

__________[ Configuration of softAP mode = CUR ]__________
__________[ AT+CWSAP_CUR? ]__________
AT+CWSAP5UI⸮r⸮ircuiz2!,""H

__________[ Disconnect from AP ]__________
__________[ AT+CWQAP ]__________
AZ+CWPAP⸮UI⸮r⸮ircuiz2!,""H
__________________
</code></pre>
 4143: <p>Cause: software serial at 115200 baud. </p>

<p>Solution: configure the esp8266 for a lower baud rate or use hardware serial.</p>

<p>The problem is basically that the Arduino cannot run 115200 baud SoftwareSerial reliably. It is common to get corruption of incoming characters, or characters completely skipped, due in part to the SoftwareSerial implementation being unable to react fast enough to the data coming in. The time between one stop bit and the next start bit is often shorter than the amount of time taken to store the data in the receive ring buffer, so the ISR cannot respond to the new start bit in time, and data is lost.  Running at a lower baud rate gives more time for the data to be stored before the next start bit arrives, thus giving the ISR time to finish and be ready to respond to the next byte coming in.</p>

<p>You need to change the baud rate of the ESP8266 itself (I find 9600 is a good reliable value) using one of (depending on your firmware version):</p>

<pre><code>AT+CIOBAUD=9600
</code></pre>

<p>or</p>

<pre><code>AT+UART_DEF=9600,8,1,0,0
</code></pre>

<p>or</p>

<pre><code>AT+IPR=9600
</code></pre>

<p>Last time I did it on an ESP8266 it was the first one that worked. </p>

<p>Then you will need to change your program to use 9600 baud instead of 115200 baud in your <code>esp8266.begin(...)</code>.</p>
