esp8266-01 with arduino mega problem

i use this program for esp8266-01 with arduino uno

#include <WiFiEsp.h>

// Emulate Serial1 on pins 6/7 if not present
#include <SoftwareSerial.h>
SoftwareSerial Serial1(6, 7); // RX, TX

char ssid[] = "DJAWEB_0D19C";            // your network SSID (name)
char pass[] = "2F088C732CAFE";        // your network password
int status = WL_IDLE_STATUS;

int ledStatus = LOW;
int led2 = 9;

WiFiEspServer server(80);

// use a ring buffer to increase speed and reduce memory allocation
RingBuffer buf(8);

void setup()
  pinMode(9, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);  // initialize digital pin LED_BUILTIN as an output.
  Serial.begin(115200);   // initialize serial for debugging
  Serial1.begin(9600);    // initialize serial for ESP module
  WiFi.init(&Serial1);    // initialize ESP module

  // check for the presence of the shield
  if (WiFi.status() == WL_NO_SHIELD) {
    Serial.println("WiFi shield not present");
    // don't continue
    while (true);

  // attempt to connect to WiFi network
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    // Connect to WPA/WPA2 network
    status = WiFi.begin(ssid, pass);

  Serial.println("You're connected to the network");
  // start the web server on port 80

void loop()
  WiFiEspClient client = server.available();  // listen for incoming clients

  if (client) {                               // if you get a client,
    Serial.println("New client");             // print a message out the serial port
    buf.init();                               // initialize the circular buffer
   while (client.connected()) {              // loop while the client's connected
      if (client.available()) {               // if there's bytes to read from the client,
        char c =;               // read a byte, then
        buf.push(c);                          // push it to the ring buffer

        // printing the stream to the serial monitor will slow down
        // the receiving of data from the ESP filling the serial buffer
        // you got two newline characters in a row
        // that's the end of the HTTP request, so send a response
        if (buf.endsWith("\r\n\r\n")) {

        // Check to see if the client request was "GET /H" or "GET /L":
        if (buf.endsWith("GET /H")) {
          Serial.println("Turn led ON");
          //ledStatus = HIGH;
          digitalWrite(9, HIGH);   // turn the LED on (HIGH is the voltage level)
      else  if (buf.endsWith("GET /L")) {
          Serial.println("Turn led OFF");
         // ledStatus = LOW;
          digitalWrite(9, LOW);    // turn the LED off by making the voltage LOW
            else   if (buf.endsWith("GET /G")) {
          Serial.println("Turn led2 ON");
          digitalWrite(13, HIGH);    // turn the LED off by making the voltage LOW
              else if (buf.endsWith("GET /M")) {
          Serial.println("Turn led2 OFF");
          digitalWrite(13, LOW);    // turn the LED off by making the voltage LOW
    // close the connection
    Serial.println("Client disconnected");

void sendHttpResponse(WiFiEspClient client)
  // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK)
  // and a content-type so the client knows what's coming, then a blank line:
  client.println("HTTP/1.1 200 OK");
  // the content of the HTTP response follows the header:
  client.print("The LED is ");
  client.println("Click <a href=\"/H\">here</a> turn the LED on
  client.println("Click <a href=\"/L\">here</a> turn the LED off

  client.println("Click <a href=\"/G\">here</a> turn the LED2 on
  client.println("Click <a href=\"/K\">here</a> turn the LED2 off
  // The HTTP response ends with another blank line:

void printWifiStatus()
  // print the SSID of the network you're attached to
  Serial.print("SSID: ");

  // print your WiFi shield's IP address
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");

  // print where to go in the browser
  Serial.print("To see this page in action, open a browser to http://");

problem number one when i add LED number 2 http page not complete operation stay waiting but it work.
problem number two when i use this program with arduino mega , esp8266 not working and led blue of esp8266 not blink

on Mega use pins of Serial1 TX1 and RX1, not 6 and 7

i try 18/19 it work thank u
and how i add led 2, it correct ?

the response is not a valid HTML document and the HTTP header doesn't contain the size of the response so the browser must improvise

what i do now ? and what i modify

what i do now ? and what i modify