|
| 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 | + |
| 15 | + This code is in the public domain. |
| 16 | + |
| 17 | + */ |
| 18 | + |
| 19 | +#include <SPI.h> |
| 20 | +#include <WiFi.h> |
| 21 | +#include <WiFiUdp.h> |
| 22 | + |
| 23 | +int status = WL_IDLE_STATUS; |
| 24 | +char ssid[] = "mynetwork"; // your network SSID (name) |
| 25 | +char pass[] = "mypassword"; // your network password |
| 26 | +int keyIndex = 0; // your network key Index number (needed only for WEP) |
| 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 | + // Open serial communications and wait for port to open: |
| 42 | + Serial.begin(9600); |
| 43 | + while (!Serial) { |
| 44 | + ; // wait for serial port to connect. Needed for Leonardo only |
| 45 | + } |
| 46 | + |
| 47 | + // check for the presence of the shield: |
| 48 | + if (WiFi.status() == WL_NO_SHIELD) { |
| 49 | + Serial.println("WiFi shield not present"); |
| 50 | + // don't continue: |
| 51 | + while(true); |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + // attempt to connect to Wifi network: |
| 56 | + while ( status != WL_CONNECTED) { |
| 57 | + Serial.print("Attempting to connect to SSID: "); |
| 58 | + Serial.println(ssid); |
| 59 | + // Connect to WPA/WPA2 network. Change this line if using open or WEP network: |
| 60 | + status = WiFi.begin(ssid, pass); |
| 61 | + |
| 62 | + // wait 10 seconds for connection: |
| 63 | + delay(10000); |
| 64 | + } |
| 65 | + |
| 66 | + Serial.println("Connected to wifi"); |
| 67 | + printWifiStatus(); |
| 68 | + |
| 69 | + Serial.println("\nStarting connection to server..."); |
| 70 | + Udp.begin(localPort); |
| 71 | +} |
| 72 | + |
| 73 | +void loop() |
| 74 | +{ |
| 75 | + sendNTPpacket(timeServer); // send an NTP packet to a time server |
| 76 | + // wait to see if a reply is available |
| 77 | + delay(1000); |
| 78 | + Serial.println( Udp.parsePacket() ); |
| 79 | + if ( Udp.parsePacket() ) { |
| 80 | + Serial.println("packet received"); |
| 81 | + // We've received a packet, read the data from it |
| 82 | + Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer |
| 83 | + |
| 84 | + //the timestamp starts at byte 40 of the received packet and is four bytes, |
| 85 | + // or two words, long. First, esxtract the two words: |
| 86 | + |
| 87 | + unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); |
| 88 | + unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); |
| 89 | + // combine the four bytes (two words) into a long integer |
| 90 | + // this is NTP time (seconds since Jan 1 1900): |
| 91 | + unsigned long secsSince1900 = highWord << 16 | lowWord; |
| 92 | + Serial.print("Seconds since Jan 1 1900 = " ); |
| 93 | + Serial.println(secsSince1900); |
| 94 | + |
| 95 | + // now convert NTP time into everyday time: |
| 96 | + Serial.print("Unix time = "); |
| 97 | + // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: |
| 98 | + const unsigned long seventyYears = 2208988800UL; |
| 99 | + // subtract seventy years: |
| 100 | + unsigned long epoch = secsSince1900 - seventyYears; |
| 101 | + // print Unix time: |
| 102 | + Serial.println(epoch); |
| 103 | + |
| 104 | + |
| 105 | + // print the hour, minute and second: |
| 106 | + Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) |
| 107 | + Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) |
| 108 | + Serial.print(':'); |
| 109 | + if ( ((epoch % 3600) / 60) < 10 ) { |
| 110 | + // In the first 10 minutes of each hour, we'll want a leading '0' |
| 111 | + Serial.print('0'); |
| 112 | + } |
| 113 | + Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) |
| 114 | + Serial.print(':'); |
| 115 | + if ( (epoch % 60) < 10 ) { |
| 116 | + // In the first 10 seconds of each minute, we'll want a leading '0' |
| 117 | + Serial.print('0'); |
| 118 | + } |
| 119 | + Serial.println(epoch %60); // print the second |
| 120 | + } |
| 121 | + // wait ten seconds before asking for the time again |
| 122 | + delay(10000); |
| 123 | +} |
| 124 | + |
| 125 | +// send an NTP request to the time server at the given address |
| 126 | +unsigned long sendNTPpacket(IPAddress& address) |
| 127 | +{ |
| 128 | + //Serial.println("1"); |
| 129 | + // set all bytes in the buffer to 0 |
| 130 | + memset(packetBuffer, 0, NTP_PACKET_SIZE); |
| 131 | + // Initialize values needed to form NTP request |
| 132 | + // (see URL above for details on the packets) |
| 133 | + //Serial.println("2"); |
| 134 | + packetBuffer[0] = 0b11100011; // LI, Version, Mode |
| 135 | + packetBuffer[1] = 0; // Stratum, or type of clock |
| 136 | + packetBuffer[2] = 6; // Polling Interval |
| 137 | + packetBuffer[3] = 0xEC; // Peer Clock Precision |
| 138 | + // 8 bytes of zero for Root Delay & Root Dispersion |
| 139 | + packetBuffer[12] = 49; |
| 140 | + packetBuffer[13] = 0x4E; |
| 141 | + packetBuffer[14] = 49; |
| 142 | + packetBuffer[15] = 52; |
| 143 | + |
| 144 | + //Serial.println("3"); |
| 145 | + |
| 146 | + // all NTP fields have been given values, now |
| 147 | + // you can send a packet requesting a timestamp: |
| 148 | + Udp.beginPacket(address, 123); //NTP requests are to port 123 |
| 149 | + //Serial.println("4"); |
| 150 | + Udp.write(packetBuffer,NTP_PACKET_SIZE); |
| 151 | + //Serial.println("5"); |
| 152 | + Udp.endPacket(); |
| 153 | + //Serial.println("6"); |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +void printWifiStatus() { |
| 158 | + // print the SSID of the network you're attached to: |
| 159 | + Serial.print("SSID: "); |
| 160 | + Serial.println(WiFi.SSID()); |
| 161 | + |
| 162 | + // print your WiFi shield's IP address: |
| 163 | + IPAddress ip = WiFi.localIP(); |
| 164 | + Serial.print("IP Address: "); |
| 165 | + Serial.println(ip); |
| 166 | + |
| 167 | + // print the received signal strength: |
| 168 | + long rssi = WiFi.RSSI(); |
| 169 | + Serial.print("signal strength (RSSI):"); |
| 170 | + Serial.print(rssi); |
| 171 | + Serial.println(" dBm"); |
| 172 | +} |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + |
| 179 | + |
| 180 | + |
| 181 | + |
| 182 | + |
0 commit comments