0

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

  • 3
    Why are you using SoftwareSerial on a Mega?!?! – Majenko Jul 25 at 8:47
  • Rx->Tx is correct, not the other way around. Also, the atmega2560 has more serial interfaces, so softwareserial is not a good choice – Sim Son Jul 25 at 9:39
3

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);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.