ESP8266 Software Serial sending wrong data on first byte

Hi. I am using the software serial library to communicate between an ESP8266 (arduino core). The idea is to write two separate asynchronous programs (for the ESP and Mega respectively) that start once communication has been established between the two modules. For this, I have decided to start the Mega "first" and then wait until the ESP begins at which point the ESP sends a byte to the Mega to signal it has started.

Code for the ESP

#include <SoftwareSerial.h>

SoftwareSerial Mega2560(D5, D6);    // Software Serial for data
HardwareSerial& Monitor = Serial;   // Hardware Monitor (UART0) for debugging via Serial Monitor

void setup()
{
    Monitor.begin(115200);
    while (!Monitor);

    Mega2560.begin(4800);
    while (!Mega2560);
    
#define ESP_READY 0xAA // arbitrary value
    Mega2560.write(ESP_READY);
    delay(1000);

    Monitor.print("\n\nSent 0x");
    Monitor.println(ESP_READY, HEX);
}

void loop()
{
}

Code for the Mega:

#include <SoftwareSerial.h>

SoftwareSerial ESP8266(11, 10);
HardwareSerial& Monitor = Serial;
    
void setup()
{
    Monitor.begin(115200);
    while (!Monitor);

    ESP8266.begin(4800);
    while (!ESP8266);

#define ESP_READY 0xAA
    char c;
    do {
        while (!ESP8266.available());
        c = ESP8266.read();
        Monitor.println(c, HEX);
    }
    while (c != ESP_READY);
}

void loop()
{
}

But for some reason the Mega receives a 0x54 while the ESP says that it sent a 0xAA ( i reset the ESP 3 times).

ESP output:

Sent 0xAA
Sent 0xAA
Sent 0xAA

Mega output:

54
54
54

Why is this happening? I did some further testing and found out that the first byte sent is always received incorrectly.

BUT! If the Mega is not ready, that byte is LOST forever.

you are right. The assumption that I am working with is that the Mega WILL be started first (manually by a reset) and THEN the ESP will begin and take whatever time it takes to connect to an AP, thereby guaranteeing that the Mega is expecting the byte before the ESP can send it. Not the best strategy but it will do for now.

Maybe try using espsoftwareserial on the 8266..
And on the Mega, why use software serial at all, are you using all hardware serials??
language/functions/communication/serial/

good luck.. ~q

It's extremely frustrating that the custom board i am working with does not have ANY of the Mega's UART pins broken out otherwise I would have used the hardware serials. Thanks for the tip. For now, the simplest workaround that I can think of is just sending the same byte twice, the second one thankfully does not get misidentified.

ok, stinkers but makes sense..
use regular software serial there but try changing to the espSoftwareSerial on the ESP, maybe that clears it up..

~q

1 Like

Looking at your code..
thinking you should re-transmit your ESP_READY until acknowledged..
small while not Mega2560.Available, then Mega Write ESP_READY, then delay 1 second..
of course change Mega code to send a reply once it receives the ESP_READY..

good luck.. ~q

1 Like

If the ESP8266 only sends data, there is Serial1, on D4, but it's TX only (no RXpin).

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.