Unreliable HTTP Server

Hi, I've been plagued with problems with running a HTTP server off a Freetronics Etherten which is an Arduino Uno with ethernet shield built in. I get corruptions in the data sent back from the HTTP server. I was previously using Arduino 22 but now have started using 1.0.

I've still got the same corruptions. It seems to happen after the program has ran for a little while. Suggestions were that it was caused by running out of memory, but FreeMemory.h's routine placed in all different locations in the program never showed any less than 1000 bytes free. I've tried other things with buffers as well.

Finally in frustration I loaded the example WebServer sketch that comes with the Arduino download, and set up a perl script to query the server once every second. After about 30 seconds I got the same corrupt reply.

The corrupt reply usually contains random binary data interspersed with old strings from the program, sometimes from previous versions of the sketch that had been replaced with newer versions, as if somewhere in memory strings were staying around.

Does anyone have any other tips or suggestions of where I could start to look? I'm beginning to think it's a waste of time to do use anything other than USB Serial with Arduinos. Thanks,

David

in the W5100 buffer, for example...

With no code, who knows what might be the problem. Using a w5100 ethernet shield on an arduino, I had my arduino serve up 150k+ pages without an issue in a test.

The whole sketch is quite large, but here is the webserver portion:

void webServer() {

  char clientline[BUFSIZ];
  int index = 0;
  
  EthernetClient client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean current_line_is_blank = true;
    
    // reset the input buffer
    index = 0;
    
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        
        // If it isn't a new line, add the character to the buffer
        if (c != '\n' && c != '\r') {
          clientline[index] = c;
          index++;
          // are we too big for the buffer? start tossing out data
          if (index >= BUFSIZ) 
            index = BUFSIZ -1;
          
          // continue to read more data!
          continue;
        }
        
        // got a \n or \r new line, which means the string is done
        clientline[index] = 0;
        
        // Look for substring such as a request to get the root file
        if (strstr(clientline, "GET / ") != 0) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/plain");
          client.println();
          
//          String dateString = (String)year() + "-" + (String)month() + "-" + (String)day() + " " + (String)hour() + ":" + (String)minute() + ":" + (String)second();
          
//          client.println(dateString);
          client.println(mW);
//          client.println(jouleCounter);
//          client.println(avFreq);
//          client.println(tsl_sens);

        } 
        break;
      }
    }
    // give the web browser time to receive the data
    delay(1);
    client.stop();
  }
  
}

Basically a lift from the WebServer example sketch. But as I say I saw the WebServer example sketch do the exact same thing. Is there something I should be doing to ensure the buffers on the w5100 don't get full or do other untoward things? Thanks,

David

Simple meta refresh test:

// for W5100 ethernet shield
// the IP address will be dependent on your local network/router
// port 80 is default for HTTP, but can be changed as needed
// use IP address like http://192.168.1.102:84/ in your brouser
// or http://zoomkat.no-ip.com:84 with dynamic IP service
// use the \ slash to escape the " in the html

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

int x=0;
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 102 };
Server server(84);

void setup()
{
  // start the server
  Ethernet.begin(mac, ip);
  server.begin();
}

void loop()
{
  // listen for incoming clients
  Client client = server.available();
  if (client) {
     while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // see if HTTP request has ended with blank line
        if (c == '\n') {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          
          //meta-refresh page every 2 seconds
          x=x+1;
          client.println("<HTML>");
          client.print("<HEAD>");
          client.print("<meta http-equiv=\"refresh\" content=\"2\">");
          client.print("<TITLE />Zoomkat's meta-refresh test</title>");
          client.print("</head>");
          client.println("<BODY>");
          client.print("Zoomkat's meta-refresh test");
          client.println("
");
                    
          client.print("page refresh number ");
          client.println(x);
          client.println("
");
          client.println("
");
          
          client.print("Zoomkat's arduino analog input values:");
          client.println("
");
          client.println("
");
          
          // output the value of each analog input pin
          for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
            client.print("analog input ");
            client.print(analogChannel);
            client.print(" is ");
            client.print(analogRead(analogChannel));
            client.println("
");
            }
           break;
          client.println("</BODY>");
          client.println("</HTML>");
         }
        }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

Thanks. Tried that sketch, just modified IP address to suit my network and changed Server and Client to EthernetServer and EthernetClient respectively for Arduino 1.0. On refresh 96 I got corrupted output, and continued on 97 and 98.

Zoomkat's meta-refresh test
t's meta-refresh test
Zoomkat's meta-refresh test
page refresh number 97

Zoomkat's arduino analog input valuesHTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
HTTP/1.1 200 OK Content-Type: text/html HTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
page refresh number 103

Zoomkat's arduino analog input values:

analog input 0 is 651
analog input 1 is 447
analog input 2 is 347
analog input 3 is 257
analog input 4 is 1023
analog input 5 is 1023
analog input 0 is 658
analog input 1 is 454
analog input 2 is 353 Zoomkat's meta-refresh test
page refresh number 107

Zoomkat's arduino analog input values:

analog input 0 is 648
analog input 1 is 444
analog input 2 is 344
analog input 3 is 254
analog input 4 is 1023
analog input 5 is 1023
E />Zoomkat's meta-refresh test Zoomkat's meta-refresh test
page refresh number 105

Zoomkat's arduino analog input values:

analog input 0 is 650
analog iHTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
t's meta-refresh test
Zoomkat's meta-refresh test
page refresh number 97

Zoomkat's arduino analog input valuesHTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
HTTP/1.1 200 OK Content-Type: text/html HTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
page refresh number 103

Zoomkat's arduino analog input values:

analog input 0 is 651
analog input 1 is 447
analog input 2 is 347
analog input 3 is 257
analog input 4 is 1023
analog input 5 is 1023
analog input 0 is 658
analog input 1 is 454
analog input 2 is 353 Zoomkat's meta-refresh test
page refresh number 107

Zoomkat's arduino analog input values:

analog input 0 is 648
analog input 1 is 444
analog input 2 is 344
analog input 3 is 254
analog input 4 is 1023
analog input 5 is 1023
E />Zoomkat's meta-refresh test Zoomkat's meta-refresh test
page refresh number 105

Zoomkat's arduino analog input values:

analog input 0 is 650
analog iHTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
t's meta-refresh test
Zoomkat's meta-refresh test
page refresh number 97

Zoomkat's arduino analog input valuesHTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
HTTP/1.1 200 OK Content-Type: text/html HTTP/1.1 200 OK Content-Type: text/html Zoomkat's meta-refresh test
page refresh number 103

Zoomkat's arduino analog input values:

analog input 0 is 651
analog input 1 is 447
analog input 2 is 347
analog input 3 is 257
analog input 4 is 1023
analog input 5 is 1023
analog input 0 is 658
analog input 1 is 454
analog input 2 is 353 Zoomkat's meta-refresh test
page refresh number 107

Zoomkat's arduino analog input values:

analog input 0 is 648
analog input 1 is 444
analog input 2 is 344
analog input 3 is 254
analog input 4 is 1023
analog input 5 is 1023
E />Zoomkat's meta-refresh test Zoomkat's meta-refresh test
page refresh number 105

Zoomkat's arduino analog input values:

analog input 0 is 650

It goes on like this for some length, that's a truncated version of the full output. The first corrupted refresh it was only the length of the normal page when it worked. After this page I've posted here it basically locked up. Thanks,

David

Here is a bug with a fix that caused me the same problem. http://code.google.com/p/arduino/issues/detail?id=605&start=200 See if that helps you.

Also, if the device has a microSD card reader, check that you have disabled the microSD card reader SPI interface. That is pin 4 on the standard ethernet shield.

void setup()
{
   pinMode(4,OUTPUT);
   digitalWrite(4,HIGH);
   Ethernet.begin(mac,ip);
   // rest of setup stuff
}

I tried that modification on the w5100.h file. It’s in a slightly different location, but same code. After recompiling I’m getting the same error. After about reload 80 on Zoomkat’s sketch it’s corrupted again and by 89 it seems to be all locked up. Thanks,

David

So what is different between my setup that served 150,000+ pages over a number of days, and your setup that crashes at 80 updates? Well, at a minimum you are running an new/different board, you are using a new/different IDE with new/different libaries. You probably can't change the board, but you can try using the 0022 IDE and ethernet libaries to see if you have the same issues.

I've been thinking about that. Perhaps it could be due to the electronics I've got on my protoshield. I might remove that and see what happens. Barring that I've been thinking I might get a standard Ethernet shield for my Aruduino Uno and see if that performs differently. Thanks,

David

and set up a perl script to query the server once every second.

That might be another wild card to look at. I used IE to test the meta refresh code.

I had trouble with large broadcast packets that the wifi library didn't defend against, causing buffer overrun. Can you run your system on an isolated network segment and/or use wireshark to see what's on the wire? Does Zoomcat's example code crash in your setup consistently at the same number of iterations?