Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <Ethernet.h>
  3. // Enter a MAC address for your controller below.
  4. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  5. byte mac[] = {
  6.   0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
  7. };
  8. // Initialize the Ethernet client library
  9. // with the IP address and port of the server
  10. // that you want to connect to (port 80 is default for HTTP):
  11. EthernetClient client;
  12. void setup() {
  13.   // Open serial communications and wait for port to open:
  14.   Serial.begin(9600);
  15.   // this check is only needed on the Leonardo:
  16.   while (!Serial) {
  17.     ; // wait for serial port to connect. Needed for native USB port only
  18.   }
  19.   // start the Ethernet connection:
  20.   if (Ethernet.begin(mac) == 0) {
  21.     Serial.println("Failed to configure Ethernet using DHCP");
  22.     // no point in carrying on, so do nothing forevermore:
  23.     for (;;)
  24.       ;
  25.   }
  26.   // print your local IP address:
  27.   printIPAddress();
  28. }
  29. void loop() {
  30.   switch (Ethernet.maintain())
  31.   {
  32.     case 1:
  33.       //renewed fail
  34.       Serial.println("Error: renewed fail");
  35.       break;
  36.     case 2:
  37.       //renewed success
  38.       Serial.println("Renewed success");
  39.       //print your local IP address:
  40.       printIPAddress();
  41.       break;
  42.     case 3:
  43.       //rebind fail
  44.       Serial.println("Error: rebind fail");
  45.       break;
  46.     case 4:
  47.       //rebind success
  48.       Serial.println("Rebind success");
  49.       //print your local IP address:
  50.       printIPAddress();
  51.       break;
  52.     default:
  53.       //nothing happened
  54.       break;
  55.   }
  56. }
  57. void printIPAddress()
  58. {
  59.   Serial.print("My IP address: ");
  60.   for (byte thisByte = 0; thisByte < 4; thisByte++) {
  61.     // print the value of each byte of the IP address:
  62.     Serial.print(Ethernet.localIP()[thisByte], DEC);
  63.     Serial.print(".");
  64.   }
  65.   Serial.println();
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement