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
+188-2Lines changed: 188 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,195 @@ void printWifiStatus()
571
571
Serial.print(rssi);
572
572
Serial.println(" dBm");
573
573
}
574
-
575
574
```
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{};
576
628
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) {
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.
352
352
353
353
```arduino
354
354
/*
355
-
Udp NTP Client
355
+
Udp NTP Client with Timezone Adjustment
356
356
357
357
Get the time from a Network Time Protocol (NTP) time server
358
358
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
365
365
by Tom Igoe
366
366
modified 28 Dec 2022
367
367
by Giampaolo Mancini
368
+
modified 29 Jan 2024
369
+
by Karl Söderby
368
370
369
371
This code is in the public domain.
370
372
*/
@@ -474,7 +476,7 @@ unsigned long parseNtpPacket() {
474
476
constexpr unsigned long seventyYears = 2208988800UL;
475
477
const unsigned long epoch = secsSince1900 - seventyYears;
476
478
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)
0 commit comments