|
4 | 4 | * Then the current time from the RTC is printed to the Serial port.
|
5 | 5 | *
|
6 | 6 | * Instructions:
|
7 |
| - * 1. Change the WiFi credentials in the arduino_secrets.h file to match your WiFi network. |
8 |
| - * 2. Upload this sketch to Portenta C33 / UNO R4 WiFi. |
9 |
| - * 3. Open the Serial Monitor. |
| 7 | + * 1. Download the NTPClient library (https://github.com/arduino-libraries/NTPClient) through the Library Manager |
| 8 | + * 2. Change the WiFi credentials in the arduino_secrets.h file to match your WiFi network. |
| 9 | + * 3. Upload this sketch to Portenta C33 / UNO R4 WiFi. |
| 10 | + * 4. Open the Serial Monitor. |
10 | 11 | *
|
11 | 12 | * Initial author: Sebastian Romero @sebromero
|
12 | 13 | */
|
13 | 14 |
|
14 | 15 | #include "RTC.h"
|
| 16 | +#include <NTPClient.h> |
15 | 17 |
|
16 | 18 | #if defined(ARDUINO_PORTENTA_C33)
|
17 | 19 | #include <WiFiC3.h>
|
|
26 | 28 | char ssid[] = SECRET_SSID; // your network SSID (name)
|
27 | 29 | char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
|
28 | 30 |
|
29 |
| -constexpr unsigned int LOCAL_PORT = 2390; // local port to listen for UDP packets |
30 |
| -constexpr int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message |
31 |
| - |
32 | 31 | int wifiStatus = WL_IDLE_STATUS;
|
33 |
| -IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server |
34 |
| -byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets |
35 | 32 | WiFiUDP Udp; // A UDP instance to let us send and receive packets over UDP
|
36 |
| - |
37 |
| -// send an NTP request to the time server at the given address |
38 |
| -unsigned long sendNTPpacket(IPAddress& address) { |
39 |
| - // set all bytes in the buffer to 0 |
40 |
| - memset(packetBuffer, 0, NTP_PACKET_SIZE); |
41 |
| - // Initialize values needed to form NTP request |
42 |
| - // (see URL above for details on the packets) |
43 |
| - packetBuffer[0] = 0b11100011; // LI, Version, Mode |
44 |
| - packetBuffer[1] = 0; // Stratum, or type of clock |
45 |
| - packetBuffer[2] = 6; // Polling Interval |
46 |
| - packetBuffer[3] = 0xEC; // Peer Clock Precision |
47 |
| - // 8 bytes of zero for Root Delay & Root Dispersion |
48 |
| - packetBuffer[12] = 49; |
49 |
| - packetBuffer[13] = 0x4E; |
50 |
| - packetBuffer[14] = 49; |
51 |
| - packetBuffer[15] = 52; |
52 |
| - |
53 |
| - // all NTP fields have been given values, now |
54 |
| - // you can send a packet requesting a timestamp: |
55 |
| - Udp.beginPacket(address, 123); //NTP requests are to port 123 |
56 |
| - Udp.write(packetBuffer, NTP_PACKET_SIZE); |
57 |
| - Udp.endPacket(); |
58 |
| -} |
| 33 | +NTPClient timeClient(Udp); |
59 | 34 |
|
60 | 35 | void printWifiStatus() {
|
61 | 36 | // print the SSID of the network you're attached to:
|
@@ -102,56 +77,21 @@ void connectToWiFi(){
|
102 | 77 | printWifiStatus();
|
103 | 78 | }
|
104 | 79 |
|
105 |
| -/** |
106 |
| - * Calculates the current unix time, that is the time in seconds since Jan 1 1970. |
107 |
| - * It will try to get the time from the NTP server up to `maxTries` times, |
108 |
| - * then convert it to Unix time and return it. |
109 |
| - * You can optionally specify a time zone offset in hours that can be positive or negative. |
110 |
| -*/ |
111 |
| -unsigned long getUnixTime(int8_t timeZoneOffsetHours = 0, uint8_t maxTries = 5){ |
112 |
| - // Try up to `maxTries` times to get a timestamp from the NTP server, then give up. |
113 |
| - for (size_t i = 0; i < maxTries; i++){ |
114 |
| - sendNTPpacket(timeServer); // send an NTP packet to a time server |
115 |
| - // wait to see if a reply is available |
116 |
| - delay(1000); |
117 |
| - |
118 |
| - if (Udp.parsePacket()) { |
119 |
| - Serial.println("packet received"); |
120 |
| - Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer |
121 |
| - |
122 |
| - //the timestamp starts at byte 40 of the received packet and is four bytes, |
123 |
| - //or two words, long. First, extract the two words: |
124 |
| - unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); |
125 |
| - unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); |
126 |
| - |
127 |
| - // Combine the four bytes (two words) into a long integer |
128 |
| - // this is NTP time (seconds since Jan 1 1900): |
129 |
| - unsigned long secsSince1900 = highWord << 16 | lowWord; |
130 |
| - |
131 |
| - // Now convert NTP time into everyday time: |
132 |
| - // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: |
133 |
| - const unsigned long seventyYears = 2208988800UL; |
134 |
| - unsigned long secondsSince1970 = secsSince1900 - seventyYears + (timeZoneOffsetHours * 3600); |
135 |
| - return secondsSince1970; |
136 |
| - } |
137 |
| - } |
138 |
| - |
139 |
| - return 0; |
140 |
| -} |
141 |
| - |
142 | 80 | void setup(){
|
143 | 81 | Serial.begin(9600);
|
144 | 82 | while (!Serial);
|
145 | 83 |
|
146 | 84 | connectToWiFi();
|
147 |
| - Serial.println("\nStarting connection to server..."); |
148 |
| - Udp.begin(LOCAL_PORT); |
149 | 85 | RTC.begin();
|
| 86 | + Serial.println("\nStarting connection to server..."); |
| 87 | + timeClient.begin(); |
| 88 | + timeClient.update(); |
150 | 89 |
|
151 | 90 | // Get the current date and time from an NTP server and convert
|
152 | 91 | // it to UTC +2 by passing the time zone offset in hours.
|
153 | 92 | // You may change the time zone offset to your local one.
|
154 |
| - auto unixTime = getUnixTime(2); |
| 93 | + auto timeZoneOffsetHours = 2; |
| 94 | + auto unixTime = timeClient.getEpochTime() + (timeZoneOffsetHours * 3600); |
155 | 95 | Serial.print("Unix time = ");
|
156 | 96 | Serial.println(unixTime);
|
157 | 97 | RTCTime timeToSet = RTCTime(unixTime);
|
|
0 commit comments