Webserver on SD-Card, fail on second call

Hi,
i’ve got a little problem with my Arduino Mega 2560 with Ethernet Shield.
I established a Webserver with HTML using the SD-Card-Slot.
The whole system works fine till the second usage of the webpage.
After that, the browser only shows a blank page. If i reset the Arduino, it works again for one single time.
I already thought about a running out of memory, but i already checked that.
Maybe you can take a look at the code or try it on your own.

Thanks in advance

oSternen

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

// MAC address from Ethernet shield sticker under board
byte mac[] = { 
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192, 168, 178, 7); // IP address, may need to change depending on network
EthernetServer server(80);  // create a server at port 80

File webFile;

//Initialising PWM-Ports
int TREIBER1 = 2;
int TREIBER2 = 3;
int TREIBER3 = 4;
int TREIBER4 = 5;
int TREIBER5 = 6;
int TREIBER6 = 7;
int TREIBER7 = 8;
// int TREIBER8 = 10; Ethernet-Shield
int TREIBER8 = 9;
int TREIBER9 = 11;
int TREIBER10 = 12;
int TREIBER11 = 13;
int TREIBER12 = 44;
int TREIBER13 = 45;
int TREIBER14 = 46;

String readString = String(35); 

const int MAX_PAGENAME_LEN = 8;  // max characters in page name 
char buffer[MAX_PAGENAME_LEN+1]; // additional character for terminating null

void setup()
{
  Serial.begin(9600);
  Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();           // start to listen for clients
  // -----initialize SD card
  Serial.begin(9600);       // for debugging
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) 
  {
    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.");
  //-----------------------
  pinMode(TREIBER1, OUTPUT);
  pinMode(TREIBER2, OUTPUT);
  pinMode(TREIBER3, OUTPUT);
  pinMode(TREIBER4, OUTPUT);
  pinMode(TREIBER5, OUTPUT);
  pinMode(TREIBER6, OUTPUT);
  pinMode(TREIBER7, OUTPUT);
  pinMode(TREIBER8, OUTPUT);
  pinMode(TREIBER9, OUTPUT);
  pinMode(TREIBER10, OUTPUT);
  pinMode(TREIBER11, OUTPUT);
  pinMode(TREIBER12, OUTPUT);
  pinMode(TREIBER13, OUTPUT);
  pinMode(TREIBER14, OUTPUT);

  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());
}

void loop()
{
  EthernetClient client = server.available();  // try to get client

  if (client)  // got client?
  { 
    boolean currentLineIsBlank = true;
    while (client.connected()) 
    {
      if (client.available()) 
      {   // client data available to read
        char c = client.read(); // read 1 byte (character) from client
        // last line of client request is blank and ends with \n
        // respond to client only after last line received
        if (readString.length() < 35)
        {
          //store characters to string
          readString = readString + c;
        }

        //Serial.print(c);
        if (c == '\n' && currentLineIsBlank) 
        {
          memset(buffer,0, sizeof(buffer));
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println("Connection: close");
          client.println();

          // send web page
          webFile = SD.open("index.htm");        // open web page file
          if (webFile) 
          {
            while(webFile.available()) 
            {
              client.write(webFile.read()); // send web page to client
            }
            webFile.close();
          }

          //---Smartphone optimization
          //          client.println("<meta name = 'viewport' content = 'initial-scale = 1.2'>");

          Processing(); 
          Serial.print("finished processing()");

          //clearing string for next read
          readString="";
          break;
        }
        // every line of text received from the client ends with \r\n
        if (c == '\n') {
          // last character on line of received text
          // starting new line with next character read
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // a text character was received from client
          currentLineIsBlank = false;
        }
      }    // end if (client.available())
    } // end while (client.connected())
    delay(2);      // give the web browser time to receive the data
    client.stop(); // close the connection
  } // end if (client)
}
void Processing()
{ 
  Serial.print("freeMemory()=");
  Serial.println(freeMemory());
  Serial.println("in Processing  ");
  Serial.println(" Der String:");
  Serial.println(readString);
  //---Muster 1
  if(readString.indexOf("M1") > -1) 
  {
    int i=(readString.indexOf("M1")+3);
    String val_string = readString.substring(i,i+3);
    int val = val_string.toInt();
    all_off();
    analogWrite (TREIBER7,val-100);
    Serial.println("LED 1 bei: ");
    Serial.println(val-100);
  }

  //---Muster 2
  else if(readString.indexOf("M2") > -1) 
  {
    int i=(readString.indexOf("M2")+3);
    String val_string = readString.substring(i,i+3);
    int val = val_string.toInt();
    all_off();
    analogWrite (TREIBER5,val-100);
    analogWrite (TREIBER7,val-100);
  }
  //---Muster 3
  else if(readString.indexOf("M3") > -1) 
  {
    int i=(readString.indexOf("M3")+3);
    String val_string = readString.substring(i,i+3);
    int val = val_string.toInt();
    all_off();
    analogWrite (TREIBER5,val-100);
  }
  //---Muster 4
  else if(readString.indexOf("M4") > -1) 
  {
    int i=(readString.indexOf("M4")+3);
    String val_string = readString.substring(i,i+3);
    int val = val_string.toInt();
    all_off();
    analogWrite (TREIBER3,val-100);
    analogWrite (TREIBER13,val-100);
    analogWrite (TREIBER5,val-100);
    analogWrite (TREIBER7,val-100);
    analogWrite (TREIBER11,val-100);
    analogWrite (TREIBER6,val-100);
  }
  //---Muster 5
  else if(readString.indexOf("M5") > -1) 
  {
    int i=(readString.indexOf("M5")+3);
    String val_string = readString.substring(i,i+3);
    int val = val_string.toInt();
    all_off();
    analogWrite (TREIBER3,val-100);
    analogWrite (TREIBER13,val-100);
    analogWrite (TREIBER11,val-100);
    analogWrite (TREIBER6,val-100);
  }
  //---Muster 6
  else if(readString.indexOf("M6") > -1) 
  {
    int i=(readString.indexOf("M6")+3);
    String val_string = readString.substring(i,i+3);
    int val = val_string.toInt();
    all_off();
    analogWrite (TREIBER3,val-100);
    analogWrite (TREIBER13,val-100);
    analogWrite (TREIBER11,val-100);
    analogWrite (TREIBER6,val-100);
    analogWrite (TREIBER7,val-100);
  }

  //---Muster 7
  else if(readString.indexOf("M7") > -1) 
  {
    int i=(readString.indexOf("M7")+3);
    String val_string = readString.substring(i,i+3);
    int val = val_string.toInt();
    all_off();
    analogWrite (TREIBER3,val-100);
    analogWrite (TREIBER13,val-100);
    analogWrite (TREIBER11,val-100);
    analogWrite (TREIBER6,val-100);
    analogWrite (TREIBER7,val-100);
    analogWrite (TREIBER4,val-100);
    analogWrite (TREIBER1,val-100);
    analogWrite (TREIBER10,val-100);
    analogWrite (TREIBER12,val-100);
  }
}

void all_off()
{
  analogWrite (TREIBER14, 0 );
  analogWrite (TREIBER1, 0);
  analogWrite (TREIBER2, 0);
  analogWrite (TREIBER3, 0);
  analogWrite (TREIBER4, 0);
  analogWrite (TREIBER5, 0);
  analogWrite (TREIBER6, 0);
  analogWrite (TREIBER7, 0);
  analogWrite (TREIBER8, 0);
  analogWrite (TREIBER9, 0);
  analogWrite (TREIBER10, 0);
  analogWrite (TREIBER11, 0);
  analogWrite (TREIBER12, 0);
  analogWrite (TREIBER13, 0);
}

HTML Page

[code]<!DOCTYPE html>
 <html>
<head><title>Arduino Webserver</title>
<style type="text/css"> 
body table {font-family: Verdana, Arial, Helvetica, sans-serif;font-size: 25px;} 
</style> 
</head>
      <body margin=0 padding=0 style='font-family:Verdana; background-color:#ffffff;'>


<meta name = 'viewport' content = 'initial-scale = 1.2'>
<table border='2'>
<tr>
<td>(&nbsp;&nbsp;) </td>
<td>(&nbsp;&nbsp;) </td>
<td>(&nbsp;&nbsp;) </td>
<td>(&nbsp;&nbsp;) </td>
<td>(&nbsp;&nbsp;) </td>
</tr>
<tr>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td>(&nbsp;&nbsp;)</td>

</tr>
<tr>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (<b><span style='color:#FFFF00'>O</b></span>)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>

</tr>
<tr>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>

</tr>
<tr>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
<td> (&nbsp;&nbsp;)</td>
</table>
                                              
<form oninput='amount.value=rangeInput.value'>
        <input type='range' id='rangeInput' name='M1' min='100' max='355' step='1' value='0'><input type=submit value=Change></output>
        <output name='amount' for='rangeInput'>0</output>
</form>
       
</body>
</html>

[/code]

Try disabling the SD SPI when starting the w5100. Leaving the SD SPI slave select floating while starting the w5100 has been known to mess up the SD card. While testing another user's sketch from the forum, I forgot to do this and it destroyed the format on one of my SD cards.

I rearranged your setup function. Try this one:

void setup()
{
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

  Ethernet.begin(mac, ip);  // initialize Ethernet device
  server.begin();           // start to listen for clients
  Serial.print("server is at ");
  Serial.println(Ethernet.localIP());

  // -----initialize SD card
  Serial.begin(9600);       // for debugging
  Serial.println("Initializing SD card...");
  if (!SD.begin(4)) 
  {
    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.");
  //-----------------------
  pinMode(TREIBER1, OUTPUT);
  pinMode(TREIBER2, OUTPUT);
  pinMode(TREIBER3, OUTPUT);
  pinMode(TREIBER4, OUTPUT);
  pinMode(TREIBER5, OUTPUT);
  pinMode(TREIBER6, OUTPUT);
  pinMode(TREIBER7, OUTPUT);
  pinMode(TREIBER8, OUTPUT);
  pinMode(TREIBER9, OUTPUT);
  pinMode(TREIBER10, OUTPUT);
  pinMode(TREIBER11, OUTPUT);
  pinMode(TREIBER12, OUTPUT);
  pinMode(TREIBER13, OUTPUT);
  pinMode(TREIBER14, OUTPUT);
}

You can try my server code. It does ok.
http://playground.arduino.cc/Code/WebServerST

Quick look it appears that your using pin 4 in part of your program.. I believe that's used in all boards for SD select.. I don't have a mega so I'm not 100% positive though.. Modify your sketch to NOT use pin 4 and retry it..

Many thanks to you all. That's a great forum here :slight_smile:

The Problem was indeed the usage of Pin 4 :slight_smile:

String readString = String(35);

Initialize readString with the value "35". How useful.