Skip to content

Commit f6efe5d

Browse files
committed
Added NTP example.
1 parent 285e88d commit f6efe5d

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ Various examples for DOIT ESP32 DEVKIT V1 using Arduino IDE and TinyGo.
1818
- [Web API Request](examples/Web%20API%20Request/README.md)
1919
- [8-Channel Transistor Array](examples/8-Channel%20Transistor%20Array/README.md)
2020
- [SSR Relay](examples/SSR%20Relay/README.md)
21+
- [NTP Client](examples/NTP%20Client/README.md)
2122

2223
## TinyGo examples
2324

examples/NTP Client/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# NTP Client
2+
3+
This exampple shows how to get current date and time from NTP server.
4+
5+
TODO code
6+
7+
## Sources
8+
9+
[ESP32 NTP Client-Server: Get Date and Time (Arduino IDE)](https://randomnerdtutorials.com/esp32-date-time-ntp-client-server-arduino/)
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <WiFi.h>
2+
#include "time.h"
3+
4+
const char* ssid = "YOUR_SSID";
5+
const char* password = "YOUR_PASSWORD";
6+
const long gmtOffset_sec = 3600;
7+
const int daylightOffset_sec = 3600;
8+
const char* ntp_server = "pool.ntp.org";
9+
10+
void setup() {
11+
Serial.begin(9600);
12+
WiFi.begin(ssid, password);
13+
while (WiFi.status() != WL_CONNECTED) {
14+
delay(500);
15+
Serial.print(".");
16+
}
17+
configTime(gmtOffset_sec, daylightOffset_sec, ntp_server);
18+
}
19+
20+
void loop() {
21+
struct tm timeinfo;
22+
if (getLocalTime(&timeinfo)) {
23+
Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
24+
}
25+
delay(1000);
26+
}

0 commit comments

Comments
 (0)