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/giga-wifi/giga-wifi.md
+187Lines changed: 187 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -346,6 +346,193 @@ void printWifiStatus()
346
346
}
347
347
```
348
348
349
+
### Wi-Fi RTC Example with Timezone Adjustment
350
+
351
+
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
+
353
+
```arduino
354
+
/*
355
+
Udp NTP Client
356
+
357
+
Get the time from a Network Time Protocol (NTP) time server
358
+
Demonstrates use of UDP sendPacket and ReceivePacket
359
+
For more on NTP time servers and the messages needed to communicate with them,
360
+
see http://en.wikipedia.org/wiki/Network_Time_Protocol
361
+
362
+
created 4 Sep 2010
363
+
by Michael Margolis
364
+
modified 9 Apr 2012
365
+
by Tom Igoe
366
+
modified 28 Dec 2022
367
+
by Giampaolo Mancini
368
+
369
+
This code is in the public domain.
370
+
*/
371
+
372
+
#include <WiFi.h>
373
+
#include <WiFiUdp.h>
374
+
#include <mbed_mktime.h>
375
+
376
+
int timezone = -1; //this is GMT -1.
377
+
378
+
int status = WL_IDLE_STATUS;
379
+
380
+
char ssid[] = "Flen"; // your network SSID (name)
381
+
char pass[] = ""; // your network password (use for WPA, or use as key for WEP)
382
+
383
+
int keyIndex = 0; // your network key index number (needed only for WEP)
384
+
385
+
unsigned int localPort = 2390; // local port to listen for UDP packets
386
+
387
+
// IPAddress timeServer(162, 159, 200, 123); // pool.ntp.org NTP server
388
+
389
+
constexpr auto timeServer{ "pool.ntp.org" };
390
+
391
+
const int NTP_PACKET_SIZE = 48; // NTP timestamp is in the first 48 bytes of the message
392
+
393
+
byte packetBuffer[NTP_PACKET_SIZE]; // buffer to hold incoming and outgoing packets
394
+
395
+
// A UDP instance to let us send and receive packets over UDP
396
+
WiFiUDP Udp;
397
+
398
+
constexpr unsigned long printInterval{ 1000 };
399
+
unsigned long printNow{};
400
+
401
+
void setup() {
402
+
// Open serial communications and wait for port to open:
403
+
Serial.begin(9600);
404
+
while (!Serial) {
405
+
; // wait for serial port to connect. Needed for native USB port only
406
+
}
407
+
408
+
// check for the WiFi module:
409
+
if (WiFi.status() == WL_NO_SHIELD) {
410
+
Serial.println("Communication with WiFi module failed!");
411
+
// don't continue
412
+
while (true)
413
+
;
414
+
}
415
+
416
+
// attempt to connect to WiFi network:
417
+
while (status != WL_CONNECTED) {
418
+
Serial.print("Attempting to connect to SSID: ");
419
+
Serial.println(ssid);
420
+
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
421
+
status = WiFi.begin(ssid, pass);
422
+
423
+
// wait 10 seconds for connection:
424
+
delay(10000);
425
+
}
426
+
427
+
Serial.println("Connected to WiFi");
428
+
printWifiStatus();
429
+
430
+
setNtpTime();
431
+
}
432
+
433
+
void loop() {
434
+
if (millis() > printNow) {
435
+
Serial.print("System Clock: ");
436
+
Serial.println(getLocaltime());
437
+
printNow = millis() + printInterval;
438
+
}
439
+
}
440
+
441
+
void setNtpTime() {
442
+
Udp.begin(localPort);
443
+
sendNTPpacket(timeServer);
444
+
delay(1000);
445
+
parseNtpPacket();
446
+
}
447
+
448
+
// send an NTP request to the time server at the given address
449
+
unsigned long sendNTPpacket(const char* address) {
0 commit comments