Ethernet shield DHCP/Static IP error

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

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

    byte mac[] = { 0x90, 0xAD, 0xDA, 0x0D, 0x96, 0xFE };
    
    EthernetClient client;
    
    void setup() {
      Serial.begin(9600);
       while (!Serial) {
        ;
      }
    
      // start the Ethernet connection:
      if (Ethernet.begin(mac) == 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 admin and I can see it gave the arduino an IP address (192.168.1.101), associated with the MAC address. I've also tried static IP in the code (Ethernet.begin(mac,ip)) but it won't work as well.

I can't ping the shield IP. 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. Using arduino IDE 1.0.3

I'm getting the "Failed to configure Ethernet using DHCP"

I guess the problem is not the router, because it always worked fine, I could just plug the cable and some other pc get's internet access, no further configuration needed.

Code looks ok.

Where did that MAC address come from? Could there be a conflict on the network?

You won't get a ping response from the arduino unless you put code on it to respond to ICMP Ping packets.

-br

there is no conflict with IP's and MAC addresses.

The MAC address in the code came from the Shield sticker in the back of the board

I removed the unneeded serial port check and the below code works with my netgear 614 router.

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

    byte mac[] = { 0x90, 0xAD, 0xDA, 0x0D, 0x96, 0xFE };
    
    EthernetClient client;
    
    void setup() {
      Serial.begin(9600);
    
      // start the Ethernet connection:
      if (Ethernet.begin(mac) == 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() {
    
    }

Yep, I removed that serial check and it's still not working.

What could be the issue here?

Do you have a microSD card in the slot?

I use this to test the SPI connection. If the serial monitor shows 192.168.2.2, then it is ok. If it shows anything else, like 0.0.0.0, then it failed.

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

byte mac[] = {  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,2);

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

  // disable SD card if one in the slot
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  Serial.println("Starting w5100");
  Ethernet.begin(mac,ip);

  Serial.println(Ethernet.localIP());
}

void loop() {
}

i did that and it worked! now i'm trying to connect to a webserver, using this tutorial http://bildr.org/2011/06/arduino-ethernet-client/ but I'm getting connection error..

EDIT: defining pin 4 as output and set it to HIGH made it work on the previous example but if I try to do with DHCP and aplying HIGH to pin 4 it will show "DHCP failed"

Mine works with zoomkat's dhcp code if I remove the SD card, or disable the SD SPI. I use a Mikrotik RB433AH for the gateway/dhcp server.

maybe there's something wrong it my configuration somehow.. i've tested with a lot of different code I found on the internet, including the ones in this post but I can't manage to get this working.

my router is a Thomson TG784n, maybe there's something off but I don't know what... If you guys know something please tell me. Thanks!

Try another router. Works with Cisco stuff too. I checked it with my ISP's router. Got an ip and was pingable.

SurferTim:
Try another router. Works with Cisco stuff too. I checked it with my ISP's router. Got an ip and was pingable.

hie, sorry i'm new to the forum and arduino as well, i'm trying to connect my arduino ethernet shield as a web client using DHCP but i dont have a router, is it possible for me to connect it to my pc via ethernet cable. i've tried the DHCP example from Ethernet - Arduino Reference but i keep getting the error : Failed to configure Ethernet using DHCP. i'm using arduino uno r3 and a w5100 ethernet shield and arduino 1.0.4 IDE, the LEDs flash if i push the reset button. i would greatly appreciate your help or suggestions

i dont have a router, is it possible for me to connect it to my pc via ethernet cable.

If you are using a windows machine, you will probably need to use something like internet connection sharing (ICS).

thank you for the response :smiley:
i had to restart my machine, probably to reset the ethernet
and i used this code to test my connection and Woohoo! it worked with my static ip
here's the code i used to test

//zoomkat 12-08-11
//simple client test
//for use with IDE 1.0
//open serial monitor and send an e to test
//for use with W5100 based ethernet shields
//note that the below bug fix may be required
// Google Code Archive - Long-term storage for Google Code Project Hosting.

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

byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x3E, 0x48 }; //physical mac address
byte ip[] = { 192,168,137, 170 }; // ip in lan assigned to arduino
//byte gateway[] = { 192, 168, 0, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
byte myserver[] = { 208, 104, 2, 86 }; // zoomkat web page server IP address
EthernetClient client;
//////////////////////

void setup(){

Ethernet.begin(mac, ip);
//Ethernet.begin(mac, ip, subnet, gateway);
Serial.begin(9600);
Serial.println("Better client test 12/01/11"); // so I can keep track of what is loaded
Serial.println("Send an e in serial monitor to test"); // what to do to test
}

void loop(){
// check for serial input
if (Serial.available() > 0) //if something in serial buffer
{
byte inChar; // sets inChar as a byte
inChar = Serial.read(); //gets byte from buffer
if(inChar == 'e') // checks to see byte is an e
{
sendGET(); // call sendGET function below when byte is an e
}
}
}

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

void sendGET() //client function to send/receive GET request data.
{
if (client.connect(myserver, 80)) { //starts client connection, checks for connection
Serial.println("connected");
client.println("GET /~shb/arduino.txt HTTP/1.0"); //download text
client.println(); //end of get request
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}

while(client.connected() && !client.available()) delay(1); //waits for data
while (client.connected() || client.available()) { //connected or data available
char c = client.read(); //gets byte from ethernet buffer
Serial.print(c); //prints byte to serial monitor
}

Serial.println();
Serial.println("disconnecting.");
Serial.println("==================");
Serial.println();
client.stop(); //stop client

}

thank you once again XD
i'd like to know how i can use it to connect it to my local wampserver with mysql so that i can store a cellnumber and serial number remotely.
I'm going to hook up a 4x3 keypad, 20x4 lcd in 4 bit mode, ethernet shield and the arduino uno r3.
any suggestions will be greatly appreciated

Can we please get back to the original topic about andrepcg's problem? I have the same setup: Aduino Uno + ethernet shield + LCD keypad shield. There is no SD card inserted in the ethernet shield.

If I remove the LCD shield DHCP works just fine. I can even ping my setup without any additional code to handle ICMP ping packets. However, if I add the LCD shield back onto the stack DHCP works unreliably. I print the ethernet board's IP address to the LCD screen and I get different addresses everytime or none at all.

Btw, my network setup includes a Linux server acting as a DHCP server where I'm actually assigning a specific IP address to a request from a specific MAC address. In other words, it is like having a static IP address but one that I can change simply by editing my dhcp.conf file on the server. I don't have to go to each device to set its IP address.

I suspect a conflict in the pins used by the lcd (8, 9, 4, 5, 6, & 7 in the LCD sample code I'm using) and the pins used by the ethernet shield. What pins are used by the ethernet shield? Is it possible to change the pins each of the shields uses to avoid conflicts?

TerryB:
Can we please get back to the original topic about andrepcg's problem? I have the same setup: Aduino Uno + ethernet shield + LCD keypad shield. There is no SD card inserted in the ethernet shield.

If I remove the LCD shield DHCP works just fine. I can even ping my setup without any additional code to handle ICMP ping packets. However, if I add the LCD shield back onto the stack DHCP works unreliably. I print the ethernet board's IP address to the LCD screen and I get different addresses everytime or none at all.

Then this is not the same problem and topic as the OP. That part was solved, as it appears it is with your's. Now you have a LCD conflict problem. I recommend starting a new thread with the following info:
Your sketch code.
What LCD shield are you using?
How are you powering all this? The ethernet shield takes a bit of power on its own.

My bad. I realized too late that I was reading two different threads and had gotten confused. Sorry about that.

i just wanted to know from anyone here, are you guys uploading the code to the board via the USB cable or the ethernet cable here? As in which port are you using to upload the code? the serial port? or the network port? Thanks in advance

This is a 3 year old thread, but I use the USB to upload sketches to my Mega.