#include "Arduino.h"
#include "WiFi.h"
#include "AsyncUDP.h"
#define secs70 2208988800UL
#define NTP_PACKET_SIZE 48
#define UPDATE_PERIOD 10 //каждый 10 сек корректируем время
unsigned long lastUpdate = 0;
long gmtOffset = 3600 * 9; //9 - мой часовой пояс
unsigned long offsetTime = 0;
unsigned long updateT = 0;
int retryUpd = 0;
time_t now; //системное время на устройстве
const char* ssid = "ssid";
const char* password = "pass";
AsyncUDP udp;
void setup()
{
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
if (WiFi.waitForConnectResult() != WL_CONNECTED) {
Serial.println("WiFi Failed");
while (1) {
delay(1000);
}
}
now = millis() / 1000;
if (udp.connect(IPAddress(162, 159, 200, 123), 123)) //time.cloudflare.com......pool.ntp.org 0-й сервер (197,84,150,123)/*
{
Serial.println("UDP connected");
udp.onPacket([](AsyncUDPPacket packet)
{
unsigned long highWord = word(packet.data()[40], packet.data()[41]);
unsigned long lowWord = word(packet.data()[42], packet.data()[43]);
unsigned long secs1900 = highWord << 16 | lowWord;
unsigned long current = secs1900 - secs70 + gmtOffset;
offsetTime = (time_t)(current);
Serial.println("TIME CORRECTION");
});
}
}
void printTime()
{
tm tm;
localtime_r(&now, &tm);
char buff[30];
strftime(buff, 256, "%Y-%m-%d %H:%M:%S", &tm);
Serial.println(buff);
}
void update() {
byte buff[NTP_PACKET_SIZE]; //48
memset(buff, 0, NTP_PACKET_SIZE);
buff[0] = 0b11100011;
buff[1] = 0;
buff[2] = 6;
buff[3] = 0xEC;
buff[12] = 49;
buff[13] = 0x4E;
buff[14] = 49;
buff[15] = 52;
if (udp.connected())
udp.write(buff, NTP_PACKET_SIZE);
}
void loop()
{
now = offsetTime + (millis() - updateT) / 1000;
if (millis() - lastUpdate > 1000)
{
Serial.print("Now: ");
printTime();
lastUpdate = millis();
retryUpd++;
if (retryUpd >= UPDATE_PERIOD)//~каждые 10 секунд корректировка асинхронная
{
updateT = millis();
update();
retryUpd = 0;
}
}
}