ethernet shield problem

i have gotten a project off of instructables.com to use a website to change a light color. its a beginner project and i used the 1.0 project code. my problem is my router give the shield an ipaddress and it expires. the router is set to allow 1 day leases. however as soon as i hook up the shield and look at the addresses handed out the shield is expired.

i havent changed anything and all files are posted here http://www.instructables.com/id/Arduino-Web-LED/

i just dont know the ethernet shield enough to do anything with it in regards to troubleshooting.

my problem is my router give the shield an ipaddress and it expires. the router is set to allow 1 day leases. however as soon as i hook up the shield and look at the addresses handed out the shield is expired.

How do you know it expired? Where are you looking at the addresses? In the router? Or the Arduino? What are the addresses it shows?

edit Sory bout mie speling. I wus rilly gud. lol

i use a linksys wireless router with 4 ports for wired lines. if you go to the router GUI and status -> local network tab you can see the client statuses. all my computers say how long the lease is left but the arduino shows expired and i can not access it.

I don't see the code at the site, so I can't comment on it.

Does the shield code show if it got an ip ok?

here is the code i had to dig it out of where i had it freakishly hidden from myself.

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

const char html[] =
  "<html><head>"
    "<script type=\"text/javascript\">"
    "var r;"
    "try {"
      "r = new XMLHttpRequest();"
    "} catch (e) {"
      "try {"
        "r = new ActiveXObject('Microsoft.XMLHTTP');"
      "} catch (e) {}"
    "}"
    "function set (c) {"
      "r.open('PUT', './led/' + c, false);"
      "r.send(null);"
    "}"
    "</script>"
    "<style type=\"text/css\">"
      ".b {width:112; height:112}"
      ".g {color:lightgrey}"
    "</style>"
  "</head>"
  "<body><table height=\"100%\" width=\"100%\">"
    "<tr><td align=\"center\" valign=\"middle\">"
      "<p>"
        "<input type=\"button\" class=\"b\" style=\"background-color:#ff0000\" onclick=\"set('ff0000')\"/>&nbsp;&nbsp;"
        "<input type=\"button\" class=\"b\" style=\"background-color:#00ff00\" onclick=\"set('00ff00')\"/>&nbsp;&nbsp;"
        "<input type=\"button\" class=\"b\" style=\"background-color:#0000ff\" onclick=\"set('0000ff')\"/>"
      "</p>"
      "<p>HTML served from <a href=\"\">this</a> Arduino, made accessible by <a href=\"http://www.yaler.org/\">Yaler</a>.</p>"
    "</td></tr>"
  "</table></body></html>";

const char http200[] = "HTTP/1.1 200 OK";
const char contentLength[] = "Content-Length: ";
const char connectionClose[] = "Connection: close";
const char contentTypeTextHtml[] = "Content-Type: text/html";
const char contentTypeTextPlain[] = "Content-Type: text/plain";
const byte RECEIVING = 0, 
           READ_CR = 1, 
           READ_CRLF = 2, 
           READ_CRLFCR = 3, 
           DONE = 4;
const byte YALER_RECEIVING = 0, 
           YALER_UPGRADING = 1, 
           YALER_TIMEOUT = 2, 
           YALER_UPGRADED = 3;
const char yalerId[] = "gsiot-vw58-1khs"; // TODO: change
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xFA }; // TODO: change
byte server[] = { 46, 137, 106, 125 }; // Oberon Yaler, TODO: change
boolean isPut;
byte state;
int count;
byte ledIndex;
byte ledUriOffset;
int htmlLength;
byte yalerState;
int yalerCount;
byte rPin = 6;
byte gPin = 5;
byte bPin = 3;
byte led[] = {0, 
              0, 
              0};

void setColor (byte r, byte g, byte b) 
{
  // SparkFun LED
  analogWrite(rPin, r);
  analogWrite(gPin, g); 
  analogWrite(bPin, b);

  // Ladyada LED
  //analogWrite(rPin, 255 - r);
  //analogWrite(gPin, 255 - g); 
  //analogWrite(bPin, 255 - b);  
}

byte byteFromHexChar (char ch) 
{
  byte result;
  if ((ch >= '0') && (ch <= '9')) 
  {
    result = ch - '0';
  } 
  else if ((ch >= 'a') && (ch <= 'f')) 
  {
    result = 10 + (ch - 'a');
  } 
  else if ((ch >= 'A') && (ch <= 'F')) 
  {
    result = 10 + (ch - 'A');
  } 
  else 
  {
    result = 0;
  }
  return result;
}

void parseRequestChar (char ch) 
{
  // PUT /<yaler-id>/led/ff0000 ... \r\n\r\n
  // GET /<yaler-id>/led ... \r\n\r\n
  if (count == 0) {
    isPut = ch == 'P';
  } 
  else if ((count >= ledUriOffset) && (count < ledUriOffset + 6)) 
  {
    byte d = byteFromHexChar(ch);
    //Serial.print(ch);
    if ((count - ledUriOffset) % 2 == 0) 
    {
      led[ledIndex] = d;
    } 
    else 
    {
      led[ledIndex] = led[ledIndex] * 16 + d;
      ledIndex++;
    }
  }
  if (state == RECEIVING) 
  {
    if (ch == '\r') 
    {
      state = READ_CR;
    }
  } 
  else if (state == READ_CR) 
  {
    // assert ch == '\n'
    state = READ_CRLF;
  } 
  else if (state == READ_CRLF) 
  {
    if (ch == '\r') {
      state = READ_CRLFCR;
    } 
    else 
    {
      state = RECEIVING;
    }
  } 
  else if (state == READ_CRLFCR) 
  {
    // assert ch == '\n'
    state = DONE;
  }
  count++;
}

void parseYalerResponseChar (char ch) 
{
  // HTTP/1.1 101 ... \r\n\r\n
  // HTTP/1.1 204 ... \r\n\r\n
  if (yalerState == YALER_RECEIVING) 
  {
    if (yalerCount == 9) 
    { // sizeof("HTTP/1.1 ?") - 1;
      if (ch == '1') { // 101
        yalerState = YALER_UPGRADING;
      } 
      else 
      { // 204
        // assert ch == '2'
        yalerState = YALER_TIMEOUT;
      }
    }
  } else if (yalerState == YALER_UPGRADING) 
  {
    if (yalerCount == 75) 
    { // sizeof("HTTP/1.1 101 ... \r\n\r\n") - 1
      yalerState = YALER_UPGRADED;
    }
  }
  yalerCount++;
}

void sendYalerPostRequest (EthernetClient c) 
{
  c.print("POST /");
  c.print(yalerId);
  c.println(" HTTP/1.1");
  c.println("Upgrade: PTTH/1.0");
  c.println("Connection: Upgrade");
  c.println("Host: www.yaler.net");
  c.print(contentLength);
  c.println("0");
  c.println();
  c.flush();
}

void receiveYalerResponse (EthernetClient c) 
{
  yalerCount = 0;
  yalerState = YALER_RECEIVING;
  while (c.connected() && (c.available() <= 0)) {} // Yaler sends 101 or 204 in < 30s
  while (c.connected() && (c.available() > 0) &&
    (yalerState != YALER_UPGRADED) &&
    (yalerState != YALER_TIMEOUT)) 
  {
    char ch = c.read();
    parseYalerResponseChar(ch);
  }
}

void sendPutResponse (EthernetClient c) 
{
  c.println(http200);
  c.println(contentTypeTextPlain);
  c.print(contentLength);
  c.println("3");
  c.println(connectionClose);
  c.println();
  c.print("200");
  c.flush();
}

void sendGetResponse (EthernetClient c) 
{
  c.println(http200);
  c.println(contentTypeTextHtml);
  c.print(contentLength);
  c.println(htmlLength);
  c.println(connectionClose);
  c.println();
  c.print(html);
  c.flush();
}

void receiveRequest (EthernetClient c) 
{
  
  count = 0;
  ledIndex = 0;
  state = RECEIVING;
  while (c.connected() && (c.available() > 0) && (state != DONE)) 
  {
    char ch = c.read();
    //Serial.print(ch);
    parseRequestChar(ch);
  }
}

void setup() 
{
  //Serial.begin(9600);
  //Serial.println("setup");
  pinMode(rPin, OUTPUT);
  pinMode(gPin, OUTPUT);
  pinMode(bPin, OUTPUT);
  setColor(255, 255, 255);
  Ethernet.begin(mac);
  htmlLength = sizeof(html) - 1;
  ledUriOffset = sizeof("PUT /") + 
                 sizeof(yalerId) + 
                 sizeof("/led/") - 3 * 1;
  setColor(0, 0, 0);
}

void loop() 
{
  EthernetClient client;
  client.connect(server, 80);
  Ethernet.maintain();
  if (client.connected()) 
  {
    //Serial.println("connected");
    sendYalerPostRequest(client);
    receiveYalerResponse(client);
    if (yalerState == YALER_UPGRADED) 
    {
      //Serial.println("upgraded");
      receiveRequest(client);
      if (state == DONE) 
      {
        if (isPut) 
        {
          setColor(led[0], led[1], led[2]);
          sendPutResponse(client);
        } 
        else 
        {
          sendGetResponse(client);
        }
      }
    } 
    else 
    {
      //Serial.println("timeout");
    }
    client.stop();
  }
}

i just tried to compile the ipaddress printer sketch and it flagged a line saying

DhcpAddressPrinter.cpp: In function 'void loop()':
DhcpAddressPrinter:57: error: 'class EthernetClass' has no member named 'maintain'

void loop() 
{  
  int status = Ethernet.maintain();
  /*
    returns:
    0: nothing happened
    1: renew failed
    2: renew success
    3: rebind fail
    4: rebind success
    */
  if(status)
  {
    long now = millis();
    Serial.print(now , DEC);
    Serial.print(" a maintain event occured after (sec):");
    Serial.println((now - lastTime)/1000, DEC);
    Serial.print("Status:");
    Serial.println(status, DEC);
    Serial.println("IP address after renew/rebind:");
    print_info();
    lastTime = now;
  }
  //simulate a somewhat huge cycle time
  delay(18400);
}

Have you tried the DhcpAddressPrinter sketch in the ethernet examples? Compile and upload, then open the serial monitor. If you get an ip, then check your router to see if that is expired.

see post above. i cant even get the address printer to compile right now.

There is no "Ethernet.maintain()" in the DhcpAddressPrinter sketch.

thats what is in the one i have that i opened. i am freakishly lost at this point and can not find a replacement library.

What version of the IDE are you using? Here is the lease from my router after running DhcpAddressPrinter. I lease for 2 days. It shows 1d23h51m52s left. That looks about right.

1 D address=192.168.2.249 mac-address=00:AA:BB:CC:DE:02 client-id="1:0:aa:bb:cc:de:2" server=dhcp3 status=bound
expires-after=1d23h51m52s last-seen=8m8s active-address=192.168.2.249 active-mac-address=00:AA:BB:CC:DE:02
active-client-id="1:0:aa:bb:cc:de:2" active-server=dhcp3 host-name="WIZnetCCDE02"

I am talking about the examples under ethernet.

yes the code i copied is straight from the ethernet address printer example sketch. the one that would not compile.

downloaded arduino software again replaced the ethernet library and i am going to see if i can get the example printer to work.

I do not know what version of the IDE you are using, but it isn't v1.0. Here is the sketch code from the examples.

/*
  DHCP-based IP printer
 
 This sketch uses the DHCP extensions to the Ethernet library
 to get an IP address via DHCP and print the address obtained.
 using an Arduino Wiznet Ethernet shield. 
 
 Circuit:
 * Ethernet shield attached to pins 10, 11, 12, 13
 
 created 12 April 2011
 by Tom Igoe
 
 */

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };

// Initialize the Ethernet client library
// with the IP address and port of the server 
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup() {
  // start the serial library:
  Serial.begin(9600);
  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    for(;;)
      ;
  }
  // print your local IP address:
  Serial.print("My IP address: ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    Serial.print("."); 
  }
  Serial.println();
}

void loop() {

}

thats what i have now. going to see if it works now.

It may take a minute or more to time out if there is no dhcp server on the localnet.

If it doesn't return with either an error or an ip after about 3 minutes, then you might need to update your IDE to v1.0.1. I don't have the link to it handy, but if you search for that, you should find it. That is the actual version I am on now.

ethernet address printer works and i get my local address from the net work

has to be this guys code. i finally got it to display the webpage and the function of it doesnt work. off to learn how to host a website on the arduino i guess. thanks everyone.