user13754: I am using ESP8266 with Arduino MEGA. ESP8266 is powered 3.3v via voltage divider, which is taking 5v from arduino and converting it down to 3.3v with the help of 5K and 10K resistors. AT commands seems to be working OK. I am using SD_card reader and kept my index.html file in SD_card. I am able to make a successful connection and esp also sends the whole index.html file, But the issue is my browser does not shows the page in html format, instead the whole html code appears on the browser. Can anyone help me to sort this issue ?
Herez the whole code;
    #include <SPI.h>
    #include <SD.h>

     #define SSID "Maxcotec"   // change this to match your WiFi SSID  
     #define PASS "qamar0334" // change this to match your WiFi password  
     #define PORT "80"      // using port 80 by default
    #define DEBUG true
     #define BUFFER_SIZE 1024 
     char buffer[BUFFER_SIZE];  
    //SoftwareSerial Serial2(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                                 // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                                 // and the RX line from the esp to the Arduino's pin 3
    File webFile, CSS_file;
    String webpage;
     // By default we are looking for OK\r\n  
     char OKrn[] = "OK\r\n";  
     byte wait_for_esp_response(int timeout, char* term=OKrn) {  
      unsigned long t=millis();  
      bool found=false;  
      int i=0;  
      int len=strlen(term);  
      // wait for at most timeout milliseconds  
      // or if OK\r\n is found  
      while(millis()<t+timeout) {  
       if(Serial2.available()) {  
        buffer[i++]=Serial2.read();  
        if(i>=len) {  
         if(strncmp(buffer+i-len, term, len)==0) {  
          found=true;  
          break;  
         }  
        }  
       }  
      }  
      buffer[i]=0;  
      Serial.print(buffer);  
      return found;  
     }  

    void setup()
    {
      Serial.begin(9600);
      Serial2.begin(115200); // your esp's baud rate might be different

       setupWiFi();  
      // print device IP address  
      Serial.print("device ip addr:");  
      //esp.println("AT+CIFSR");  


      //sendData("AT+RST\r\n",2000,DEBUG); // reset module
      //sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
      sendData("AT+CIFSR\r",1000,DEBUG); // get ip address
      sendData("AT+CIPMUX=1\r",1000,DEBUG); // configure for multiple connections
      sendData("AT+CIPSERVER=1,80\r",1000,DEBUG); // turn on server on port 80

        // initialize SD card
        Serial.println("Initializing SD card...");
        if (!SD.begin(53)) {
            Serial.println("ERROR - SD card initialization failed!");
            return;    // init failed
        }
        Serial.println("SUCCESS - SD card initialized.");
        // check for index.htm file
        if (!SD.exists("index.htm")) {
            Serial.println("ERROR - Can't find index.htm file!");
            return;  // can't find index file
        }
        Serial.println("SUCCESS - Found index.htm file."); 


             // send web page
        webFile = SD.open("index.htm");        // open web page file
        if (webFile) {
            while(webFile.available()> 0) {
                //client.write(webFile.read()); // send web page to client
               char ch = webFile.read(); // Read a character
               webpage += ch; // Add it
            }
            webFile.close();
        }
         Serial.print("file contents>>>");
         //Serial.println(webpage);
         //delay(20000);
    }

    void loop()
    {
      if(Serial2.available()) // check if the esp is sending a message 
      {
        /*
        while(Serial2.available())
        {
          // The esp has data so display its output to the serial window 
          char c = Serial2.read(); // read the next character.
          Serial.write(c);
        } */

        if(Serial2.find("+IPD,"))
        {
         delay(1000);

         int connectionId = Serial2.read()-48; // subtract 48 because the read() function returns 
                                               // the ASCII decimal value and 0 (the first decimal number) starts at 48

         //String webpage = "<h1>Hello</h1><h2>World!</h2><button id=\"11\">Toggle Pin 11</button>";//<button>LED1</button>";


         //Serial.print("file contents>>>");
         //Serial.println(webpage);
         //delay(20000);
         String cipSend = "AT+CIPSEND=";
         cipSend += connectionId;
         cipSend += ",";
         cipSend +=webpage.length();
         cipSend +="\r\n";

         sendData(cipSend,1000,DEBUG);
         sendData(webpage,1000,DEBUG);

         //webpage="<button>LED2</button>";

    //     cipSend = "AT+CIPSEND=";
    //     cipSend += connectionId;
    //     cipSend += ",";
    //     cipSend +=webpage.length();
    //     cipSend +="\r\n";
    //     
    //     sendData(cipSend,1000,DEBUG);
    //     sendData(webpage,1000,DEBUG);

         String closeCommand = "AT+CIPCLOSE="; 
         closeCommand+=connectionId; // append connection id
         closeCommand+="\r\n";

         sendData(closeCommand,3000,DEBUG);
        }
      }
    }

    void setupWiFi() {  
      // try empty AT command  
      Serial2.println("AT");  
      wait_for_esp_response(2000);  
      // set mode 1 (client)  
      Serial2.println("AT+CWMODE=1");  
      wait_for_esp_response(2000);   
      // reset WiFi module  
     /*  
      esp.print("AT+RST\r\n");  
      wait_for_esp_response(3000);  
      delay(5000);  
      */  
      // join AP  
      Serial2.print("AT+CWJAP=\"");  
      Serial2.print("xxxxxxx");  
      Serial2.print("\",\"");  
      Serial2.print("xxxxxxx");  
      Serial2.println("\"");  
      // this may take a while, so wait for 5 seconds  
      wait_for_esp_response(5000);  
      Serial2.println("AT+CIPSTO=30");   
      wait_for_esp_response(1000);  
      // start server  
      Serial2.println("AT+CIPMUX=1");  
      wait_for_esp_response(1000);  
      Serial2.print("AT+CIPSERVER=1,"); // turn on TCP service  
      Serial2.println(PORT);  
      wait_for_esp_response(1000);  
     }  

    String sendData(String command, const int timeout, boolean debug)
    {
        String response = "";

        Serial2.print(command); // send the read character to the Serial2

        long int time = millis();

        while( (time+timeout) > millis())
        {
          while(Serial2.available())
          {

            // The esp has data so display its output to the serial window 
            char c = Serial2.read(); // read the next character.
            response+=c;
          }  
        }

        if(debug)
        {
          Serial.print(response);
        }

        return response;
    }

And this is what I am getting in web browser;
I am using Opera browser but I have tried in Chrome and firefox aswell. when I open this index.html page in my laptop with the same browser, it opens perfectly. 

user20527: See the text from "'HTTP/1.1 200 OK'" until "'DOCTYPE html'" in the following couple of links which say the same, and you might like to read the rest of those pages:

https://www.arduino.cc/en/Tutorial/WebServer
http://www.allaboutcircuits.com/projects/using-an-arduino-as-a-web-server


user19886: Slightly off-topic, as this is not the source of your problem, but as jBot42 says, you need a proper regulator. When you use a voltage divider, your load (the ESP8266) is connected in parallel with one of the resistors (the 10K one) and effectively changes the output voltage of the regulator. As the consumption of power in the ESP8266 changes (e.g. when the wifi circuit transfers data), the voltage will also change.
At the very least you should place an electrolytic capacitor in parallel with the 10K resistor, but even this is not a good solution.

user20579: It looks like you did not send the HTTP headers. Before sending any of the HTML content send following lines:
"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n"

Make sure you send all the \r\n !

13754: <p>I am using ESP8266 with Arduino MEGA. ESP8266 is powered 3.3v via voltage divider, which is taking 5v from arduino and converting it down to 3.3v with the help of 5K and 10K resistors. AT commands seems to be working OK. I am using SD_card reader and kept my index.html file in SD_card. I am able to make a successful connection and esp also sends the whole index.html file, But the issue is my browser does not shows the page in html format, instead the whole html code appears on the browser. Can anyone help me to sort this issue ?
Herez the whole code;</p>

<pre><code>    #include &lt;SPI.h&gt;
    #include &lt;SD.h&gt;

     #define SSID "Maxcotec"   // change this to match your WiFi SSID  
     #define PASS "qamar0334" // change this to match your WiFi password  
     #define PORT "80"      // using port 80 by default
    #define DEBUG true
     #define BUFFER_SIZE 1024 
     char buffer[BUFFER_SIZE];  
    //SoftwareSerial Serial2(2,3); // make RX Arduino line is pin 2, make TX Arduino line is pin 3.
                                 // This means that you need to connect the TX line from the esp to the Arduino's pin 2
                                 // and the RX line from the esp to the Arduino's pin 3
    File webFile, CSS_file;
    String webpage;
     // By default we are looking for OK\r\n  
     char OKrn[] = "OK\r\n";  
     byte wait_for_esp_response(int timeout, char* term=OKrn) {  
      unsigned long t=millis();  
      bool found=false;  
      int i=0;  
      int len=strlen(term);  
      // wait for at most timeout milliseconds  
      // or if OK\r\n is found  
      while(millis()&lt;t+timeout) {  
       if(Serial2.available()) {  
        buffer[i++]=Serial2.read();  
        if(i&gt;=len) {  
         if(strncmp(buffer+i-len, term, len)==0) {  
          found=true;  
          break;  
         }  
        }  
       }  
      }  
      buffer[i]=0;  
      Serial.print(buffer);  
      return found;  
     }  

    void setup()
    {
      Serial.begin(9600);
      Serial2.begin(115200); // your esp's baud rate might be different

       setupWiFi();  
      // print device IP address  
      Serial.print("device ip addr:");  
      //esp.println("AT+CIFSR");  


      //sendData("AT+RST\r\n",2000,DEBUG); // reset module
      //sendData("AT+CWMODE=2\r\n",1000,DEBUG); // configure as access point
      sendData("AT+CIFSR\r",1000,DEBUG); // get ip address
      sendData("AT+CIPMUX=1\r",1000,DEBUG); // configure for multiple connections
      sendData("AT+CIPSERVER=1,80\r",1000,DEBUG); // turn on server on port 80

        // initialize SD card
        Serial.println("Initializing SD card...");
        if (!SD.begin(53)) {
            Serial.println("ERROR - SD card initialization failed!");
            return;    // init failed
        }
        Serial.println("SUCCESS - SD card initialized.");
        // check for index.htm file
        if (!SD.exists("index.htm")) {
            Serial.println("ERROR - Can't find index.htm file!");
            return;  // can't find index file
        }
        Serial.println("SUCCESS - Found index.htm file."); 


             // send web page
        webFile = SD.open("index.htm");        // open web page file
        if (webFile) {
            while(webFile.available()&gt; 0) {
                //client.write(webFile.read()); // send web page to client
               char ch = webFile.read(); // Read a character
               webpage += ch; // Add it
            }
            webFile.close();
        }
         Serial.print("file contents&gt;&gt;&gt;");
         //Serial.println(webpage);
         //delay(20000);
    }

    void loop()
    {
      if(Serial2.available()) // check if the esp is sending a message 
      {
        /*
        while(Serial2.available())
        {
          // The esp has data so display its output to the serial window 
          char c = Serial2.read(); // read the next character.
          Serial.write(c);
        } */

        if(Serial2.find("+IPD,"))
        {
         delay(1000);

         int connectionId = Serial2.read()-48; // subtract 48 because the read() function returns 
                                               // the ASCII decimal value and 0 (the first decimal number) starts at 48

         //String webpage = "&lt;h1&gt;Hello&lt;/h1&gt;&lt;h2&gt;World!&lt;/h2&gt;&lt;button id=\"11\"&gt;Toggle Pin 11&lt;/button&gt;";//&lt;button&gt;LED1&lt;/button&gt;";


         //Serial.print("file contents&gt;&gt;&gt;");
         //Serial.println(webpage);
         //delay(20000);
         String cipSend = "AT+CIPSEND=";
         cipSend += connectionId;
         cipSend += ",";
         cipSend +=webpage.length();
         cipSend +="\r\n";

         sendData(cipSend,1000,DEBUG);
         sendData(webpage,1000,DEBUG);

         //webpage="&lt;button&gt;LED2&lt;/button&gt;";

    //     cipSend = "AT+CIPSEND=";
    //     cipSend += connectionId;
    //     cipSend += ",";
    //     cipSend +=webpage.length();
    //     cipSend +="\r\n";
    //     
    //     sendData(cipSend,1000,DEBUG);
    //     sendData(webpage,1000,DEBUG);

         String closeCommand = "AT+CIPCLOSE="; 
         closeCommand+=connectionId; // append connection id
         closeCommand+="\r\n";

         sendData(closeCommand,3000,DEBUG);
        }
      }
    }

    void setupWiFi() {  
      // try empty AT command  
      Serial2.println("AT");  
      wait_for_esp_response(2000);  
      // set mode 1 (client)  
      Serial2.println("AT+CWMODE=1");  
      wait_for_esp_response(2000);   
      // reset WiFi module  
     /*  
      esp.print("AT+RST\r\n");  
      wait_for_esp_response(3000);  
      delay(5000);  
      */  
      // join AP  
      Serial2.print("AT+CWJAP=\"");  
      Serial2.print("xxxxxxx");  
      Serial2.print("\",\"");  
      Serial2.print("xxxxxxx");  
      Serial2.println("\"");  
      // this may take a while, so wait for 5 seconds  
      wait_for_esp_response(5000);  
      Serial2.println("AT+CIPSTO=30");   
      wait_for_esp_response(1000);  
      // start server  
      Serial2.println("AT+CIPMUX=1");  
      wait_for_esp_response(1000);  
      Serial2.print("AT+CIPSERVER=1,"); // turn on TCP service  
      Serial2.println(PORT);  
      wait_for_esp_response(1000);  
     }  

    String sendData(String command, const int timeout, boolean debug)
    {
        String response = "";

        Serial2.print(command); // send the read character to the Serial2

        long int time = millis();

        while( (time+timeout) &gt; millis())
        {
          while(Serial2.available())
          {

            // The esp has data so display its output to the serial window 
            char c = Serial2.read(); // read the next character.
            response+=c;
          }  
        }

        if(debug)
        {
          Serial.print(response);
        }

        return response;
    }
</code></pre>

<p>And this is what I am getting in web browser;<a href="https://i.stack.imgur.com/lhR2n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lhR2n.png" alt="Opera browser"></a></p>

<p>I am using Opera browser but I have tried in Chrome and firefox aswell. when I open this index.html page in my laptop with the same browser, it opens perfectly. </p>
 20527: <p>See the text from "'HTTP/1.1 200 OK'" until "'DOCTYPE html'" in the following couple of links which say the same, and you might like to read the rest of those pages:</p>

<ul>
<li><a href="https://www.arduino.cc/en/Tutorial/WebServer" rel="nofollow">https://www.arduino.cc/en/Tutorial/WebServer</a></li>
<li><a href="http://www.allaboutcircuits.com/projects/using-an-arduino-as-a-web-server" rel="nofollow">http://www.allaboutcircuits.com/projects/using-an-arduino-as-a-web-server</a></li>
</ul>
 19886: <p>Slightly off-topic, as this is not the source of your problem, but as jBot42 says, you need a proper regulator. When you use a voltage divider, your load (the ESP8266) is connected in parallel with one of the resistors (the 10K one) and effectively changes the output voltage of the regulator. As the consumption of power in the ESP8266 changes (e.g. when the wifi circuit transfers data), the voltage will also change.</p>

<p>At the very least you should place an electrolytic capacitor in parallel with the 10K resistor, but even this is not a good solution.</p>
 20579: <p>It looks like you did not send the HTTP headers. Before sending any of the HTML content send following lines:</p>

<pre><code>"HTTP/1.1 200 OK\r\n"
"Content-Type: text/html\r\n\r\n"
</code></pre>

<p>Make sure you send all the <code>\r\n</code> !</p>
