iotivity 0.9.0
[platform/upstream/iotivity.git] / service / notification-manager / SampleApp / arduino / Time / examples / TimeNTP / TimeNTP.ino
1 /*
2  * Time_NTP.pde
3  * Example showing time sync to NTP time source
4  *
5  * This sketch uses the Ethernet library
6  */
7  
8 #include <Time.h> 
9 #include <Ethernet.h>
10 #include <EthernetUdp.h>
11 #include <SPI.h>
12
13 byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; 
14 // NTP Servers:
15 IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov
16 // IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov
17 // IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov
18
19
20 const int timeZone = 1;     // Central European Time
21 //const int timeZone = -5;  // Eastern Standard Time (USA)
22 //const int timeZone = -4;  // Eastern Daylight Time (USA)
23 //const int timeZone = -8;  // Pacific Standard Time (USA)
24 //const int timeZone = -7;  // Pacific Daylight Time (USA)
25
26
27 EthernetUDP Udp;
28 unsigned int localPort = 8888;  // local port to listen for UDP packets
29
30 void setup() 
31 {
32   Serial.begin(9600);
33   while (!Serial) ; // Needed for Leonardo only
34   delay(250);
35   Serial.println("TimeNTP Example");
36   if (Ethernet.begin(mac) == 0) {
37     // no point in carrying on, so do nothing forevermore:
38     while (1) {
39       Serial.println("Failed to configure Ethernet using DHCP");
40       delay(10000);
41     }
42   }
43   Serial.print("IP number assigned by DHCP is ");
44   Serial.println(Ethernet.localIP());
45   Udp.begin(localPort);
46   Serial.println("waiting for sync");
47   setSyncProvider(getNtpTime);
48 }
49
50 time_t prevDisplay = 0; // when the digital clock was displayed
51
52 void loop()
53 {  
54   if (timeStatus() != timeNotSet) {
55     if (now() != prevDisplay) { //update the display only if time has changed
56       prevDisplay = now();
57       digitalClockDisplay();  
58     }
59   }
60 }
61
62 void digitalClockDisplay(){
63   // digital clock display of the time
64   Serial.print(hour());
65   printDigits(minute());
66   printDigits(second());
67   Serial.print(" ");
68   Serial.print(day());
69   Serial.print(" ");
70   Serial.print(month());
71   Serial.print(" ");
72   Serial.print(year()); 
73   Serial.println(); 
74 }
75
76 void printDigits(int digits){
77   // utility for digital clock display: prints preceding colon and leading 0
78   Serial.print(":");
79   if(digits < 10)
80     Serial.print('0');
81   Serial.print(digits);
82 }
83
84 /*-------- NTP code ----------*/
85
86 const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
87 byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
88
89 time_t getNtpTime()
90 {
91   while (Udp.parsePacket() > 0) ; // discard any previously received packets
92   Serial.println("Transmit NTP Request");
93   sendNTPpacket(timeServer);
94   uint32_t beginWait = millis();
95   while (millis() - beginWait < 1500) {
96     int size = Udp.parsePacket();
97     if (size >= NTP_PACKET_SIZE) {
98       Serial.println("Receive NTP Response");
99       Udp.read(packetBuffer, NTP_PACKET_SIZE);  // read packet into the buffer
100       unsigned long secsSince1900;
101       // convert four bytes starting at location 40 to a long integer
102       secsSince1900 =  (unsigned long)packetBuffer[40] << 24;
103       secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
104       secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
105       secsSince1900 |= (unsigned long)packetBuffer[43];
106       return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
107     }
108   }
109   Serial.println("No NTP Response :-(");
110   return 0; // return 0 if unable to get the time
111 }
112
113 // send an NTP request to the time server at the given address
114 void sendNTPpacket(IPAddress &address)
115 {
116   // set all bytes in the buffer to 0
117   memset(packetBuffer, 0, NTP_PACKET_SIZE);
118   // Initialize values needed to form NTP request
119   // (see URL above for details on the packets)
120   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
121   packetBuffer[1] = 0;     // Stratum, or type of clock
122   packetBuffer[2] = 6;     // Polling Interval
123   packetBuffer[3] = 0xEC;  // Peer Clock Precision
124   // 8 bytes of zero for Root Delay & Root Dispersion
125   packetBuffer[12]  = 49;
126   packetBuffer[13]  = 0x4E;
127   packetBuffer[14]  = 49;
128   packetBuffer[15]  = 52;
129   // all NTP fields have been given values, now
130   // you can send a packet requesting a timestamp:                 
131   Udp.beginPacket(address, 123); //NTP requests are to port 123
132   Udp.write(packetBuffer, NTP_PACKET_SIZE);
133   Udp.endPacket();
134 }
135