Need help designing a sketch to use with the ESP-01

The beginners guide to the ESP shows how to program the ESP using the Arduino IDE. Programming with the IDE allow much greater flexibility than the the AT commands. The beginners guide has lots of information on using the ESP in a network. Then you can connect he Mega to the ESP with one of the (3 extra) hardware serial ports.