ESP8266

PieterP:
Then why not use a hardware serial port?
Don't do that, the 3.3V pin of the Arduino can only supply 50mA, the ESP can draw up to 5 times as much.
Also, you need a bypassing capacitor: when transmitting WiFi, it draws a lot of current in very small peaks, without bypass caps, your voltage will drop low enough to make it unstable.
Not for long if you are connecting its 3.3V input pins to 5V outputs of the Mega. You will destroy the ESP.
Use a voltage divider or a level shifter.

First make sure your hardware is correct, once that is done, we can start to look at what's wrong with your code.

I explained how to wire it in this post: ESP8266 - #9 by PieterP - Project Guidance - Arduino Forum. (forget about the LEDs and the program button, you don't need them.)

what if I just used an external supply and use the voltage regulator to provide esp with sufficient power? would that be okay or will still have to use those bypass capacitors?