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.