Sending web page as text

Hi. Sorry if this is an obvious question… but I am trying to read a web page from SD card then send it to the client. I am using Arduino Uno and Arduino Ethernet Shield with the embeded card reader.
I have the following code:

//rudolf 25-02-2014
//Dimmer and switch from SD
//routerbot code
//for use with IDE 0021
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields
//version: 1.2.13

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 
int dim = 50;
File myFile;
int sdinit = 0;

//////////////////////

void setup(){

  pinMode(6, OUTPUT); //pin selected to control
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("version: 1.2.13"); // so I can keep track of what is loaded
  digitalWrite(10, HIGH);
  digitalWrite(4, LOW);
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  }
  else {
    Serial.println("initialization done.");
  }
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        boolean reading = false;
        int outp = 0;
        char c = client.read();
        analogWrite(9, dim);
        

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Rudolf");
               client.println("Content-Type: text/html");
               client.println();  
             }
             else {
              myFile = SD.open("test.txt");        // open web page file
                    if (myFile) {
                        while(myFile.available()) {
                            client.write(myFile.read()); // send web page to client
                        }
                        myFile.close();
                    }
             }

          delay(1);
          //stopping client
          client.stop();
          
          if(readString.indexOf("HIGH") >0 || readString.indexOf("LOW") >0){
            if(readString.indexOf("HIGH") >0){
              outp = 1;
            }
            else if(readString.indexOf("LOW") >0){
              outp = 0;
            }
            if(readString.indexOf("HIGH6") >0 || readString.indexOf("LOW6") >0){
              c = '6';
            }
            else if(readString.indexOf("HIGH2") >0 || readString.indexOf("LOW2") >0){
              c = '2';
            }
            else if(readString.indexOf("HIGH3") >0 || readString.indexOf("LOW3") >0){
              c = '3';
            }
            else if(readString.indexOf("HIGH4") >0 || readString.indexOf("LOW4") >0){
              c = '4';
            }
            else if(readString.indexOf("HIGH5") >0 || readString.indexOf("LOW5") >0){
              c = '5';
            }
            else if(readString.indexOf("HIGH9") >0 || readString.indexOf("LOW9") >0){
              c = '9';
            }
            
            switch (c) {
              case '6':
              //add code here to trigger on 2
              if(outp == 1){
                digitalWrite(6, HIGH);
              }
              else{
                digitalWrite(6, LOW);
              }
              break;
              case '2':
              //add code here to trigger on 3
              if(outp == 1){
                digitalWrite(2, HIGH);
              }
              else{
                digitalWrite(2, LOW);
              }
              break;
              case '3':
              //add code here to trigger on 4
              if(outp == 1){
                digitalWrite(3, HIGH);
              }
              else{
                digitalWrite(3, LOW);
              }
              break;
              case '4':
              //add code here to trigger on 5
              if(outp == 1){
                digitalWrite(4, HIGH);
              }
              else{
                digitalWrite(4, LOW);
              }
              //printHtml(client);
              break;
              case '5':
              //add code here to trigger on 6
              if(outp == 1){
                digitalWrite(5, HIGH);
              }
              else{
                digitalWrite(5, LOW);
              }
              break;
              case '9':
              if(outp == 1){
                if (dim < 250){
                  dim = dim + 10;
                  analogWrite(9, dim);
                }
                else if(dim == 250){
                  dim = dim + 5;
                  analogWrite(9, dim);
                }
              }
              else{
                if (dim == 5){
                  dim = dim - 5;
                  analogWrite(9, dim);
                }
                else if(dim > 0){
                  dim = dim - 10;
                  analogWrite(9, dim);
                }
              }
              break;
            }
            
          //clearing string for next read
          readString="";
          c='0' ;
          }
        }
      }
    }
  }
}

And test.txt contains the following text:

Rudolf Arduino Internet Control

Rudolf Arduino Internet Control

Pin 2 &nbsp&nbsp ON &nbsp&nbsp OFF

<a target=“inlineframe”>Pin 3
<a href="/?HIGH3" target=“inlineframe”>&nbsp&nbsp ON
<a href="/?LOW3" target=“inlineframe”>&nbsp&nbsp OFF

<a target=“inlineframe”>Pin 4
<a href="/?HIGH4" target=“inlineframe”>&nbsp&nbsp ON
<a href="/?LOW4" target=“inlineframe”>&nbsp&nbsp OFF

<a target=“inlineframe”>Pin 5
<a href="/?HIGH5" target=“inlineframe”>&nbsp&nbsp ON
<a href="/?LOW5" target=“inlineframe”>&nbsp&nbsp OFF

<a target=“inlineframe”>Pin 6
<a href="/?HIGH6" target=“inlineframe”>&nbsp&nbsp ON
<a href="/?LOW6" target=“inlineframe”>&nbsp&nbsp OFF

Dimmer - Pin 9 =
dim/2.55
%
<a href="/?HIGH9" target=“inlineframe” onclick=“setTimeout(‘window.location.reload(true);’,50);”>&nbsp&nbsp +3.92%
<a href="/?LOW9" target=“inlineframe” onclick=“setTimeout(‘window.location.reload(true);’,50);”>&nbsp&nbsp -3.92%

dim

However, when I access the web page (meaning 192.168.1.102:84), all i get is the web page as text (the browser doesn’t actually convert the code to html, it just spits it out as plain text).
Any ideas are very welcome.
Thanks in advance.

The WebClient example sends these three lines before the data:

          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");  // the connection will be closed after completion of the response

Why doesn’t your code?

1) actually, it does contain a variation of those lines 2) I found that those 3 lines can be almost anything, as they are just to check for the HTTP response from the client 3) already tried that...doesn't work. Still just code displayed as plain text.

1) actually, it does contain a variation of those lines

But only when the GET request contained a '?'.

2) I found that those 3 lines can be almost anything, as they are just to check for the HTTP response from the client

No. They tell the client what kind of data you are sending it, among other things, so that it knows how to display the data.

3) already tried that...doesn't work. Still just code displayed as plain text.

I'd guess, then, that you did not put them in the code in the right place. Since you modified the code, you should have re-posted it.

 //rudolf 25-02-2014
//Dimmer and switch from SD
//routerbot code
//for use with IDE 0021
//open serial monitor to see what the arduino receives
//use the \ slash to escape the " in the html 
//address will look like http://192.168.1.102:84/ when submited
//for use with W5100 based ethernet shields
//version: 1.2.13

#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //physical mac address
byte ip[] = { 192, 168, 1, 102 }; // ip in lan
byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
EthernetServer server(84); //server port

String readString; 
int dim = 50;
File myFile;
int sdinit = 0;

//////////////////////

void setup(){

  pinMode(6, OUTPUT); //pin selected to control
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(9, OUTPUT);
  pinMode(10, OUTPUT);
  
  //start Ethernet
  Ethernet.begin(mac, ip, gateway, subnet);
  server.begin();

  //enable serial data print 
  Serial.begin(9600); 
  Serial.println("version: 1.2.13"); // so I can keep track of what is loaded
  digitalWrite(10, HIGH);
  digitalWrite(4, LOW);
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) {
    Serial.println("initialization failed!");
  }
  else {
    Serial.println("initialization done.");
  }
}

void loop(){
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        boolean reading = false;
        int outp = 0;
        char c = client.read();
        analogWrite(9, dim);
        

        //read char by char HTTP request
        if (readString.length() < 100) {

          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } 

        //if HTTP request has ended
        if (c == '\n') {

          ///////////////
          Serial.println(readString); //print to serial monitor for debuging 

          //now output HTML data header
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 200 OK");
               client.println("Content-Type: text/html");
               client.println("Connection: close");
             }
             else {
              myFile = SD.open("test.txt");        // open web page file
                    if (myFile) {
                        while(myFile.available()) {
                            client.write(myFile.read()); // send web page to client
                        }
                        myFile.close();
                    }
             }

          delay(1);
          //stopping client
          client.stop();
          
          if(readString.indexOf("HIGH") >0 || readString.indexOf("LOW") >0){
            if(readString.indexOf("HIGH") >0){
              outp = 1;
            }
            else if(readString.indexOf("LOW") >0){
              outp = 0;
            }
            if(readString.indexOf("HIGH6") >0 || readString.indexOf("LOW6") >0){
              c = '6';
            }
            else if(readString.indexOf("HIGH2") >0 || readString.indexOf("LOW2") >0){
              c = '2';
            }
            else if(readString.indexOf("HIGH3") >0 || readString.indexOf("LOW3") >0){
              c = '3';
            }
            else if(readString.indexOf("HIGH4") >0 || readString.indexOf("LOW4") >0){
              c = '4';
            }
            else if(readString.indexOf("HIGH5") >0 || readString.indexOf("LOW5") >0){
              c = '5';
            }
            else if(readString.indexOf("HIGH9") >0 || readString.indexOf("LOW9") >0){
              c = '9';
            }
            
            switch (c) {
              case '6':
              //add code here to trigger on 2
              if(outp == 1){
                digitalWrite(6, HIGH);
              }
              else{
                digitalWrite(6, LOW);
              }
              break;
              case '2':
              //add code here to trigger on 3
              if(outp == 1){
                digitalWrite(2, HIGH);
              }
              else{
                digitalWrite(2, LOW);
              }
              break;
              case '3':
              //add code here to trigger on 4
              if(outp == 1){
                digitalWrite(3, HIGH);
              }
              else{
                digitalWrite(3, LOW);
              }
              break;
              case '4':
              //add code here to trigger on 5
              if(outp == 1){
                digitalWrite(4, HIGH);
              }
              else{
                digitalWrite(4, LOW);
              }
              //printHtml(client);
              break;
              case '5':
              //add code here to trigger on 6
              if(outp == 1){
                digitalWrite(5, HIGH);
              }
              else{
                digitalWrite(5, LOW);
              }
              break;
              case '9':
              if(outp == 1){
                if (dim < 250){
                  dim = dim + 10;
                  analogWrite(9, dim);
                }
                else if(dim == 250){
                  dim = dim + 5;
                  analogWrite(9, dim);
                }
              }
              else{
                if (dim == 5){
                  dim = dim - 5;
                  analogWrite(9, dim);
                }
                else if(dim > 0){
                  dim = dim - 10;
                  analogWrite(9, dim);
                }
              }
              break;
            }
            
          //clearing string for next read
          readString="";
          c='0' ;
          }
        }
      }
    }
  }
}
             if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 200 OK");
               client.println("Content-Type: text/html");
               client.println("Connection: close");
             }
             else {
              myFile = SD.open("test.txt");        // open web page file
                    if (myFile) {
                        while(myFile.available()) {
                            client.write(myFile.read()); // send web page to client
                        }
                        myFile.close();
                    }
             }

So, if the data in the file is to be sent, send it without any header telling the client that the data is to be understood as html. Without that, the client (rightly) assumes text.

And, that IS the behavior you are seeing. Either send those three lines unconditionally, or move them into the else block and put that stuff you had in the if block back. It isn't rocket science.

Already did that too in version 1.2.09 (yeah...I'm one of those geeks that keep track of their version history :D ). The page then simply fails to load (no response from the server).

if(readString.indexOf('?') >=0) { //don't send new page
               client.println("HTTP/1.1 204 Rudolf");
               client.println("Content-Type: text/html");
             }
             else {
               client.println("HTTP/1.1 200 Rudolf");
               client.println("Content-Type: text/html");
               client.println("Connection: close");  // the connection will be closed after completion of the response 
               myFile = SD.open("test.txt");        // open web page file
                    if (myFile) {
                        while(myFile.available()) {
                            client.write(myFile.read()); // send web page to client
                        }
                        myFile.close();
                    }
             }

Already did that too in version 1.2.09 (yeah...I'm one of those geeks that keep track of their version history smiley-grin ). The page then simply fails to load (no response from the server).

Can't see that version. + Can't see your serial output.

+ Can't see your browser input.

Can't help. Sorry.

Serial output:

version: 1.2.09
Initializing SD card…
initialization done.
GET / HTTP/1.1

Attached image of LOADED webpage
Code is in previous post

You forgot to send a blank line as the "end of header" marker.

               client.println("HTTP/1.1 200 Rudolf");
               client.println("Content-Type: text/html");

// you didn't send an "end of header" blank line like this. Note the added "\r\n"
               client.println("Connection: close\r\n");  // the connection will be closed after completion of the response 

               myFile = SD.open("test.txt");        // open web page file
                    if (myFile) {
                        while(myFile.available()) {
                            client.write(myFile.read()); // send web page to client
                        }
                        myFile.close();

Yep....that did it....I knew it was something stupid that I forgot :D Thanks a lot Surfer :)