Skip to content

Commit f5c782b

Browse files
authored
Merge pull request #1714 from arduino/karlsoderby/giga-cs-rtc-ntp
[GIGA R1 WiFi] Update RTC/NTP/UDP example.
2 parents 2f299c1 + 6bde3b0 commit f5c782b

File tree

2 files changed

+194
-6
lines changed

2 files changed

+194
-6
lines changed

content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md

Lines changed: 188 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ String getLocaltime()
383383

384384
To get accurate time, you'll want to change the values in `void RTCset()` to whatever time it is when you're starting this clock. As long as the VRTC pin is connected to power, the clock will keep ticking and time will be kept accurately.
385385

386-
### RTC Wi-Fi® Example
386+
### RTC / UDP / NTP Example
387387

388388
With the following sketch, you can automatically set the time by requesting the time from a Network Time Protocol (NTP), using the UDP protocol.
389389

@@ -571,9 +571,195 @@ void printWifiStatus()
571571
Serial.print(rssi);
572572
Serial.println(" dBm");
573573
}
574-
575574
```
575+
### RTC / UDP / NTP Example (Timezone)
576+
577+
This example provides an option to set the timezone. As the received epoch is based on GMT time, you can input e.g. `-1` or `5` which represents the hours. The `timezone` variable is changed at the top of the example.
578+
579+
```arduino
580+
/*
581+
Udp NTP Client with Timezone Adjustment
582+
583+
Get the time from a Network Time Protocol (NTP) time server
584+
Demonstrates use of UDP sendPacket and ReceivePacket
585+
For more on NTP time servers and the messages needed to communicate with them,
586+
see http://en.wikipedia.org/wiki/Network_Time_Protocol
587+
588+
created 4 Sep 2010
589+
by Michael Margolis
590+
modified 9 Apr 2012
591+
by Tom Igoe
592+
modified 28 Dec 2022
593+
by Giampaolo Mancini
594+
modified 29 Jan 2024
595+
by Karl Söderby
596+
597+
This code is in the public domain.
598+
*/
599+
600+
#include <WiFi.h>
601+
#include <WiFiUdp.h>
602+
#include <mbed_mktime.h>
603+
604+
int timezone = -1; //this is GMT -1.
605+
606+
int status = WL_IDLE_STATUS;
607+
608+
char ssid[] = "Flen"; // your network SSID (name)
609+
char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
610+
611+
int keyIndex = 0; // your network key index number (needed only for WEP)
612+
613+
unsigned int localPort = 2390; // local port to listen for UDP packets
614+
615+
// IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server
616+
617+
constexpr auto timeServer{ "pool.ntp.org" };
618+
619+
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
620+
621+
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
622+
623+
// A UDP instance to let us send and receive packets over UDP
624+
WiFiUDP Udp;
625+
626+
constexpr unsigned long printInterval{ 1000 };
627+
unsigned long printNow{};
576628
629+
void setup() {
630+
// Open serial communications and wait for port to open:
631+
Serial.begin(9600);
632+
while (!Serial) {
633+
; // wait for serial port to connect. Needed for native USB port only
634+
}
635+
636+
// check for the WiFi module:
637+
if (WiFi.status() == WL_NO_SHIELD) {
638+
Serial.println("Communication with WiFi module failed!");
639+
// don't continue
640+
while (true)
641+
;
642+
}
643+
644+
// attempt to connect to WiFi network:
645+
while (status != WL_CONNECTED) {
646+
Serial.print("Attempting to connect to SSID: ");
647+
Serial.println(ssid);
648+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
649+
status = WiFi.begin(ssid, pass);
650+
651+
// wait 10 seconds for connection:
652+
delay(10000);
653+
}
654+
655+
Serial.println("Connected to WiFi");
656+
printWifiStatus();
657+
658+
setNtpTime();
659+
}
660+
661+
void loop() {
662+
if (millis() > printNow) {
663+
Serial.print("System Clock: ");
664+
Serial.println(getLocaltime());
665+
printNow = millis() + printInterval;
666+
}
667+
}
668+
669+
void setNtpTime() {
670+
Udp.begin(localPort);
671+
sendNTPpacket(timeServer);
672+
delay(1000);
673+
parseNtpPacket();
674+
}
675+
676+
// send an NTP request to the time server at the given address
677+
unsigned long sendNTPpacket(const char* address) {
678+
memset(packetBuffer, 0, NTP_PACKET_SIZE);
679+
packetBuffer[0] = 0b11100011; // LI, Version, Mode
680+
packetBuffer[1] = 0; // Stratum, or type of clock
681+
packetBuffer[2] = 6; // Polling Interval
682+
packetBuffer[3] = 0xEC; // Peer Clock Precision
683+
// 8 bytes of zero for Root Delay & Root Dispersion
684+
packetBuffer[12] = 49;
685+
packetBuffer[13] = 0x4E;
686+
packetBuffer[14] = 49;
687+
packetBuffer[15] = 52;
688+
689+
Udp.beginPacket(address, 123); // NTP requests are to port 123
690+
Udp.write(packetBuffer, NTP_PACKET_SIZE);
691+
Udp.endPacket();
692+
}
693+
694+
unsigned long parseNtpPacket() {
695+
if (!Udp.parsePacket())
696+
return 0;
697+
698+
Udp.read(packetBuffer, NTP_PACKET_SIZE);
699+
const unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
700+
const unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
701+
const unsigned long secsSince1900 = highWord << 16 | lowWord;
702+
constexpr unsigned long seventyYears = 2208988800UL;
703+
const unsigned long epoch = secsSince1900 - seventyYears;
704+
705+
const unsigned long new_epoch = epoch + (3600 * timezone); //multiply the timezone with 3600 (1 hour)
706+
707+
set_time(new_epoch);
708+
709+
#if defined(VERBOSE)
710+
Serial.print("Seconds since Jan 1 1900 = ");
711+
Serial.println(secsSince1900);
712+
713+
// now convert NTP time into everyday time:
714+
Serial.print("Unix time = ");
715+
// print Unix time:
716+
Serial.println(epoch);
717+
718+
// print the hour, minute and second:
719+
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
720+
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
721+
Serial.print(':');
722+
if (((epoch % 3600) / 60) < 10) {
723+
// In the first 10 minutes of each hour, we'll want a leading '0'
724+
Serial.print('0');
725+
}
726+
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
727+
Serial.print(':');
728+
if ((epoch % 60) < 10) {
729+
// In the first 10 seconds of each minute, we'll want a leading '0'
730+
Serial.print('0');
731+
}
732+
Serial.println(epoch % 60); // print the second
733+
#endif
734+
735+
return epoch;
736+
}
737+
738+
String getLocaltime() {
739+
char buffer[32];
740+
tm t;
741+
_rtc_localtime(time(NULL), &t, RTC_FULL_LEAP_YEAR_SUPPORT);
742+
strftime(buffer, 32, "%Y-%m-%d %k:%M:%S", &t);
743+
return String(buffer);
744+
}
745+
746+
void printWifiStatus() {
747+
// print the SSID of the network you're attached to:
748+
Serial.print("SSID: ");
749+
Serial.println(WiFi.SSID());
750+
751+
// print your board's IP address:
752+
IPAddress ip = WiFi.localIP();
753+
Serial.print("IP Address: ");
754+
Serial.println(ip);
755+
756+
// print the received signal strength:
757+
long rssi = WiFi.RSSI();
758+
Serial.print("signal strength (RSSI):");
759+
Serial.print(rssi);
760+
Serial.println(" dBm");
761+
}
762+
```
577763

578764
### VRTC Pin
579765

content/hardware/10.mega/boards/giga-r1-wifi/tutorials/giga-wifi/giga-wifi.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ void printMacAddress(byte mac[]) {
158158
}
159159
```
160160

161-
### Wi-Fi RTC Example
161+
### RTC / UDP / NTP Example
162162

163163
```arduino
164164
/*
@@ -346,13 +346,13 @@ void printWifiStatus()
346346
}
347347
```
348348

349-
### Wi-Fi RTC Example with Timezone Adjustment
349+
### RTC / UDP / NTP Example (Timezone)
350350

351351
This example provides an option to set the timezone. As the received epoch is based on GMT time, you can input e.g. `-1` or `5` which represents the hours. The `timezone` variable is changed at the top of the example.
352352

353353
```arduino
354354
/*
355-
Udp NTP Client
355+
Udp NTP Client with Timezone Adjustment
356356
357357
Get the time from a Network Time Protocol (NTP) time server
358358
Demonstrates use of UDP sendPacket and ReceivePacket
@@ -365,6 +365,8 @@ This example provides an option to set the timezone. As the received epoch is ba
365365
by Tom Igoe
366366
modified 28 Dec 2022
367367
by Giampaolo Mancini
368+
modified 29 Jan 2024
369+
by Karl Söderby
368370
369371
This code is in the public domain.
370372
*/
@@ -474,7 +476,7 @@ unsigned long parseNtpPacket() {
474476
constexpr unsigned long seventyYears = 2208988800UL;
475477
const unsigned long epoch = secsSince1900 - seventyYears;
476478
477-
new_epoch = epoch + (3600 * timezone); //multiply the timezone with 3600 (1 hour)
479+
const unsigned long new_epoch = epoch + (3600 * timezone); //multiply the timezone with 3600 (1 hour)
478480
479481
set_time(new_epoch);
480482

0 commit comments

Comments
 (0)