-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal time get in C++
60 lines (51 loc) · 1.35 KB
/
local time get in C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <ESP8266WiFi.h>
#include <time.h>
// Replace with your network credentials
const char* ssid = "yourSSID";
const char* password = "yourPASSWORD";
// Set the time (year, month, day, hour, minute, second) in UTC
const tm timeInfo = {
2024 - 1900, // year
1 - 1, // month (0 - 11)
22, // day
12, // hour
0, // minute
0 // second
};
void printLocalTime() {
time_t now = time(nullptr);
struct tm *timeinfo;
timeinfo = localtime(&now);
Serial.print("Current time: ");
Serial.printf("%04d-%02d-%02d %02d:%02d:%02d\n",
timeinfo->tm_year + 1900,
timeinfo->tm_mon + 1,
timeinfo->tm_mday,
timeinfo->tm_hour,
timeinfo->tm_min,
timeinfo->tm_sec);
}
void setup() {
// Start serial communication
Serial.begin(115200);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
Serial.print('.');
}
Serial.println();
// Set the system time
if (!sntp_set_time(0)) {
Serial.println("Failed to set time");
} else {
Serial.println("Time set successfully");
}
}
void loop() {
// Print the current time
printLocalTime();
// Add your code here to perform actions based on the current time
// Delay for 1 second
delay(1000);
}