Skip to content

Commit

Permalink
improve esp8266 ntp sync time
Browse files Browse the repository at this point in the history
  • Loading branch information
alvarolb committed Apr 25, 2022
1 parent b4a7b13 commit 180b301
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions src/ThingerESP8266.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
#ifndef THINGER_ESP8266_H
#define THINGER_ESP8266_H

#ifndef THINGER_NTP_SERVER
#define THINGER_NTP_SERVER "pool.ntp.org"
#endif

#ifndef THINGER_NTP_TIMEOUT
#define THINGER_NTP_TIMEOUT 30000
#endif

#include <ESP8266WiFi.h>
#include <time.h>
#include "ThingerWifi.h"
Expand Down Expand Up @@ -54,29 +62,36 @@ class ThingerESP8266 : public ThingerWifiClient<WiFiClient>{
protected:

#ifndef THINGER_INSECURE_SSL
bool setClock() {
configTime(0, 0, "pool.ntp.org", "time.nist.gov");
THINGER_DEBUG("NTP_SYN", "Waiting for NTP time sync...");

void debug_clock(){
#ifdef _DEBUG_
time_t now = time(nullptr);
char* time = ctime(&now);
time[strlen(time)-1]=0;
THINGER_DEBUG_VALUE("NTP_SYN", "Current time (UTC): ", time);
#endif
}

bool set_clock() {
if(time(nullptr) > 8 * 3600 * 2){
debug_clock();
return true;
}

THINGER_DEBUG("NTP_SYN", "Waiting for NTP time sync from: " THINGER_NTP_SERVER);
configTime(0, 0, THINGER_NTP_SERVER);
unsigned long ntp_timeout = millis();
while (time(nullptr) < 8 * 3600 * 2) {
if(millis() - ntp_timeout > 30000){
if(millis() - ntp_timeout > THINGER_NTP_TIMEOUT){
THINGER_DEBUG("NTP_SYN", "Cannot sync time!");
return false;
}
delay(500);
}
#ifdef _DEBUG_
time_t now = time(nullptr);
char* time = ctime(&now);
time[strlen(time)-1]=0;
THINGER_DEBUG_VALUE("NTP_SYN", "Current time (UTC): ", time);
#endif
debug_clock();
return true;
}

bool connect_network() override{
return ThingerWifiClient<WiFiClientSecure>::connect_network() && setClock();
}
#endif

bool connect_socket() override{
Expand All @@ -87,6 +102,7 @@ class ThingerESP8266 : public ThingerWifiClient<WiFiClient>{
THINGER_DEBUG("SSL/TLS", "Warning: TLS/SSL certificate will not be checked!")
#else
client_.setTrustAnchors(&x509);
if(!set_clock()) return false;
#endif
return client_.connect(get_host(), THINGER_SSL_PORT);
}
Expand Down

0 comments on commit 180b301

Please sign in to comment.