0

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
__________________
  • I also have another, simpler, sketch that accepts commands from the Serial Monitor instead. It has equivalent behaviour in terms of responses. – KC Tucker Jun 5 '17 at 21:37
2

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(...).

| improve this answer | |
  • Though not mentioned above, I have tried also 9600, 19200, 38400, 57600. All are indecipherable, except for 9600, which yields absolutely nothing. 115200 is the only one that even approximates a proper response. – KC Tucker Jun 6 '17 at 0:02
  • I have also used multiple ESP8266 chips, plus different Uno and Mega2560 boards. – KC Tucker Jun 6 '17 at 0:09
  • @KCTucker: yes, the module is set too high, that's what he's saying. once you lower it, it will not only work, it will work without the glitches you're asking about. – dandavis Jun 6 '17 at 8:17
  • @Majenko For clarification, I found that AT+IPR worked fine, as, apparently, would AT+UART_DEF=9600,8,1,0,0. However, the change effected by AT+CIOBAUD was not sustaining. A reference @ esp8266.com/viewtopic.php?f=13&t=718 indicates that AT+CIOBAUD changes the baudrate only temporarily, which matches my own experience. – KC Tucker Jun 7 '17 at 0:57
0

Since you mentioned you tried other baud rates, I am guessing it could be an RF issue. ESP8266 is a very sensitive device and is known to have weird electrical behaviour. Do you have a cap between VCC and GND?

| improve this answer | |
  • You could try a lower baud rate, it might work but still isn't a solution. Which board are you using? 115200 works just fine on my NodeMCU. – user3544134 Jun 6 '17 at 11:51
  • BTW, you do know right that you don't really need two boards. You can program the ESP in Arduino IDE itself, using Arduino code. – user3544134 Jun 6 '17 at 11:54
  • Of course. This is just a simple test script. The actual script requires 11 data pins though. – KC Tucker Jun 7 '17 at 0:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.