user58371: I am trying to read string send from esp8266 esp01 to Arduino mega. When I use normal Rx and Tx ports, it works properly but when i am initializing new serial port using SoftwareSerial then nothing is being shown on the serial monitor.
Please help me as i have been stuck and trying to solve this for weeks now.
Below are the code for esp8266 and Arduino mega


Code for ARDUINO MEGA


#include<SoftwareSerial.h>

SoftwareSerial myS(4,5);

String data="true";

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
myS.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(myS.available()){
  data=myS.readString();
  }
  Serial.println(data);
  //Serial.write(data);
  delay(2000);
}



Code for ESP8266


void setup() {
  Serial.begin(9600);     // Initialize the Serial interface with baud rate of 9600
}

// the loop function runs over and over again forever
void loop() {  
  if(Serial.available()>0)    //Checks is there any data in buffer 
  {
    Serial.print("We got:");
    Serial.print(char(Serial.read()));  //Read serial data byte and send back to serial monitor
  }
  else
  {
    Serial.println("Hello World..."); //Print Hello word every one second
    delay(1000);                      // Wait for a second
  }
}


Moreover, i have tried read() as well instead of readString(), but it does not work either. I have also tried switching Rx and TX pins meaning connecting Rx->Tx, Tx->Rx and vice versa i.e. Rx->Rx and Tx->Tx, but it does not work in any of the case. 
Any help or suggestion will be appreciated.
Thanks

user4143: SoftwareSerial only works on a few select GPIO pins on the Mega. Specifically those pins that have PCINT on them.
But that is irrelevant. There is no reason to use SoftwareSerial on a Mega except in exceptional circumstances.  You have four hardware UARTs on the Mega, so there is no call to use SoftwareSerial.  Use the real UART pins and objects:
Serial -> RX(0) / TX(1)
Serial1 -> RX1(19) / TX1(18)
Serial2 -> RX2(17) / TX2(16)
Serial3 -> RX3(15) / TX3(14)

Just replace all references in your code to the SoftwareSerial instance with Serial1 (delete the #include and the object instance constructor) and use TX1 and RX1 to connect your module:
String data="true";

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    Serial1.begin(9600);
}

void loop() {
    // put your main code here, to run repeatedly:
    if(Serial1.available()){
        data=Serial1.readString();
    }
    Serial.println(data);
    //Serial.write(data);
    delay(2000);
}


58371: <p>I am trying to read string send from esp8266 esp01 to Arduino mega. When I use normal Rx and Tx ports, it works properly but when i am initializing new serial port using SoftwareSerial then nothing is being shown on the serial monitor.</p>

<p>Please help me as i have been stuck and trying to solve this for weeks now.
Below are the code for esp8266 and Arduino mega</p>

<blockquote>
  <blockquote>
    <p>Code for ARDUINO MEGA</p>
  </blockquote>
</blockquote>

<pre><code>#include&lt;SoftwareSerial.h&gt;

SoftwareSerial myS(4,5);

String data="true";

void setup() {
  // put your setup code here, to run once:
Serial.begin(9600);
myS.begin(9600);
}

void loop() {
  // put your main code here, to run repeatedly:
  if(myS.available()){
  data=myS.readString();
  }
  Serial.println(data);
  //Serial.write(data);
  delay(2000);
}
</code></pre>

<blockquote>
  <blockquote>
    <p>Code for ESP8266</p>
  </blockquote>
</blockquote>

<pre><code>void setup() {
  Serial.begin(9600);     // Initialize the Serial interface with baud rate of 9600
}

// the loop function runs over and over again forever
void loop() {  
  if(Serial.available()&gt;0)    //Checks is there any data in buffer 
  {
    Serial.print("We got:");
    Serial.print(char(Serial.read()));  //Read serial data byte and send back to serial monitor
  }
  else
  {
    Serial.println("Hello World..."); //Print Hello word every one second
    delay(1000);                      // Wait for a second
  }
}
</code></pre>

<hr>

<p>Moreover, i have tried read() as well instead of readString(), but it does not work either. I have also tried switching Rx and TX pins meaning connecting Rx->Tx, Tx->Rx and vice versa i.e. Rx->Rx and Tx->Tx, but it does not work in any of the case. 
Any help or suggestion will be appreciated.
Thanks</p>
 4143: <p>SoftwareSerial only works on a few select GPIO pins on the Mega. Specifically those pins that have PCINT on them.</p>

<p>But that is irrelevant. There is no reason to use <code>SoftwareSerial</code> on a Mega except in exceptional circumstances.  You have four hardware UARTs on the Mega, so there is no call to use <code>SoftwareSerial</code>.  Use the real UART pins and objects:</p>

<pre><code>Serial -&gt; RX(0) / TX(1)
Serial1 -&gt; RX1(19) / TX1(18)
Serial2 -&gt; RX2(17) / TX2(16)
Serial3 -&gt; RX3(15) / TX3(14)
</code></pre>

<p>Just replace all references in your code to the <code>SoftwareSerial</code> instance with <code>Serial1</code> (delete the <code>#include</code> and the object instance constructor) and use TX1 and RX1 to connect your module:</p>

<pre><code>String data="true";

void setup() {
    // put your setup code here, to run once:
    Serial.begin(9600);
    Serial1.begin(9600);
}

void loop() {
    // put your main code here, to run repeatedly:
    if(Serial1.available()){
        data=Serial1.readString();
    }
    Serial.println(data);
    //Serial.write(data);
    delay(2000);
}
</code></pre>
