ESP 8266 (Harware serial with Arduino mega) :

Code is not working properly! Not able to identify the error. plz help

Can you give us a little more please? What is the output of serial console, how are things wired up etc?

I have flashed the v22 AT version of firmware.
The ESP 8266 module is getting connected and IP is assigned and also it is ready to ping message is displayed.
But after sometime it stops working.
I get the message that " your device has been disconnected" on the app