-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimplesntp_esp32.ino
187 lines (135 loc) · 4.64 KB
/
simplesntp_esp32.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#undef SNTP_MAIN
//#include <wificredentials.h>
// in wificredentials the following are define:
// char* ntpServers[] = { "nl.pool.ntp.org", "be.pool.ntp.org", "de.pool.ntp.org"};
// const char* ntpTimezone = "CET-1CEST,M3.5.0/2,M10.5.0/3";
// const char* wifiSsid[] = {"yourssid", "yourotherssid","yourmothersssi"};
// const char* wifiPassword[] = ("yourWiFipassword","passwordOfyourOtherssid","yourmotherssidspassword"} ;
#include "WiFi.h"
#include "time.h"
#include "lwip/err.h"
#include "lwip/apps/sntp.h"
/*----------------------------------------------------------------*/
int time_to_jurassic(void )
{
int result;
time_t new_time;
struct tm trex;
struct timeval ptero;
trex.tm_sec =0;
trex.tm_min =0;
trex.tm_hour=0;
trex.tm_mday=30;
trex.tm_mon =8;
trex.tm_year=1993-1900; // set time to release date of Jurassic Park
new_time=mktime(&trex);
ptero.tv_sec =new_time;
ptero.tv_usec=0;
result=settimeofday(&ptero,NULL);
return result;
}
//----------------------------------------------------------------
// workaround for Arduino and maybe IDF only using 1 ntpserver
void restart_sntp(){
static int first_server = 0;
int server = first_server;
log_d("sntp server[0] = %s", ntpServers[ server] );
sntp_stop();
sntp_setoperatingmode(SNTP_OPMODE_POLL);
for( int i = 0; i < 3; ++i ){
sntp_setservername( i , (char *)ntpServers[ server]);
++server;
if ( server > 2 ) server = 0;
}
first_server++;
if ( first_server > 2 ) {
tft_message( "No NTP time server could be reached.","Will restart in 10 seconds");
log_e("Restart. No server could be found");
delay(10000);
ESP.restart();
}
sntp_init();
}
/*--------------------------------------------------------------*/
void ntp_waitforsync(){
time_t rawt;
struct tm *tinfo;
struct tm linfo, ginfo;
int y, count;
// workaround for Arduino and maybe IDF only using 1 ntpserver
for(count = 0;; ++count){
if ( count > 2 ){
restart_sntp();
delay(10);
count = 0;
}
time( &rawt );
tinfo = localtime( &rawt );
Serial.printf( "Waiting for ntp sync, time is now " );
Serial.print(ctime(&rawt));
y = tinfo->tm_year + 1900;
if ( y > 2000 ) break;
delay(500);
}
gmtime_r( &rawt, &ginfo);
localtime_r( &rawt, &linfo);
//Serial.printf(" localtime hour %d\n", linfo.tm_hour);
//Serial.printf(" gmtime hour %d\n", ginfo.tm_hour);
time_t g = mktime(&ginfo);
time_t l = mktime(&linfo);
double offsetSeconds = difftime( l, g );
int offsetHours = (int)offsetSeconds /(60*60);
//Assuming DST is always + 1 hour, which apparently is not always true.
Serial.printf("Timezone offset is %d hour%s, %s, current offset is %d hour%s\n\n",
offsetHours,
(offsetHours>1 || offsetHours < -1)?"s":"",
linfo.tm_isdst?"DST in effect": "DST not in effect",
linfo.tm_isdst?offsetHours+1:offsetHours,
( (offsetHours+1)>1 || (offsetHours+1) < -1)?"s":"");
}
/*--------------------------------------------------------------*/
void ntp_setup(bool waitforsync){
// sometimes the esp boots with a time in the future. This defeats the
// test. The settimeofday_cb function is not available on the ESP32 AFAIK
if ( waitforsync && time_to_jurassic() ) Serial.println( "Error setting time to long long ago");
// set timezone
setenv( "TZ" , ntpTimezone, 1);
tzset();
// configTime on the ESP32 does not honor the TZ env, unlike the ESP8266
restart_sntp();
// A new NTP request will be done every hour (hopefully, to be verified
// with tcpdump one day.
if( waitforsync )ntp_waitforsync();
}
/*----------------------------------------------------------------*/
void tellTime(){
time_t now;
now = time(nullptr);
Serial.printf(" localtime : %s", asctime( localtime(&now) ) );
Serial.printf(" gmtime : %s\n", asctime( gmtime(&now) ) );
}
/*----------------------------------------------------------------*/
#ifdef SNTP_MAIN
void setup() {
Serial.begin(115200);
Serial.printf("Connecting to %s ", ssid);
WiFi.setHostname("simplesntp_esp32");
WiFi.begin(wifiSsid, wifiPassword);
for (int wifipoll = 0; WiFi.status() != WL_CONNECTED; ++wifipoll) {
delay(500);
Serial.print(".");
if ( wifipoll%10 == 0 ){
WiFi.begin(wifiSsid, wifiPassword);
}
}
Serial.println(" CONNECTED ");
Serial.print("IP address = ");
Serial.println(WiFi.localIP());
ntp_setup( true );
}
void loop() {
delay(50000);
tellTime();
// put your main code here, to run repeatedly:
}
#endif