You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/hardware/10.mega/boards/giga-r1-wifi/tutorials/cheat-sheet/cheat-sheet.md
+189-2Lines changed: 189 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -383,7 +383,7 @@ String getLocaltime()
383
383
384
384
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.
385
385
386
-
### RTC Wi-Fi® Example
386
+
### RTC / UDP / NTP Example
387
387
388
388
With the following sketch, you can automatically set the time by requesting the time from a Network Time Protocol (NTP), using the UDP protocol.
389
389
@@ -571,9 +571,196 @@ void printWifiStatus()
571
571
Serial.print(rssi);
572
572
Serial.println(" dBm");
573
573
}
574
-
575
574
```
576
575
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) {
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.
0 commit comments