ESP8266 Serial Connection with Arduino Mega not working using SoftwareSerial

Hello Guys,

I would like to do a basic connection B/w ESP8266(ESP) and Arduino mega using following code but nothing reported on the SR monitor and AT commands running this codes are not responding too.I have tried bare minimum code and connecting RX to RX TX to TX which seems to and I can cee the AT command OK and AT+GMR

My reqirement is I have 2 Arduino megas and have 2 ESP8266 modules. I want to connect to 2 megas wirelessly to perform serial communication. So first step is that I want to connect Mega to esp next I will connect other mega to its respective Esp and then try making serial communication wirelessly between the 2 megas using esp modules.
I am open for advises, if there is an alternative approach without SoftwareSerial I am willing to try them.
I am using 3.3v power from Arduino as I have not created a seprate Power supply

But when I try to using softwareserial nothing shows up in the Seriam Monitor. I have checked wiring they looks good. I have tried several baud rates no luck, but When I click Send on Serial Monitor Blue ligt turn on for micro sec

Thanks in advance
STEP1 SETUP
Current connection is as follows:
TX(ESP)->PIN2(MEGA)
RX(ESP)->PIN3(MEGA)

#include <SoftwareSerial.h>

SoftwareSerial ESPSerial(2, 3); // RX, TX

void setup() {
  // Open serial communications
  Serial.begin(115200);
  // set the data rate for the SoftwareSerial port
  // if this doesnt work you for you then try different baud rate
  ESPSerial.begin(115200);
  ESPSerial.println("Ready to take AT commands");
}

void loop() { // run over and over
  if (ESPSerial.available()) {
    // reads input from serial console and writes to esp8266
    Serial.write(ESPSerial.read());
  }
  if (Serial.available()) {
    // reads input from esp8266 and writes to serial console
    ESPSerial.write(Serial.read());
  }
}

if there is an alternative approach without SoftwareSerial I am willing to try them.

The mega has multiple hardware serial ports. Use them.

The Arduino Mega has three additional serial ports: Serial1 on pins 19 (RX) and 18 (TX), Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). To use these pins to communicate with your personal computer, you will need an additional USB-to-serial adaptor, as they are not connected to the Mega’s USB-to-serial adaptor. To use them to communicate with an external TTL serial device, connect the TX pin to your device’s RX pin, the RX to your device’s TX pin, and the ground of your Mega to your device’s ground.

sahar_ca:
STEP1 SETUP
Current connection is as follows:
TX(ESP)->PIN2(MEGA)
RX(ESP)->PIN3(MEGA)

(1) From the above texts, it is my understanding that:
TX-pin (Transmit) of ESP is connected with Digital Pin-2 (Soft Receive) of MEGA
RX-pin (Receive) of ESP is connected with Digital Pin-3 (Soft Transmit) of MEGA

(2) TX and RX pins of ESP are driving the Serial Monitor/IDE; therefore, it is recommended not to use them for some other purposes. So, let us change your connection as follows:
D1-pin (Soft Transmit) of ESP is connected with Digital Pin-2 (Soft Receive) of MEGA
D2-pin (Soft Receive) of ESP is connected with Digital Pin-3 (Soft Transmit) of MEGA

(3) The Connection Diagram
uartNodeMCUSofrSer.png

ESP-Master Codes:

#include<SoftwareSerial.h>
SoftwareSerial mySUART(4, 5);  //D2, D1 = SRX, STX

void setup()
{
  Serial.begin(115200);
  mySUART.begin(115200);
}

void loop()
{
  if(Serial.available()>0)
  {
    byte x = Serial.read();
    mySUART.write(x);
  }
  if(mySUART.available()>0)
  {
    Serial.write((char)mySUART.read());
  }
}

MEGA-Slave Codes:

#include<SoftwareSerial.h>
SoftwareSerial mySUART(2, 3);  //SRX, STX

void setup()
{
  Serial.begin(115200);
  mySUART.begin(115200);
}

void loop()
{
  if(Serial.available()>0)
  {
    byte x = Serial.read();
    mySUART.write(x);
  }
  if(mySUART.available()>0)
  {
    Serial.write((char)mySUART.read());
  }
}

(4) The setup of Step-3 does not work.

(5) Let us try the following setup using Hardware UART1 Port of MEGA

ESP-Master Codes:

#include<SoftwareSerial.h>
SoftwareSerial mySUART(4, 5);  //D2, D1 = SRX, STX

void setup()
{
  Serial.begin(115200);
  mySUART.begin(115200);
}

void loop()
{
  if(Serial.available()>0)
  {
    byte x = Serial.read();
    mySUART.write(x);
  }
  if(mySUART.available()>0)
  {
    Serial.write((char)mySUART.read());
  }
}

MEGA-Slave Codes:

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
}

void loop()
{
  if(Serial.available()>0)
  {
    byte x = Serial.read();
    Serial1.write(x);
  }
  if(Serial1.available()>0)
  {
    Serial.write((char)Serial1.read());
  }
}

(6) The Setup of Step-5 Works fine!

uartNodeMCU.png

uartNodeMCUSofrSer.png

cattledog gave good advice already about using hardware serial on the Mega instead of software serial. But just in case you are still wondering why the original code didn't work: I guess you didn't read the SoftwareSerial library's documentation:

Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).

Notice that pin 2 is not on that list?

The other problem is that SoftwareSerial won't work reliably at 115200 baud. You'll be safe using it at 9600.

As you can see, it's better in every way to use those lovely extra hardware serial ports the Mega provides.

pert:
Notice that pin 2 is not on that list?

That's why I could send data using DPin-3 of MEGA to ESP. DPin-2 of MEGA did not work to receive data from ESP.

I am glad to know why DPin-2 of MEGA did not work. K+.

Thanks guys for the detail response I am glad that I can live without SoftwareSerial thanks cattledog for the cool advise.

Currently my setup has changed after applying the suggested change and not using SoftwareSerial library.

TX-pin (Transmit) of ESP is connected with RX0 of MEGA
RX-pin (Receive) of ESP is connected with TX1 (Soft Transmit) of MEGA

Here is my code. the main ask is how can I execute AT commends within code.

If I use Bare minmum code without any code of line under Setup or Loop function , then I can execute AT commands manually on the SR monitor with the above mentioned connections and I can see the OK. But as soon as add a line like Serial.begin(9600); under the setup function I cant see any response form the AT commend.

My ultimate requirement is that I will be using TX1 at pin 18 and RX1 at pin 19 to connect esp module on the Master Mega as my TX0 ,RX0 pins will be occupied by the touch screen shields. Use TX0 and RX0 on the Slave Mega connecting to other esp module.

Thanks

void setup() {
     Serial.begin(9600); 
     Serial.print("AT"); //I was expecting to execute the AT command and return OK  but it is showing AT
}

void loop() {
 
   
}

If you connect your ESP to pins 18 and 19, and use Serial1.print("AT"), and Serial1.available() and Seria1.read() to read the reply, and Serial.print() to print it, you should see OK as a response.

PaulS:
If you connect your ESP to pins 18 and 19, and use Serial1.print("AT"), and Serial1.available() and Seria1.read() to read the reply, and Serial.print() to print it, you should see OK as a response.

Thanks for the advise
It worked for one, now I setup a master Mega and Salve mega both using ESP module. Master is setup as Station and Slave is setup as AP. I want to send message from SR Monitor of master to SR monitor of Slave
please advise how can this be accomplished

here is the working code running on both Master and Salve

void setup() {
  // Open serial communications
  Serial1.begin(115200);
  Serial.begin(115200);
   while(!Serial); 
    while(!Serial1);
   while(Serial2.available()>0)
  // set the data rate for the SoftwareSerial port
  // if this doesnt work you for you then try different baud rate
 // ESPSerial.begin(115200);
  //ESPSerial.println("Ready to take AT commands");
  Serial1.println("AT+RST");
  Serial1.flush();
  Serial1.println("AT+CIFSR");
  Serial1.println("AT");
  Serial1.println("AT+ CIPSTATUS");
  Serial1.println("AT+CIPMUX=0");

}

void loop() { // run over and over
  if (Serial1.available()) {
    // reads input from serial console and writes to esp8266
    Serial.write(Serial1.read());
  }
  if (Serial.available()) {
    // reads input from esp8266 and writes to serial console
    Serial1.write(Serial.read());
  }
  
}

I Managed to setup a master Mega(Serial Comm Sender) and Slave mega(Receiver) both using ESP 8266 module. Master is setup as Station and Slave is setup as AP. I want to send Seriall monitor message from Master Mega to of master to Slave via arduino code Can you please advise how can this be accomplished.

Connection is as follows
Master Mega
ESP|MEGA
RX | TX1
TX | RX1

SLAVE Mega
ESP|MEGA
RX | TX1
TX | RX1

Please help

Can you please advise how can this be accomplished.

Once you get a message on the Mega, using Serial to read it, sending it via the Serial1 instance to the ESP is trivial.

What have you coded the ESP on the master end to do?
What have you coded the ESP on the slave end to do?

What have you coded the ESP on the master end to do?
What have you coded the ESP on the slave end to do?

Yes, what is the purpose of this project. For simple wireless Mega to Mega communications you have added a lot of complexity with the esp8266.

Additionally, why you are using AT commands with the esp's and no libraries?

For simple point to point wireless, you may want to consider Bluetooth or nrf24L01. With the esp82666's you might want to consider ESP-NOW which is a simplified protocol.ESP8266 with ESP-NOW as an alternative to nRF24L01+ - Exhibition / Gallery - Arduino Forum

PaulS:
Once you get a message on the Mega, using Serial to read it, sending it via the Serial1 instance to the ESP is trivial.

What have you coded the ESP on the master end to do?
What have you coded the ESP on the slave end to do?

I am running following code In Master and Slave Megas and Looking for what should I do to Send this Message "<1 1234 45 1>" from Master Mega to Slave Mega using UART.

I am able to send UART like AT commands from Mega to ESP the reqruiement is send this message "<1 1234 45 1>" from Master mega to Slave Mega and both has Seprate ESP module installed and configured

I hope this information helps

void setup() {
  // Open serial communications
  Serial1.begin(115200);
  Serial.begin(115200);
   while(!Serial); 
    while(!Serial1);
  

}

void loop() { // run over and over
  if (Serial1.available()) {
    // reads input from serial console and writes to esp8266
    Serial.write(Serial1.read());
  }
  if (Serial.available()) {
    // reads input from esp8266 and writes to serial console
    Serial1.write(Serial.read());
  }
  
  
}

I am running following code In Master and Slave Megas and Looking for what should I do to Send this Message "<1 1234 45 1>" from Master Mega to Slave Mega using UART.

The code you posted should indeed have the Master Mega read <1 1234 45 1> from the monitor and send it to the ESP 8266 connected to it.

As PaulS says

Once you get a message on the Mega, using Serial to read it, sending it via the Serial1 instance to the ESP is trivial.

Indeed, when the received message is sent on Serial1 to the master ESP, how does that ESP read it? How does that ESP send it over the air? How does the slave ESP read the broadcast message? How does the slave ESP send it to the slave Arduino.

Regarding the code on the Mega, please review the Serial basics tutorial by Robin2 which demonstrates robust methods on how to receive with start/end markers. The methods as presented do not preserve the start/end markers, and you may want to do that for forwarding the message to the ESP. Here's the tutorial code modified to preserve the start/end markers for sending to the esp.

const byte numChars = 20;
char receivedChars[numChars];

boolean newData = false;

void setup() {
  Serial.begin(115200);
  Serial1.begin(115200);
  Serial.println("Enter message in serial monitor");
}

void loop() {
  recvWithStartEndMarkers();
  sendToESP();
}

void recvWithStartEndMarkers() {
  static boolean recvInProgress = false;
  static byte ndx = 0;
  char startMarker = '<';
  char endMarker = '>';
  char rc;

  while (Serial.available() > 0 && newData == false) { 
    rc = Serial.read();

    if (recvInProgress == true) {
      if (rc != endMarker) {
        receivedChars[ndx] = rc;
        ndx++;
        if (ndx >= numChars) {
          ndx = numChars - 1;
        }
      }
      else {
        receivedChars[ndx] = endMarker;//preserve endmarker 
        receivedChars[ndx+1] = '\0';// terminate the string
        recvInProgress = false;
        ndx = 0;
        newData = true;
      }
    }

    else if (rc == startMarker) {
      receivedChars[0] = startMarker;//preserve startMarker
      ndx++;
      recvInProgress = true;
    }
  } 
}

void sendToESP() {
  if (newData == true) {
    //echo back to Monitor
    Serial.print("This just in ... ");
    Serial.println(receivedChars);
    //send to ESP
    Serial1.print(receivedChars);
    newData = false;
  }
}