EthernetShield.ino 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. * Note: to use an ethernet shield, You must also set #define EZTIME_ETHERNET in $sketch_dir/libraries/ezTime/src/ezTime.h
  3. *
  4. * Also note that all ezTime examples can be used with an Ethernet shield if you just replace the beginning of the sketch
  5. * with the beginning of this one.
  6. */
  7. #include <ezTime.h>
  8. #include <Ethernet.h>
  9. // Enter a MAC address for your controller below. (Or use address below, just make sure it's unique on your network)
  10. // Newer Ethernet shields have a MAC address printed on a sticker on the shield
  11. #define MAC_ADDRESS { 0xBA, 0xDB, 0xAD, 0xC0, 0xFF, 0xEE }
  12. void setup() {
  13. // Open serial communications and wait for port to open:
  14. Serial.begin(115200);
  15. while (!Serial) { ; } // wait for serial port to connect. Needed for native USB port only
  16. Serial.println();
  17. // You can use Ethernet.init(pin) to configure the CS pin
  18. //Ethernet.init(10); // Most Arduino shields (default if unspecified)
  19. //Ethernet.init(5); // MKR ETH shield
  20. //Ethernet.init(0); // Teensy 2.0
  21. //Ethernet.init(20); // Teensy++ 2.0
  22. //Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
  23. //Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
  24. Serial.print(F("Ethernet connection ... "));
  25. byte mac [] = MAC_ADDRESS;
  26. if (Ethernet.begin(mac) == 0) {
  27. Serial.println(F("failed. (Reset to retry.)"));
  28. while (true) { ; }; // Hang
  29. } else {
  30. Serial.print(F("got DHCP IP: "));
  31. Serial.println(Ethernet.localIP());
  32. }
  33. // give the Ethernet shield a second to initialize:
  34. delay(1000);
  35. // OK, we're online... So the part above here is what you swap in before the waitForSync() in the other examples...
  36. // Wait for ezTime to get its time synchronized
  37. waitForSync();
  38. Serial.println();
  39. Serial.println("UTC: " + UTC.dateTime());
  40. Timezone myTZ;
  41. // Provide official timezone names
  42. // https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
  43. myTZ.setLocation(F("Pacific/Auckland"));
  44. Serial.print(F("New Zealand: "));
  45. Serial.println(myTZ.dateTime());
  46. // Wait a little bit to not trigger DDoS protection on server
  47. // See https://github.com/ropg/ezTime#timezonedropnl
  48. delay(5000);
  49. // Or country codes for countries that do not span multiple timezones
  50. myTZ.setLocation(F("de"));
  51. Serial.print(F("Germany: "));
  52. Serial.println(myTZ.dateTime());
  53. // Same as above
  54. delay(5000);
  55. // See if local time can be obtained (does not work in countries that span multiple timezones)
  56. Serial.print(F("Local (GeoIP): "));
  57. if (myTZ.setLocation()) {
  58. Serial.println(myTZ.dateTime());
  59. } else {
  60. Serial.println(errorString());
  61. }
  62. Serial.println();
  63. Serial.println(F("Now ezTime will show an NTP sync every 60 seconds"));
  64. // Set NTP polling interval to 60 seconds. Way too often, but good for demonstration purposes.
  65. setInterval(60);
  66. // Make ezTime show us what it is doing
  67. setDebug(INFO);
  68. }
  69. void loop() {
  70. events();
  71. }