-
Notifications
You must be signed in to change notification settings - Fork 10
/
esp32_ntp.ino
44 lines (38 loc) · 1 KB
/
esp32_ntp.ino
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
#include <WiFi.h>
#define NTP_SERVER "ip_ntp_server"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
void printLocalTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Connection Err");
return;
}
// Ajustar o deslocamento horário para UTC -3 (AST/BRT)
timeinfo.tm_hour -= 3;
if (timeinfo.tm_hour < 0) {
timeinfo.tm_hour += 24; // Garantir que o valor de hora seja positivo
}
Serial.println(&timeinfo, "%H:%M:%S");
Serial.println(&timeinfo, "%d/%m/%Y %Z");
}
void setup() {
Serial.begin(115200);
Serial.println("Connecting to ");
Serial.print("WiFi ");
WiFi.begin("ssid", "password", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("Online");
Serial.println("Updating time...");
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
void loop() {
printLocalTime();
delay(250);
}