Arduino LED control via http thru Ethernet Shield

I am working on a simple LED control program using my Seeedstudio Ethernet Shield v2.0, and have received no errors inside the compiler at this point, but when I visit the IP given through Serial, nothing is received. Your help would be much appreciated.

#include <SPI.h>
#include <EthernetV2_0.h>
#define W5200_CS 10
#define SDCARD_CS 4

int led = 3;

byte mac[] = { 0xAB, 0xBC, 0xCD, 0xDE, 0xEF, 0xFA};
byte ip[] = { 192, 168, 1, 177};

String readString;

EthernetServer server(80);

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

  pinMode(led, OUTPUT);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.println(Ethernet.localIP());
}


void loop() {
  EthernetClient client = server.available();
  if (client)
  {
    while (client.connected())
    {
      if (client.available())
      {

        char c = client.read();

        if (readString.length() < 100)
        {
          readString += c;
        }

        if (c == '\n')
        {
          Serial.println(readString); //print to serial monitor for debuging

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>LED Controller</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>LED CONTROL</H1>");
          client.println("<hr />");
          client.println("
");
          client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
          client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");
          client.println("
");
          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          client.stop();

          if (readString.indexOf("?button1on") > 0)
          {
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button1off") > 0)
          {
            digitalWrite(led, LOW);
          }
          readString = "";
        }
      }
      // give the web browser time to receive the data
      delay(1);
      // close the connection:
      client.stop();
      Serial.println("client disonnected");
    }
  }
}

WebServerControl.ino (2.32 KB)

Are you certain that 192.168.1.177 is in the localnet IP range?

Yes, I am certain. While running the 'WebServer' example I used the same IP and MAC addresses and it ran without issue.

It appears the client stop is in the wrong place. It should be outside the "while(client.connected())" loop. Your code reads one character, then calls client stop().

      // give the web browser time to receive the data
      delay(1);
      // close the connection:
      client.stop();
      Serial.println("client disonnected");
    }
// should be here
  }
}

Thanks! I'll try that!

I moved that section of code, but I still get the same time out error in Chrome

Add some serial debugging. I added a few lines that I commented with “// new”. What does the serial monitor show?

#include <SPI.h>
#include <EthernetV2_0.h>
#define W5200_CS 10
#define SDCARD_CS 4

int led = 3;

byte mac[] = { 0xAB, 0xBC, 0xCD, 0xDE, 0xEF, 0xFA};
byte ip[] = { 192, 168, 1, 177};

String readString;

EthernetServer server(80);

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

  pinMode(led, OUTPUT);

  pinMode(SDCARD_CS, OUTPUT);
  digitalWrite(SDCARD_CS, HIGH);

  Ethernet.begin(mac, ip);
  server.begin();
  Serial.println(Ethernet.localIP());
}


void loop() {
  EthernetClient client = server.available();
  if (client)
  {
// new
    Serial.println("Client connected");

    while (client.connected())
    {
      if (client.available())
      {

        char c = client.read();
// new
        Serial.write(c);

        if (readString.length() < 100)
        {
          readString += c;
        }

        if (c == '\n')
        {

// new
          Serial.println("Sending response");

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

          client.println("HTTP/1.1 200 OK"); //send new page
          client.println("Content-Type: text/html");
          client.println();
          client.println("<HTML>");
          client.println("<HEAD>");
          client.println("<TITLE>LED Controller</TITLE>");
          client.println("</HEAD>");
          client.println("<BODY>");
          client.println("<H1>LED CONTROL</H1>");
          client.println("<hr />");
          client.println("
");
          client.println("<a href=\"/?button1on\"\">Turn On LED</a>");
          client.println("<a href=\"/?button1off\"\">Turn Off LED</a>
");
          client.println("
");
          client.println("</BODY>");
          client.println("</HTML>");

          delay(1);
          client.stop();

          if (readString.indexOf("?button1on") > 0)
          {
            digitalWrite(led, HIGH);
          }
          if (readString.indexOf("?button1off") > 0)
          {
            digitalWrite(led, LOW);
          }
          readString = "";
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
    Serial.println("client disconnected");
  }
}

Running the program with your additions I do not get any additional messages in the Serial Monitor. The only thing that appears is the IP address.

That means the client is not connecting for some reason. Are you sure the WebServer example worked ok? Does it still work?

I don't have a w5200, but I changed your sketch to the standard ethernet library include, and the IP to match my localnet, and your code worked ok here.

edit: I even tried it with the Seeed Studio W5200 library (EthernetV2_0), commented out the "define W5200" in the w5200.h file, and it worked ok with my w5100.

Thanks for all of your help. After hours of troubleshooting, I decided to use an example program to build it off of, and it now works quite well. The program uses the same method to connect the client, but it is working just fine, which is quite odd. Anyways, thank you again for the help. I have been very impressed with these first few weeks in the Arduino community, and you have been a prime example of it's greatness.