37 lines
930 B
C++
37 lines
930 B
C++
#ifndef TIMESYNC_HPP
|
|
#define TIMESYNC_HPP
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "Arduino.h"
|
|
#include "WiFi.h"
|
|
#include "time.h"
|
|
|
|
class TimeSync {
|
|
public:
|
|
static void Sync() {
|
|
const char* ssid = "ConnectonRefused";
|
|
const char* password = "retryconnection";
|
|
|
|
const char* ntpServer = "pool.ntp.org";
|
|
WiFi.begin(ssid, password);
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("WiFi connected.");
|
|
configTime(0, 0, ntpServer);
|
|
delay(2000);
|
|
WiFi.disconnect();
|
|
}
|
|
|
|
static uint64_t GetTime() {
|
|
struct timeval tv;
|
|
gettimeofday(&tv, NULL); // gets time in seconds and microseconds
|
|
uint64_t epochMillis = (uint64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
|
|
return epochMillis;
|
|
}
|
|
};
|
|
|
|
#endif |