Webduino HelloWorld - Random garbage in browser

My environment:

Hardware:
MacBookPro running OS X 10.7.5
OSEPP Mega 2560
Arduino Ethernet Shield
Empty, unformatted 2GB mini-SD card in slot
Cabling directly from Mac to Ethernet Shield with 3' patch cord and crossover adaptor

Software:
Arduino IDE 1.0.3
Web_HelloWorld.pde downloaded from sirleech (Christopher Lee) · GitHub today.
Arduino is at 192.168.1.210, Mac is at 192.168.1.100
DHCP is turned on

So I was having problems with example called WebServer (the one that spits out 6 analog pin readings to the client), so I went looking for another solution. I tried Webduino and got pretty much the same result.

I'm running the HelloWorld sketch, right out of the box, exactly as provided at github.

The first time I run it after loading the Arduino, it works:

Hello, World!

After a random number of reloads of the page, I will see something like this:

Loopcount = 61254 /> <???
Hello, World!
HTTP/1.0 400 Bad RequHTTP/1.0 200 OK Server: Webduino/1.7 Access-Control-Allow-Origin: * HTTP/1.0 200 OK Server: Webduino/1.7 Access-Control-Allow-Origin: * Content-Type: text/html; charset=utf-8
Hello, World!
o/1.7 Access-Control-Allow-Origin: * Content-Type: text/html; charset=utf-8
Hello, World!

(.... lots more like this deleted ....)

Much of the garbage that is sprinkled throughout the 600+ lines (about 41kB) of output in the browser is stuff I recognize from previous runs when I was attempting to debug the other example code (it was doing very similar stuff as well).

I also got the "Epic Fail" message once, plus a bunch of this historical data, but I haven't seen it again as I type this.

Any ideas?

Why are you connecting the Arduino to the PC? If that is really an option, why bother with the Ethernet shield?

Connect the Arduino with Ethernet shield to the router, as it was intended to be connected.

My reason for using a crossover adaptor is that during development in a Starbucks or public library, it's a really neat solution to not being able to hook my Arduino up to a router.

I'm now at home, have it connected to my router and have the exact same results.

Thoughts?

K

Have you tried any other examples? I have a web server sketch in the playground.
http://playground.arduino.cc/Code/WebServerST

zoomkat has some good server examples on the forum also. Have you tried those?

edit: I forgot to mention that a SD card in the slot on the shield will cause garbage on the SPI if not disabled or initialized. If you have one in the slot, remove it for the tests.

Your SDCard suggestion was the magic bullet, it appears.

I had tried increasing the polling delay (main loop, each time through it checks for client):

...
EthernetClient client = server.available();
...

I put a delay as high as 3-4 seconds in the loop. When I do that, the funky behavior disappears WITH OR WITHOUT the SD Card. However, to run the loop at full speed I must remove the SDCard.

Now I need to dig in and figure out how to run with the SD Card, because that's part of my plan. I'd like to serve web pages read from the memory card.

Thanks for your help!!

Kevin

This setup code initializes both correctly.

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

  // disable w5100 SPI while starting SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print(F("Starting SD..."));
  if(SD.begin(4) == 0) Serial.println(F("fail"));
  else Serial.println(F("ok"));

  Serial.print(F("Starting w5100..."));
  Ethernet.begin(mac, ip, gateway, gateway, subnet); 
  Serial.println(Ethernet.localIP());

  Serial.println(F("Ready."));
}

edit: I added the F() function to the static strings to save SRAM.

Now I need to dig in and figure out how to run with the SD Card, because that's part of my plan. I'd like to serve web pages read from the memory card.

Below is some test code that serves a couple types of files from the SD card. It includes SurferTim's method of forming large data packets to decrease the file download time. You should be able to download the needed files from the links provided, put them on your SD card, and see how it works.

//zoomkat 2/26/13
//SD server slider test code
//open serial monitor to see what the arduino receives
//browser address will look like http://192.168.1.102:84/servosld.htm when submited
//for use with W5100 based ethernet shields
//put the servosld.htm, slider.js, bluev_sl.gif,
//and bluev_bg.gif on the SD card
//download flies at:
// http://web.comporium.net/~shb/pix/servosld.htm
// http://web.comporium.net/~shb/pix/slider.js
// http://web.comporium.net/~shb/pix/bluev_bg.gif
// http://web.comporium.net/~shb/pix/bluev_sl.gif
// 

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

#include <Servo.h> 
Servo myservoa, myservob, myservoc, myservod;  // create servo object to control a servo 
Servo myservoe, myservof, myservog; // myservoh not used due to lack of another free pin
String readString, pos;

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

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

void setup(){

  Serial.begin(9600);

  // disable w5100 while setting up SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);
  Serial.print("Starting SD..");
  if(!SD.begin(4)) Serial.println("failed");
  else Serial.println("ok");

  Ethernet.begin(mac, ip, gateway, gateway, subnet);

  //delay(2000);
  server.begin();
  Serial.println("Ready");

  myservoa.attach(2);  //the pin for the servoa control
  myservob.attach(3);  //the pin for the servob control
  myservoc.attach(5);  //the pin for the servoc control
  myservod.attach(6);  //the pin for the servod control 
  myservoe.attach(7);  //the pin for the servoa control
  myservof.attach(8);  //the pin for the servob control
  myservog.attach(9);  //the pin for the servoc control
  //myservoh.attach(10);  //the pin for the servod control 

}

void loop(){
  // Create a client connection
  EthernetClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();

        //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

            //select proper header for file to be sent to browser

          client.println("HTTP/1.1 200 OK"); //send new page
          if(readString.indexOf("servosld") >=0) {
            client.println("Content-Type: text/html");
            client.println(); 
          }

          if(readString.indexOf("slider") >=0) {
            client.println("Content-Type: application/x-javascript");
            client.println(); 
          }

          if(readString.indexOf("bluev") >=0) {
            client.println("Content-Type: image/gif");
            client.println(); 
          }

          //select file to send to browser
          if(readString.indexOf("servosld") >=0) {
            File myFile = SD.open("SERVOSLD.HTM");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;              

              while (myFile.available()) 
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }                
              }
              if(clientCount > 0) client.write(clientBuf,clientCount);            
              myFile.close();
            }
          }

          if(readString.indexOf("slider") >=0) {
            File myFile = SD.open("slider.js");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;              

              while (myFile.available()) 
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }                
              }
              if(clientCount > 0) client.write(clientBuf,clientCount); 
              myFile.close();
            }
          }

          if(readString.indexOf("bluev_sl") >=0) {
            File myFile = SD.open("bluev_sl.gif");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;              

              while (myFile.available()) 
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }                
              }
              if(clientCount > 0) client.write(clientBuf,clientCount); 
              myFile.close();
            }
          }

          if(readString.indexOf("bluev_bg") >=0) {
            File myFile = SD.open("bluev_bg.gif");
            if (myFile) {

              byte clientBuf[64];
              int clientCount = 0;              

              while (myFile.available()) 
              {
                clientBuf[clientCount] = myFile.read();
                clientCount++;

                if(clientCount > 63)
                {
                  client.write(clientBuf,64);
                  clientCount = 0;
                }                
              }
              if(clientCount > 0) client.write(clientBuf,clientCount); 
              myFile.close();
            }
          }

          delay(1);
          //stopping client
          client.stop();

          //process GET string request from client and and position servo

          pos = readString.substring(8, 12); //get the first four characters         
          //Serial.println(pos);
          int n = pos.toInt();  //convert readString into a number   
          Serial.println(n); 
          Serial.println();

          if(readString.indexOf("?0") >0) myservoa.writeMicroseconds(n);
          if(readString.indexOf("?1") >0) myservob.writeMicroseconds(n);
          if(readString.indexOf("?2") >0) myservoc.writeMicroseconds(n);
          if(readString.indexOf("?3") >0) myservod.writeMicroseconds(n);
          if(readString.indexOf("?4") >0) myservoe.writeMicroseconds(n);
          if(readString.indexOf("?5") >0) myservof.writeMicroseconds(n);
          if(readString.indexOf("?6") >0) myservog.writeMicroseconds(n);
          //only seven servo pins, so back to myservoa for testing
          if(readString.indexOf("?7") >0) myservoa.writeMicroseconds(n);

          //clearing string for next read
          readString="";
          pos="";
        }
      }
    }
  } 
}