Attachment 'getNTPtime.c'

Download

   1 /* get UTC time from NTP server  2015-12-20
   2   needs the following declarations in the main program:
   3   unsigned int localPort = 2390;      // local port to listen for UDP packets
   4   unsigned long epoch;
   5   unsigned long timeNTPstart;
   6   unsigned long timeUnix;
   7 
   8   // Don't hardwire the IP address or we won't get the benefits of the pool.
   9   //  Lookup the IP address for the host name instead 
  10   //IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
  11   IPAddress timeServerIP; // time.nist.gov NTP server address
  12   const char* ntpServerName = "time.nist.gov";
  13 
  14   const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
  15 
  16   byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
  17 
  18   // A UDP instance to let us send and receive packets over UDP
  19   WiFiUDP udp;
  20 */
  21 
  22 // send an NTP request to the time server at the given address
  23 unsigned long sendNTPpacket(IPAddress& address)
  24 {
  25   Serial.println("sending NTP packet...");
  26   // set all bytes in the buffer to 0
  27   memset(packetBuffer, 0, NTP_PACKET_SIZE);
  28   // Initialize values needed to form NTP request
  29   // (see URL above for details on the packets)
  30   packetBuffer[0] = 0b11100011;   // LI, Version, Mode
  31   packetBuffer[1] = 0;     // Stratum, or type of clock
  32   packetBuffer[2] = 6;     // Polling Interval
  33   packetBuffer[3] = 0xEC;  // Peer Clock Precision
  34   // 8 bytes of zero for Root Delay & Root Dispersion
  35   packetBuffer[12]  = 49;
  36   packetBuffer[13]  = 0x4E;
  37   packetBuffer[14]  = 49;
  38   packetBuffer[15]  = 52;
  39 
  40   // all NTP fields have been given values, now
  41   // you can send a packet requesting a timestamp:
  42   udp.beginPacket(address, 123); //NTP requests are to port 123
  43   udp.write(packetBuffer, NTP_PACKET_SIZE);
  44   udp.endPacket();
  45 }  // sendNTPPacket()
  46 
  47 void unixToUTC(unsigned long epoch) {
  48   // print the hour, minute and second:
  49   Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)
  50   Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
  51   Serial.print(':');
  52   if ( ((epoch % 3600) / 60) < 10 ) {
  53     // In the first 10 minutes of each hour, we'll want a leading '0'
  54     Serial.print('0');
  55   }
  56   Serial.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
  57   Serial.print(':');
  58   if ( (epoch % 60) < 10 ) {
  59     // In the first 10 seconds of each minute, we'll want a leading '0'
  60     Serial.print('0');
  61   }
  62   Serial.println(epoch % 60); // print the second
  63 }  // unixToUTC()
  64 
  65 void getNTPtime() {
  66   //get a random server from the pool
  67   WiFi.hostByName(ntpServerName, timeServerIP); 
  68 
  69   sendNTPpacket(timeServerIP); // send an NTP packet to a time server
  70   // wait to see if a reply is available
  71   delay(1000);
  72   
  73   int cb = udp.parsePacket();
  74   if (!cb) {
  75     Serial.println("no packet yet");
  76   }
  77   else {
  78     Serial.print("packet received, length=");
  79     Serial.println(cb);
  80     // We've received a packet, read the data from it
  81     udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
  82 
  83     //the timestamp starts at byte 40 of the received packet and is four bytes,
  84     // or two words, long. First, esxtract the two words:
  85 
  86     unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
  87     unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
  88     // combine the four bytes (two words) into a long integer
  89     // this is NTP time (seconds since Jan 1 1900):
  90     unsigned int secsSince1900 = highWord << 16 | lowWord;
  91     Serial.print("Seconds since Jan 1 1900 = " );
  92     Serial.println(secsSince1900);
  93 
  94     // now convert NTP time into everyday time:
  95     Serial.print("Unix time = ");
  96     // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
  97     const unsigned long seventyYears = 2208988800UL;
  98     // subtract seventy years:
  99     epoch = secsSince1900 - seventyYears;
 100     // print Unix time:
 101     Serial.println(epoch);
 102     // print UTC
 103     unixToUTC(epoch);
 104   }
 105 }  // getNTPtime()
 106   

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2015-12-20 15:34:00, 3.3 KB) [[attachment:GetTemp.c]]
  • [get | view] (2015-12-18 19:00:00, 79.9 KB) [[attachment:Thingspeak_WarmWasser.png]]
  • [get | view] (2015-12-21 15:53:00, 11.9 KB) [[attachment:WiFiLoggerOneWire.c]]
  • [get | view] (2015-12-20 15:35:00, 1.0 KB) [[attachment:WiFiLoggerOneWire.h]]
  • [get | view] (2015-12-15 21:02:00, 77.5 KB) [[attachment:WiFiLoggerOneWireVZ.png]]
  • [get | view] (2015-12-20 15:05:00, 103.1 KB) [[attachment:WiFiLoggerOneWire_config.png]]
  • [get | view] (2015-12-18 19:51:00, 89.0 KB) [[attachment:WiFiLogger_DSC06322.jpg]]
  • [get | view] (2016-05-08 10:40:00, 36.5 KB) [[attachment:WiFiLogger_screenshot.png]]
  • [get | view] (2016-06-20 05:37:00, 35.1 KB) [[attachment:WiFiLogger_screenshot2.png]]
  • [get | view] (2015-12-15 14:38:00, 100.5 KB) [[attachment:WiFi_Logger_DSC06319.jpg]]
  • [get | view] (2015-12-20 15:33:00, 0.7 KB) [[attachment:credentials.c]]
  • [get | view] (2015-12-20 15:33:00, 3.9 KB) [[attachment:getNTPtime.c]]
  • [get | view] (2015-12-20 15:34:00, 5.4 KB) [[attachment:handleHTTP.c]]
  • [get | view] (2015-12-20 15:35:00, 0.4 KB) [[attachment:tools.c]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.