Ethernet Shield + DHCP

Dear All,

Anyone tried the Ethernet shield connecting using DHCP? Got the code from → software → arduino ethernet → dhcp but cannot comply with my Uno R3 and Ethernet R3. Dont know whether its because of version problem. Run the code from the example

//  Copyright (C) 2010 Georg Kaindl
//  This file is part of Arduino EthernetDHCP.
//  EthernetDHCP is free software: you can redistribute it and/or modify
//  it under the terms of the GNU Lesser General Public License as
//  published by the Free Software Foundation, either version 3 of
//  the License, or (at your option) any later version.
//  EthernetDHCP is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  GNU Lesser General Public License for more details.
//  You should have received a copy of the GNU Lesser General Public
//  License along with EthernetDHCP. If not, see
//  <>.

//  Illustrates how to use EthernetDHCP in polling (non-blocking)
//  mode.

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

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

const char* ip_to_str(const uint8_t*);

void setup()
  // Initiate a DHCP session. The argument is the MAC (hardware) address that
  // you want your Ethernet shield to use. The second argument enables polling
  // mode, which means that this call will not block like in the
  // SynchronousDHCP example, but will return immediately.
  // Within your loop(), you can then poll the DHCP library for its status,
  // finding out its state, so that you can tell when a lease has been
  // obtained. You can even find out when the library is in the process of
  // renewing your lease.
  EthernetDHCP.begin(mac, 1);

void loop()
  static DhcpState prevState = DhcpStateNone;
  static unsigned long prevTime = 0;
  // poll() queries the DHCP library for its current state (all possible values
  // are shown in the switch statement below). This way, you can find out if a
  // lease has been obtained or is in the process of being renewed, without
  // blocking your sketch. Therefore, you could display an error message or
  // something if a lease cannot be obtained within reasonable time.
  // Also, poll() will actually run the DHCP module, just like maintain(), so
  // you should call either of these two methods at least once within your
  // loop() section, or you risk losing your DHCP lease when it expires!
  DhcpState state = EthernetDHCP.poll();

  if (prevState != state) {

    switch (state) {
      case DhcpStateDiscovering:
        Serial.print("Discovering servers.");
      case DhcpStateRequesting:
        Serial.print("Requesting lease.");
      case DhcpStateRenewing:
        Serial.print("Renewing lease.");
      case DhcpStateLeased: {
        Serial.println("Obtained lease!");

        // 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.print("My IP address is ");

        Serial.print("Gateway IP address is ");

        Serial.print("DNS IP address is ");

  } else if (state != DhcpStateLeased && millis() - prevTime > 300) {
     prevTime = millis();

  prevState = state;

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

Its says that the wiring.h in the EthernetDHCP.cpp is missing. Which I dont know where to get one. Anyone had try this code before or had working DHCP code to share?

Thanks a lot

This works for me with w5100 and IDE v1.0.1 or later.

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

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

void setup() {

  // disable SD SPI if one in the slot

  Serial.print("Starting ethernet...");
  if(!Ethernet.begin(mac)) Serial.println("failed");
  else Serial.println(Ethernet.localIP());

void loop() {

DHCP is supported by core libraries in 1.0+. Get rid of the pre-1.0 library you downloaded.

Hi Pauls,

Are you referring to the Lib folder that is in my 1.0.1? As attached thats all my folders

Anyone tried the Ethernet shield connecting using DHCP?

Yes, and it works good! :slight_smile:

Are you referring to the Lib folder that is in my 1.0.1? As attached thats all my folders

No. In your initial post, you said you downloaded a library that was not needed. You need to find where you put it, and get rid of it.