1 | /*
|
---|
2 | DHCP-based IP printer
|
---|
3 |
|
---|
4 | This sketch uses the DHCP extensions to the Ethernet library
|
---|
5 | to get an IP address via DHCP and print the address obtained.
|
---|
6 | using an Arduino Wiznet Ethernet shield.
|
---|
7 |
|
---|
8 | Circuit:
|
---|
9 | * Ethernet shield attached to pins 10, 11, 12, 13
|
---|
10 |
|
---|
11 | created 12 April 2011
|
---|
12 | modified 9 Apr 2012
|
---|
13 | by Tom Igoe
|
---|
14 |
|
---|
15 | */
|
---|
16 |
|
---|
17 | #include <SPI.h>
|
---|
18 | #include <Ethernet2.h>
|
---|
19 |
|
---|
20 | // Enter a MAC address for your controller below.
|
---|
21 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield
|
---|
22 | byte mac[] = {
|
---|
23 | 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
|
---|
24 | };
|
---|
25 |
|
---|
26 | // Initialize the Ethernet client library
|
---|
27 | // with the IP address and port of the server
|
---|
28 | // that you want to connect to (port 80 is default for HTTP):
|
---|
29 | EthernetClient client;
|
---|
30 |
|
---|
31 | void setup() {
|
---|
32 | // Open serial communications and wait for port to open:
|
---|
33 | Serial.begin(9600);
|
---|
34 | // this check is only needed on the Leonardo:
|
---|
35 | while (!Serial) {
|
---|
36 | ; // wait for serial port to connect. Needed for Leonardo only
|
---|
37 | }
|
---|
38 |
|
---|
39 | // start the Ethernet connection:
|
---|
40 | if (Ethernet.begin(mac) == 0) {
|
---|
41 | Serial.println("Failed to configure Ethernet using DHCP");
|
---|
42 | // no point in carrying on, so do nothing forevermore:
|
---|
43 | for (;;)
|
---|
44 | ;
|
---|
45 | }
|
---|
46 | // print your local IP address:
|
---|
47 | Serial.print("My IP address: ");
|
---|
48 | for (byte thisByte = 0; thisByte < 4; thisByte++) {
|
---|
49 | // print the value of each byte of the IP address:
|
---|
50 | Serial.print(Ethernet.localIP()[thisByte], DEC);
|
---|
51 | Serial.print(".");
|
---|
52 | }
|
---|
53 | Serial.println();
|
---|
54 | }
|
---|
55 |
|
---|
56 | void loop() {
|
---|
57 |
|
---|
58 | }
|
---|
59 |
|
---|
60 |
|
---|