Ethernet Shield DHCP Address Printer
Get an IP address via DHCP and print it out.
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 Ethernet shield.
DHCP is used to assign an IP address when Ethernet.begin(mac) is called. Using DHCP significantly increases the size of a sketch. Using the localIP() function, the assigned IP address is sent out via the Serial Monitor.
Hardware Required
Arduino Board
Circuit
The Ethernet shield allows you to connect a WIZNet Ethernet controller to the Arduino boards via the SPI bus. It uses the ICSP header pins and pin 10 as chip select for the SPI connection to the Ethernet controller chip. Later models of the Ethernet shield also have an SD Card on board. Digital pin 4 is used to control the chip select pin on the SD card.
The shield should be connected to a network with an Ethernet cable. You will need to change the network settings in the program to correspond to your network.
Image developed using Fritzing. For more circuit examples, see the Fritzing project page
In the above image, the Arduino board would be stacked below the Ethernet shield.
Code
1/*2
3 DHCP-based IP printer4
5 This sketch uses the DHCP extensions to the Ethernet library6
7 to get an IP address via DHCP and print the address obtained.8
9 using an Arduino Wiznet Ethernet shield.10
11 Circuit:12
13 Ethernet shield attached to pins 10, 11, 12, 1314
15 created 12 April 201116
17 modified 9 Apr 201218
19 by Tom Igoe20
21 modified 02 Sept 201522
23 by Arturo Guadalupi24
25 */26
27#include <SPI.h>28#include <Ethernet.h>29
30// Enter a MAC address for your controller below.31// Newer Ethernet shields have a MAC address printed on a sticker on the shield32byte mac[] = {33
34 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x0235};36
37void setup() {38
39 // You can use Ethernet.init(pin) to configure the CS pin40
41 //Ethernet.init(10); // Most Arduino shields42
43 //Ethernet.init(5); // MKR ETH shield44
45 //Ethernet.init(0); // Teensy 2.046
47 //Ethernet.init(20); // Teensy++ 2.048
49 //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet50
51 //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet52
53 // Open serial communications and wait for port to open:54
55 Serial.begin(9600);56
57 while (!Serial) {58
59 ; // wait for serial port to connect. Needed for native USB port only60
61 }62
63 // start the Ethernet connection:64
65 Serial.println("Initialize Ethernet with DHCP:");66
67 if (Ethernet.begin(mac) == 0) {68
69 Serial.println("Failed to configure Ethernet using DHCP");70
71 if (Ethernet.hardwareStatus() == EthernetNoHardware) {72
73 Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");74
75 } else if (Ethernet.linkStatus() == LinkOFF) {76
77 Serial.println("Ethernet cable is not connected.");78
79 }80
81 // no point in carrying on, so do nothing forevermore:82
83 while (true) {84
85 delay(1);86
87 }88
89 }90
91 // print your local IP address:92
93 Serial.print("My IP address: ");94
95 Serial.println(Ethernet.localIP());96}97
98void loop() {99
100 switch (Ethernet.maintain()) {101
102 case 1:103
104 //renewed fail105
106 Serial.println("Error: renewed fail");107
108 break;109
110 case 2:111
112 //renewed success113
114 Serial.println("Renewed success");115
116 //print your local IP address:117
118 Serial.print("My IP address: ");119
120 Serial.println(Ethernet.localIP());121
122 break;123
124 case 3:125
126 //rebind fail127
128 Serial.println("Error: rebind fail");129
130 break;131
132 case 4:133
134 //rebind success135
136 Serial.println("Rebind success");137
138 //print your local IP address:139
140 Serial.print("My IP address: ");141
142 Serial.println(Ethernet.localIP());143
144 break;145
146 default:147
148 //nothing happened149
150 break;151
152 }153}
Last revision 2018/09/07 by SM
Suggest changes
The content on docs.arduino.cc is facilitated through a public GitHub repository. If you see anything wrong, you can edit this page here.
License
The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.