|
| 1 | +/* |
| 2 | +
|
| 3 | + Udp NTP Client |
| 4 | +
|
| 5 | + Get the time from a Network Time Protocol (NTP) time server |
| 6 | + Demonstrates use of UDP sendPacket and ReceivePacket |
| 7 | + For more on NTP time servers and the messages needed to communicate with them, |
| 8 | + see http://en.wikipedia.org/wiki/Network_Time_Protocol |
| 9 | +
|
| 10 | + created 4 Sep 2010 |
| 11 | + by Michael Margolis |
| 12 | + modified 9 Apr 2012 |
| 13 | + by Tom Igoe |
| 14 | + updated for the ESP8266 12 Apr 2015 |
| 15 | + by Ivan Grokhotkov |
| 16 | +
|
| 17 | + This code is in the public domain. |
| 18 | +
|
| 19 | + */ |
| 20 | + |
| 21 | +#include <ESP8266WiFi.h> |
| 22 | +#include <WiFiUdp.h> |
| 23 | + |
| 24 | +char ssid[] = "*************"; // your network SSID (name) |
| 25 | +char pass[] = "********"; // your network password |
| 26 | + |
| 27 | + |
| 28 | +unsigned int localPort = 2390; // local port to listen for UDP packets |
| 29 | + |
| 30 | +IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server |
| 31 | + |
| 32 | +const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message |
| 33 | + |
| 34 | +byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets |
| 35 | + |
| 36 | +// A UDP instance to let us send and receive packets over UDP |
| 37 | +WiFiUDP udp; |
| 38 | + |
| 39 | +void setup() |
| 40 | +{ |
| 41 | + Serial.begin(115200); |
| 42 | + Serial.println(); |
| 43 | + Serial.println(); |
| 44 | + |
| 45 | + // We start by connecting to a WiFi network |
| 46 | + Serial.print("Connecting to "); |
| 47 | + Serial.println(ssid); |
| 48 | + WiFi.begin(ssid, pass); |
| 49 | + |
| 50 | + while (WiFi.status() != WL_CONNECTED) { |
| 51 | + delay(500); |
| 52 | + Serial.print("."); |
| 53 | + } |
| 54 | + Serial.println(""); |
| 55 | + |
| 56 | + Serial.println("WiFi connected"); |
| 57 | + Serial.println("IP address: "); |
| 58 | + Serial.println(WiFi.localIP()); |
| 59 | + |
| 60 | + Serial.println("Starting UDP"); |
| 61 | + udp.begin(localPort); |
| 62 | + Serial.print("Local port: "); |
| 63 | + Serial.println(udp.localPort()); |
| 64 | +} |
| 65 | + |
| 66 | +void loop() |
| 67 | +{ |
| 68 | + sendNTPpacket(timeServer); // send an NTP packet to a time server |
| 69 | + // wait to see if a reply is available |
| 70 | + delay(1000); |
| 71 | + |
| 72 | + int cb = udp.parsePacket(); |
| 73 | + if (!cb) { |
| 74 | + Serial.println("no packet yet"); |
| 75 | + } |
| 76 | + else { |
| 77 | + Serial.print("packet received, length="); |
| 78 | + Serial.println(cb); |
| 79 | + // We've received a packet, read the data from it |
| 80 | + udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer |
| 81 | + |
| 82 | + //the timestamp starts at byte 40 of the received packet and is four bytes, |
| 83 | + // or two words, long. First, esxtract the two words: |
| 84 | + |
| 85 | + unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); |
| 86 | + unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); |
| 87 | + // combine the four bytes (two words) into a long integer |
| 88 | + // this is NTP time (seconds since Jan 1 1900): |
| 89 | + unsigned long secsSince1900 = highWord << 16 | lowWord; |
| 90 | + Serial.print("Seconds since Jan 1 1900 = " ); |
| 91 | + Serial.println(secsSince1900); |
| 92 | + |
| 93 | + // now convert NTP time into everyday time: |
| 94 | + Serial.print("Unix time = "); |
| 95 | + // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: |
| 96 | + const unsigned long seventyYears = 2208988800UL; |
| 97 | + // subtract seventy years: |
| 98 | + unsigned long epoch = secsSince1900 - seventyYears; |
| 99 | + // print Unix time: |
| 100 | + Serial.println(epoch); |
| 101 | + |
| 102 | + |
| 103 | + // print the hour, minute and second: |
| 104 | + Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) |
| 105 | + Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) |
| 106 | + Serial.print(':'); |
| 107 | + if ( ((epoch % 3600) / 60) < 10 ) { |
| 108 | + // In the first 10 minutes of each hour, we'll want a leading '0' |
| 109 | + Serial.print('0'); |
| 110 | + } |
| 111 | + Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) |
| 112 | + Serial.print(':'); |
| 113 | + if ( (epoch % 60) < 10 ) { |
| 114 | + // In the first 10 seconds of each minute, we'll want a leading '0' |
| 115 | + Serial.print('0'); |
| 116 | + } |
| 117 | + Serial.println(epoch % 60); // print the second |
| 118 | + } |
| 119 | + // wait ten seconds before asking for the time again |
| 120 | + delay(10000); |
| 121 | +} |
| 122 | + |
| 123 | +// send an NTP request to the time server at the given address |
| 124 | +unsigned long sendNTPpacket(IPAddress& address) |
| 125 | +{ |
| 126 | + Serial.println("sending NTP packet..."); |
| 127 | + // set all bytes in the buffer to 0 |
| 128 | + memset(packetBuffer, 0, NTP_PACKET_SIZE); |
| 129 | + // Initialize values needed to form NTP request |
| 130 | + // (see URL above for details on the packets) |
| 131 | + packetBuffer[0] = 0b11100011; // LI, Version, Mode |
| 132 | + packetBuffer[1] = 0; // Stratum, or type of clock |
| 133 | + packetBuffer[2] = 6; // Polling Interval |
| 134 | + packetBuffer[3] = 0xEC; // Peer Clock Precision |
| 135 | + // 8 bytes of zero for Root Delay & Root Dispersion |
| 136 | + packetBuffer[12] = 49; |
| 137 | + packetBuffer[13] = 0x4E; |
| 138 | + packetBuffer[14] = 49; |
| 139 | + packetBuffer[15] = 52; |
| 140 | + |
| 141 | + // all NTP fields have been given values, now |
| 142 | + // you can send a packet requesting a timestamp: |
| 143 | + udp.beginPacket(address, 123); //NTP requests are to port 123 |
| 144 | + udp.write(packetBuffer, NTP_PACKET_SIZE); |
| 145 | + udp.endPacket(); |
| 146 | +} |
0 commit comments