Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.08 KB | None | 0 0
  1. #include <Ethernet.h>
  2. #include <SPI.h>
  3. #include <EthernetUdp.h>
  4. #include <Time.h>
  5.  
  6. //IDE Version 1.0.5
  7. //Arduino Uno R3
  8. //Arduino Ethernet Shield R3 (W5100)
  9. //No SD card in card slot
  10. //
  11. //Edit this section for your network setup
  12. //---------------------------------------------
  13. byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Use the MAC for your shield
  14. IPAddress ip(192,168,0,177);
  15. IPAddress gateway(192,168,0,1);
  16. IPAddress subnet(255,255,255,0);
  17. IPAddress mydns(192,168,0,1);
  18. char timeServer[] = "0.north-america.pool.ntp.org"; //Time server you wish to use
  19. //Time Zone Selection
  20. //const int TZ_OFFSET = 4*3600; //AST UTC-4
  21. const int TZ_OFFSET = 5*3600; //EST UTC-5
  22. //const int TZ_OFFSET = 6*3600; //CST UTC-6
  23. //const int TZ_OFFSET = 7*3600; //MST UTC-7
  24. //const int TZ_OFFSET = 8*3600; //PST UTC-8
  25. //const int TZ_OFFSET = 9*3600; //AKST UTC-9
  26. //const int TZ_OFFSET = 10*3600; //HST UTC-10
  27. //---------------------------------------------
  28. //
  29. const int NTP_PACKET_SIZE = 48; //NTP time stamp is in the first 48 bytes of the message
  30. byte packetBuffer[NTP_PACKET_SIZE]; //Buffer to hold incoming and outgoing packets
  31. time_t prevDisplay;
  32. EthernetUDP Udp;
  33. //
  34. unsigned long requestNTP()
  35. {
  36. //Send NTP packet
  37. //Set all bytes in the buffer to 0
  38. memset(packetBuffer, 0, NTP_PACKET_SIZE);
  39. //Init values needed to form NTP request
  40. packetBuffer[0] = 0b11100011; //LI, Version, Mode
  41. packetBuffer[1] = 0; //Stratum, or type of clock
  42. packetBuffer[2] = 6; //Polling Interval
  43. packetBuffer[3] = 0xEC; //Peer Clock Precision
  44. //Skip 8 bytes and leave zeros for Root Delay & Root Dispersion
  45. packetBuffer[12] = 49;
  46. packetBuffer[13] = 0x4E;
  47. packetBuffer[14] = 49;
  48. packetBuffer[15] = 52;
  49. //All NTP fields have been given values, now
  50. //you can send a packet requesting a timestamp:
  51. Udp.beginPacket(timeServer, 123); //NTP requests are to port 123
  52. Udp.write(packetBuffer, NTP_PACKET_SIZE);
  53. Udp.endPacket();
  54. //Wait to see if a reply is available
  55. delay(250); //Adjust this delay for time server (effects accuracy, use shortest delay possible)
  56. if (Udp.parsePacket())
  57. {
  58. //We've received a packet, read the data from it
  59. Udp.read(packetBuffer, NTP_PACKET_SIZE);
  60. //The timestamp starts at byte 40 of the received packet and is four bytes,
  61. //or two words, long. First, extract the two words:
  62. unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  63. unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  64. //Combine the four bytes (two words) into a long integer
  65. //This is NTP time (seconds since Jan 1 1900):
  66. unsigned long secsSince1900 = highWord << 16 | lowWord;
  67. //Now convert NTP time into everyday time:
  68. //Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  69. const unsigned long SEVENTY_YEARS = 2208988800UL;
  70. //Subtract seventy years:
  71. unsigned long epoch = secsSince1900 - SEVENTY_YEARS;
  72. epoch = epoch - TZ_OFFSET;
  73. epoch = epoch + dstOffset(epoch); //Adjust for DLT
  74. return epoch;
  75. }
  76. return 0;
  77. }
  78. int dstOffset (unsigned long unixTime)
  79. {
  80. //Receives unix epoch time and returns seconds of offset for local DST
  81. //Valid thru 2099 for US only, Calculations from "http://www.webexhibits.org/daylightsaving/i.html"
  82. //Code idea from jm_wsb @ "http://forum.arduino.cc/index.php/topic,40286.0.html"
  83. //Get epoch times @ "http://www.epochconverter.com/" for testing
  84. //DST update wont be reflected until the next time sync
  85. time_t t = unixTime;
  86. int beginDSTDay = (14 - (1 + year(t) * 5 / 4) % 7);
  87. int beginDSTMonth=3;
  88. int endDSTDay = (7 - (1 + year(t) * 5 / 4) % 7);
  89. int endDSTMonth=11;
  90. if (((month(t) > beginDSTMonth) && (month(t) < endDSTMonth))
  91. || ((month(t) == beginDSTMonth) && (day(t) > beginDSTDay))
  92. || ((month(t) == beginDSTMonth) && (day(t) == beginDSTDay) && (hour(t) >= 2))
  93. || ((month(t) == endDSTMonth) && (day(t) < endDSTDay))
  94. || ((month(t) == endDSTMonth) && (day(t) == endDSTDay) && (hour(t) < 1)))
  95. return (3600); //Add back in one hours worth of seconds - DST in effect
  96. else
  97. return (0); //NonDST
  98. }
  99. void setup()
  100. {
  101. Serial.begin(9600);
  102. Ethernet.begin(mac, ip, mydns, gateway, subnet);
  103. Udp.begin(8888);
  104. delay(3000);
  105. setSyncInterval(28800); //Every 8 hours
  106. setSyncProvider (requestNTP);
  107. while (timeStatus() < 2); //Wait for time sync
  108. prevDisplay = now();
  109. }
  110. void loop()
  111. {
  112. if( now() != prevDisplay) //Update the display only if the time has changed
  113. {
  114. prevDisplay = now();
  115. digitalClockDisplay();
  116. }
  117. }
  118. void digitalClockDisplay()
  119. {
  120. //Digital clock display of the time
  121. Serial.print(hour());
  122. printDigits(minute());
  123. printDigits(second());
  124. Serial.print(" - ");
  125. Serial.print(day());
  126. Serial.print(" ");
  127. Serial.print(monthShortStr(month()));
  128. Serial.print(" ");
  129. Serial.print(year());
  130. Serial.println();
  131. }
  132. void printDigits(int digits)
  133. {
  134. //Utility function for digital clock display: prints preceding colon and leading 0
  135. Serial.print(":");
  136. if(digits < 10)
  137. Serial.print('0');
  138. Serial.print(digits);
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement