-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
NTP_Time_Date
143 lines (95 loc) · 3.75 KB
/
NTP_Time_Date
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// hello
// https://oshwlab.com/l.dijkman/esp32-dev-kit-38-pin-to-spi-touch-tft
// https://github.com/ldijkman/ART-ESP32-Touch-TFT-Thermostat
// http://www.Arduino.TK
//
// https://m.facebook.com/groups/2643123052617990
//
// GNU General Public License,
// which basically means that you may freely copy, change, and distribute it,
// but you may not impose any restrictions on further distribution,
// and you must make the source code available.
//
// standalone program for test
#include <NTPClient.h> // https://github.com/arduino-libraries/NTPClient
#include <WiFi.h> // for WiFi shield
#include <WiFiUdp.h>
#include <TimeLib.h> // https://github.com/PaulStoffregen/Time
const char *ssid = "Bangert-30-Andijk";
const char *password = "password";
int last_second = 0, second_ = 0, minute_ = 0, hour_ = 0, day_ = 0, month_ = 0, year_ = 0;
const char *monthname[13] = {"null", "Januari", "Februari", "March", "April", "May", "Juni",
"Juli", "Augustus", "September", "October", "November", "December"
};
char dayname[8][12] = {"null", "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "};
WiFiUDP ntpUDP;
// You can specify the time server pool and the offset, (in seconds)
// additionaly you can specify the update interval (in milliseconds).
//NTPClient timeClient(ntpUDP, "europe.pool.ntp.org", 3600, 24*60*60*1000); // 3600 sec = 1 hour offset and update every 24 hours
NTPClient timeClient(ntpUDP, "139.162.149.127", 3600, 24*60*60*1000); // 3600 sec = 1 hour offset and update every 24 hours
//1europe.pool.ntp.orgurope.pool.ntp.org./139.162.149.127
//2europe.pool.ntp.orgurope.pool.ntp.org./91.210.59.156
//3europe.pool.ntp.orgurope.pool.ntp.org./173.212.196.208
//4europe.pool.ntp.orgurope.pool.ntp.org./77.68.139.83
void setup(){
Serial.begin(115200);
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {
unsigned long unix_epoch = timeClient.getEpochTime(); // Get Unix epoch time from the NTP server
second_ = second(unix_epoch);
if (last_second != second_) {
if(!timeClient.update()){ // NTP time
Serial.print("problem at line 472 time update ");
}
Serial.println(timeClient.getFormattedTime());
Serial.println(timeClient.getDay());
Serial.println(timeClient.getEpochTime());
minute_ = minute(unix_epoch);
hour_ = hour(unix_epoch);
day_ = day(unix_epoch);
month_ = month(unix_epoch);
year_ = year(unix_epoch);
// Send time and date to serial monitor
Serial.print(hour_);
Serial.print(":");
Serial.print(minute_);
Serial.print(":");
Serial.println(second_);
Serial.print(day_);
Serial.print(" ");
Serial.print(month_);
Serial.print(" ");
Serial.println(year_);
Serial.print("day number of the week ");
Serial.println(weekday(unix_epoch));
Serial.print(hour_);
Serial.print(":");
if (minute_ < 10)Serial.print("0");
Serial.print(minute_);
Serial.print(":");
if (second_ < 10)Serial.print("0");
Serial.print(second_);
Serial.print(" ");
Serial.print(day(unix_epoch));
Serial.print(" ");
Serial.print(month(unix_epoch));
Serial.print(" ");
Serial.print(year(unix_epoch));
Serial.print(" ");
Serial.print(dayname[weekday(unix_epoch)]);
Serial.print(" ");
Serial.print(day(unix_epoch));
Serial.print(" ");
Serial.print(monthname[month(unix_epoch)]);
Serial.print(" ");
Serial.print(year(unix_epoch));
Serial.print(" ");
last_second = second_;
}
}