Arduino Mega - ESP32 Serial Communication

Hello. I have a project in which we have to make an Arduino Mega and an NodeMCU ESP32 to communicate bidirectionally.
We can send data from the ESP to Arduino, but not invers, so we can't send a response to the ESP32. I may need some advice on how to overcome this problem!
I will link the codes below. Thank you in advance!
ESP32 CODE

#include <SoftwareSerial.h>

SoftwareSerial RXSerialESP(3, 1);   // RX, TX
SoftwareSerial TXSerialESP(13, 15); // RX, TX

void setup()
{
    Serial.begin(115200);
    RXSerialESP.begin(115200);
    TXSerialESP.begin(115200);
    Serial.println("Starting...");
    delay(1000);
}

void loop()
{
    Serial.println("Sending data...");

    TXSerialESP.println("COMING FROM TX ESP");
    RXSerialESP.println("COMING FROM RX ESP");
    delay(1000);
    if (RXSerialESP.available()){
        char buffer;
        buffer = RXSerialESP.read();
        Serial.print("DATA RX: ");
        Serial.println(buffer);
        delay(500);
    }
    if (TXSerialESP.available()){
        char buffer;
        buffer = TXSerialESP.read();
        Serial.print("DATA TX: ");
        Serial.println(buffer);
        delay(500);
    }
    delay(3000);
}

Arduino CODE

#include <SoftwareSerial.h>

SoftwareSerial RXSerialArd(0, 1);   // RX, TX
SoftwareSerial TXSerialArd(18, 19); // RX, TX

void setup()
{
    Serial.begin(115200);
    RXSerialArd.begin(115200);
    TXSerialArd.begin(115200);
    Serial.println("Starting...");
    delay(1000);
}

void loop()
{
    char buffer[25];
    if (RXSerialArd.available()){
        char buffer;
        buffer = RXSerialArd.read();
        Serial.print("DATA RX: ");
        Serial.println(buffer);
        delay(500);
    }
    if (TXSerialArd.available()){
        char buffer;
        buffer = TXSerialArd.read();
        Serial.print("DATA TX: ");
        Serial.println(buffer);
        delay(500);
    }
    if (Serial.available())
    {
        Serial.readBytesUntil('\n', buffer, 25);
        Serial.print("DATA: ");
        Serial.println(buffer);
        delay(500);
        TXSerialArd.println("DATA RECIVED TX!");
        delay(500);
        RXSerialArd.println("DATA RECIVED RX!");
    }
    delay(3000);
}

Well, start by drawing a schematic of your connections. Once you've thus told us what it is you're doing, we'll start. Right now, I fail to see why you need two serial links on each device.
C

Using SoftwareSerial on / with a Mega is never a good idea.

1 Like

Why use softwareserial with an ESP32 when the ESP32 has 4 serial ports, you can use 2?

Provide a circuit showing the level shifters being used.

softwareserial does not work well with an ESP32, as a note.

Multiple software serials require a special use case as a note.

here is an example of using 2 serial ports with an esp32.

#include <HardwareSerial.h>


HardwareSerial GPSSerial ( 1 );
// pin 2=RX, pin 15=TX
HardwareSerial LIDARSerial ( 2 );
// pin 26=RX, pin 25=TX


voiding setups()
{

  LIDARSerial.begin ( SerialDataBits, SERIAL_8N1, 26, 25 );
  GPSSerial.begin ( GPS_DataBits, SERIAL_8N1, 2, 15 ); // begin GPS hardware serial



}

void fReceiveSerial_LIDAR( void * parameters  )
{
  bool BeginSentence = false;
  sSerial.reserve ( StringBufferSize300 );
  char OneChar;
  for ( ;; )
  {
    EventBits_t xbit = xEventGroupWaitBits (eg, evtReceiveSerial_LIDAR, pdTRUE, pdTRUE, portMAX_DELAY);
    if ( LIDARSerial.available() >= 1 )
    {
      while ( LIDARSerial.available() )
      {
        OneChar = LIDARSerial.read();
        if ( BeginSentence )
        {
          if ( OneChar == ‘>’)
          {
            if ( xSemaphoreTake( sema_ParseLIDAR_ReceivedSerial, xSemaphoreTicksToWait10 ) == pdTRUE )
            {
              xQueueOverwrite( xQ_LIDAR_Display_INFO, ( void * ) &sSerial );
              xEventGroupSetBits( eg, evtParseLIDAR_ReceivedSerial );
              //
            }
            BeginSentence = false;
            break;
          }
          sSerial.concat ( OneChar );
        }
        else
        {
          if ( OneChar == ‘<’ )
          {
            sSerial = “”; // clear string buffer
            BeginSentence = true; // found begining of sentence
          }
        }
      } //  while ( LIDARSerial.available() )
    } //if ( LIDARSerial.available() >= 1 )
    xSemaphoreGive( sema_ReceiveSerial_LIDAR );
  }
  vTaskDelete( NULL );
} //void fReceiveSerial_LIDAR( void * parameters  )

1 Like

So I cannot upload the image, because of company privacy VPN, but we have connected the GPIO1(TXD0) - 0 (RX0), GPIO1(TXD0) - 1 (TX0), GPIO15(TXD0) - 18 (RX1), GPIO13(TXD0) - 19 (TX1). We have used a two voltage dividers between the RX and TX to 3.3V.

I'm afraid I can't make head nor tails out of your description versus your pin definitions in your two codes. It makes no sense to me to define a bidirectional port for communication in one direction, and a different bidirectional port for communication in the other direction. In addition to that, you've got port pin numbers from one port in another port. I don't believe you can do that, and additionally, even if you could, why would you?
Look again at the pin assignments for Serial, Serial1, and Serial 2 in both units.
C

Sorry to read that you will not be able to help me help you. I wish you the best of luck. I'm out.

Ditto. This goes nowhere.
C

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