-1

I'm testing the Ethernet shield with an Arduino Uno, and I'm getting a DHCP error just using the example sketch.

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

byte MACaddress[] = { 0x90, 0xAD, 0xDA, 0x0D, 0x96, 0xFE };

EthernetClient client;

void setup() {
    Serial.begin(9600);
    while (!Serial) {
        ;
    }

    // Start the Ethernet connection:
    if (Ethernet.begin(MACaddress) == 0) {
        Serial.println("Failed to configure Ethernet using DHCP");
        for(;;)
            ;
    }
    Serial.print("My IP address: ");
    for (byte thisByte = 0; thisByte < 4; thisByte++) {
        Serial.print(Ethernet.localIP()[thisByte], DEC);
        Serial.print(".");
    }
    Serial.println();
}

void loop() {
}

I've opened the router administration page, and I can see it gave the Arduino an IP address, associated with the MAC address. I've also tried a static IP address in the code (Ethernet.begin(MACaddress, IPaddress)), but it won't work either.

I can't ping the shield IP address that shows in the router administrator page.

What is wrong with just this simple code?

Everything is out of the box, the Arduino and the shield. I haven't done anything with them, just connected the shield to the Arduino and sent the code. It seems everything is working fine, the LEDs are blinking for both boards.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
andrepcg
  • 1,301
  • 7
  • 26
  • 45
  • There are many Ethernet shields, edit the question to include the model. The sample code is meant for one type and may not be compatible. People cannot help in that respect if they do not know what the hardware. – jdr5ca Jan 19 '13 at 01:46

2 Answers2

0

These loops are useless.. Could you try something like:

#if defined(ARDUINO) && ARDUINO > 18
#include <SPI.h>
#endif
#include <Ethernet.h>
#include <EthernetDHCP.h>

// MAC Address
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

const char* ip_to_str(const uint8_t*);

// Initialize the Ethernet server library
Server server(8080);

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

  Serial.println("Attempting to obtain a DHCP lease...");

  // Initiate a DHCP session. The argument is the MAC (hardware) address that
  // you want your Ethernet shield to use. This call will block until a DHCP
  // lease has been obtained. The request will be periodically resent until
  // a lease is granted, but if there is no DHCP server on the network or if
  // the server fails to respond, this call will block forever.
  // Thus, you can alternatively use polling mode to check whether a DHCP
  // lease has been obtained, so that you can react if the server does not
  // respond (see the PollingDHCP example).
  EthernetDHCP.begin(mac);

  // Since we're here, it means that we now have a DHCP lease, so we print
  // out some information.
  const byte* ipAddr = EthernetDHCP.ipAddress();
  const byte* gatewayAddr = EthernetDHCP.gatewayIpAddress();
  const byte* dnsAddr = EthernetDHCP.dnsIpAddress();

  Serial.println("A DHCP lease has been obtained.");

  Serial.print("My IP address is ");
  Serial.println(ip_to_str(ipAddr));

  Serial.print("Gateway IP address is ");
  Serial.println(ip_to_str(gatewayAddr));

  Serial.print("DNS IP address is ");
  Serial.println(ip_to_str(dnsAddr));

  // Start the server
   server.begin();
}

void loop()
{
  // You should periodically call this method in your loop(): It will allow
  // the DHCP library to maintain your DHCP lease, which means that it will
  // periodically renew the lease and rebind if the lease cannot be renewed.
  // Thus, unless you call this somewhere in your loop, your DHCP lease might
  // expire, which you probably do not want :-)
  EthernetDHCP.maintain();

  // listen for incoming clients
  Client client = server.available();
  if (client) {
    // an http request ends with a blank line
    boolean currentLineIsBlank = true;
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        // if you've gotten to the end of the line (received a newline
        // character) and the line is blank, the http request has ended,
        // so you can send a reply
        if (c == '\n' && currentLineIsBlank) {
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();

          // Some misc. HTML 
          client.println("<title>Arduino Control Panel</title>");
          client.println("<center><h1>Control Panel</h1></center>");
          client.println("<p></p>");

          // 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("<br />");
          }
          break;
        }
        if (c == '\n') {
          // you're starting a new line
          currentLineIsBlank = true;
        } 
        else if (c != '\r') {
          // you've gotten a character on the current line
          currentLineIsBlank = false;
        }
      }
    }
    // give the web browser time to receive the data
    delay(1);
    // close the connection:
    client.stop();
  }
}

// Just a utility function to nicely format an IP address.
const char* ip_to_str(const uint8_t* ipAddr)
{
  static char buf[16];
  sprintf(buf, "%d.%d.%d.%d\0", ipAddr[0], ipAddr[1], ipAddr[2], ipAddr[3]);
  return buf;
}
Stanimir Stoyanov
  • 1,623
  • 18
  • 29
0

I'm not sure what you mean by "I've also tried a static IP address in the code". If you simply replaced if (Ethernet.begin(MACaddress) == 0) {

with if (Ethernet.begin(MACaddress, myIP) == 0) {

the result may be unpredictable, because there is no return value.

Read

EthernetBegin

Returns The DHCP version of this function, Ethernet.begin(mac), returns an int: 1 on a successful DHCP connection, 0 on failure. The other versions don't return anything.

have you tried one of the examples with fixed IP's?

atmelino
  • 2,887
  • 2
  • 20
  • 15