Skip to main content
5 of 9
added 776 characters in body
Mercury
  • 101
  • 1
  • 5

esp8266 garbage out only when using SoftwareSerial

I'm trying to operate the esp8266. When I connect RX+TX pins to pins 1+2 on the Arduino, everything works fine. but when I'm trying to use software serial all hell breaks loose and the recieved data is garbadeged (It seems like everything is ok but I can't read the output data... more interesting, the output is not consistent!

example: I'm using a predefined function to send the data, and sending a simple AT command (again, using RXTX conncted to Arduino RXTX everything works well and I get OK).

bool sendCommand(const String& cmd,uint64_t timeout,bool vrbs = false,String* responseP = 0)
{
    String strCntnr;
    String& response = responseP? *responseP: strCntnr;
    response = "";
    if(vrbs)
        Serial.print("RX: "+cmd+ "...");

    esp8266.print(cmd+"\r\n"); // send the read character to the esp8266

    for(uint64_t time = millis();(time+timeout) > millis();)
    for(;esp8266.available();response+=(char)esp8266.read());

    bool ok = response.length()>0;

    response.replace("\r\n"," ");
    if(vrbs)
        Serial.println(ok? "OK ("+response+")":"ERROR");   
    return ok;
}

Setup function:

SoftwareSerial esp8266(3, 2); // RX, TX
void setup() {
    esp8266.begin(115200 );
    Serial.begin(115200 );
   
    sendCommand("AT",2000,true);
...
}

And output:

AT...OK (AR CטjµLר)

or:

AT...OK (AjCH´Dטk×Hר)

or:

RX: AT...OK (AR CטjµD�)

,or any other random string...

EDIT:

I've uptade the code I'm running to a simple one:

#include <SoftwareSerial.h>
SoftwareSerial esp8266(11, 10); // RX, TX
void setup() {
  // put your setup code here, to run once:
  esp8266.begin(19200);
  Serial.begin(115200 );
}

void loop() {
esp8266.write("AT\r\n");
String buffer;
Serial.print("SENDING AT...");
for(uint64_t time = millis();(time+1000) > millis();)
  for(;esp8266.available();buffer+=(char)esp8266.read());
  buffer.replace("\r\n"," ");
Serial.println("RESPONSE:" + buffer);
}

And the output: here

I've tried changing the SerialMonitor baud rate, and the input pins, but results are all the same.

Mercury
  • 101
  • 1
  • 5