5

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:

#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: enter image description here

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

I've recently changed the MCU(uno) to a different and even a different esp module from a different supplier, I am still getting the same results! I can't be the first one to encounter such phenomena...

Mercury
  • 101
  • 1
  • 5

3 Answers3

2

Try dropping the SoftwareSerial connection speed to 19200. Ie, esp8266.begin(19200). 115200 it is the absolute max the SoftwareSerial can theoretically handle. Also if you are not using all the change interrupt pins on your MCU (Uno/Nano?) then use any 2 pins from 10, 11, 12 or 13 rather than 3, 2 // RX, TX. Not sure about sendCommand function. If you just want to test the connection then it should be as simple as:

SoftwareSerial esp8266(10, 11); // RX, TX

void setup() {
    esp8266.begin(19200);
    Serial.begin(115200 );

    char buffer[50];
    esp8266.write("AT\r\n");
    esp8266.readBytes(buffer, sizeof(buffer));
    Serial.println(buffer);

...
}

Obviously once you need to open web pages and recieve responses then you will need a libary to handle IDP+ data. With a bit of coercion I got Adafruit_ESP8266.h working with SoftwareSerial.

chris
  • 86
  • 3
0

A quick thing to try out.

Try changing Serial.begin(115200); to Serial.begin(9600); and leave esp8266.begin(115200);.

I'm also completely new to Arduino, but I remember seeing the junk values when the serial monitor baud rate is set to a different value than the Arduino's Baud rate.

I was using Arduino Uno and if I set the baud rate to a value other 9600 in the serial window, I was seeing junk values in println function calls.

i.e Set Serial.Begin(9600); and the Serial Montior baud rate also 9600.

Sherin Mathew
  • 101
  • 1
  • 7
0

From my experience, if you want to have a consistent data transfer over the softserial, you must disable (in Arduino) the global interrupts for the time of transmission. To disable global interrupts use cli() command, to enable it again use sei() command.

gosewski
  • 1
  • 1