-
Notifications
You must be signed in to change notification settings - Fork 0
/
WiFIforrealtimedata.ino
46 lines (36 loc) · 1.05 KB
/
WiFIforrealtimedata.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
45
46
#include <WiFi.h>
#include <WiFiUdp.h>
#include <time.h>
// Replace with your network credentials
const char* ssid = "your_SSID";
const char* password = "your_PASSWORD";
// NTP server details
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 3600;
const int daylightOffset_sec = 0;
// Create a UDP instance to communicate with the NTP server
WiFiUDP udp;
void setup() {
Serial.begin(115200);
Serial.println("Connecting to WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting...");
}
Serial.println("Connected to WiFi");
// Set the time zone and initiate the UDP communication
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
udp.begin(2390);
}
void loop() {
time_t now = time(nullptr);
if (now > 0) {
// The time has been fetched successfully
Serial.println(ctime(&now));
} else {
// The time has not been fetched yet
Serial.println("Waiting for time sync...");
}
delay(1000);
}