DhcpAddressPrinter.ino 1.4 KB

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