Skip to content

Commit 61e1024

Browse files
committed
Update
1 parent e4a2486 commit 61e1024

File tree

2 files changed

+191
-4
lines changed

2 files changed

+191
-4
lines changed

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

Lines changed: 189 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,196 @@ void printWifiStatus()
571571
Serial.print(rssi);
572572
Serial.println(" dBm");
573573
}
574-
575574
```
576575

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

578765
### VRTC Pin
579766

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

Lines changed: 2 additions & 2 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,7 +346,7 @@ 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

0 commit comments

Comments
 (0)