-2

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;Opera 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.

8
  • Did you tell the browser that you're sending it HTML? Apr 26, 2016 at 19:15
  • Looks like you're lacking the whole of the header portion to me...
    – Majenko
    Apr 26, 2016 at 19:27
  • I am following link this tutorials. Its seems like he is also not sending the header portion. Apr 26, 2016 at 19:31
  • 1
    @AnumSheraz try using the developer tools to get the headers. To do this for instance in Chrome press F12, go to the network tab, load the page and look at the files being retrieved. Find the index.html file, right click on it and press "Copy response headers"; then post them in your question: this way we'll be able to see what are the missing ones
    – frarugi87
    Apr 26, 2016 at 20:28
  • Also, please get a proper 3.3V regulator.
    – 3871968
    Apr 26, 2016 at 23:45

4 Answers 4

4

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 !

2

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:

2
  • this is an html question, not an arduino question. What is it doing here? Sending html from an esp8266 is no different than sending it from any other device.
    – JayEye
    Apr 27, 2016 at 6:48
  • Before asking the question, I didn't knew that its could be html problem, I thought it might the voltage issue (as many ppl have this issue with there esp8266). Apr 27, 2016 at 13:37
2

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.

-1

Make sure you save the SD card file as index.htm.

That is what you are calling for in your code. Your description has a reference to index.html. You will get the result you see.

1
  • Whilst you are correct, in that there is a mismatch between the source code calling a .htm file and the OP saying that the file on the SD card is a .html file, this is most likely a typo, in the question, on the part of the OP. The Arduino is clearly accessing the file correctly. So, unfortunately, this will not fix the issue that the OP is experiencing. The correct answer has already been provided, which is that the HTTP headers were missing. Dec 11, 2016 at 0:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.