ESP8266

I am using the arduino mega2560

Then WHY in heavens name are you using SoftwareSerial? You have 4 hardware serial ports. Connect the ESPxxxx to one of them.

You DID read the page about which pins you could do SoftwareSerial on on the Mega, didn't you? (I didn't think so.)