2016-12-31 00:38:51 +01:00
|
|
|
/*
|
|
|
|
* Acquires time from NTP server
|
|
|
|
*/
|
|
|
|
|
2016-12-29 00:03:58 +01:00
|
|
|
void handleNetworkTime()
|
|
|
|
{
|
2017-04-26 10:13:33 +02:00
|
|
|
if (ntpEnabled && ntpConnected && millis() - ntpLastSyncTime > 50000000L && WiFi.status() == WL_CONNECTED)
|
2017-02-07 16:02:27 +01:00
|
|
|
{
|
|
|
|
if (millis() - ntpPacketSentTime > 10000)
|
|
|
|
{
|
|
|
|
sendNTPPacket();
|
|
|
|
ntpPacketSentTime = millis();
|
|
|
|
}
|
|
|
|
if (checkNTPResponse())
|
|
|
|
{
|
|
|
|
ntpLastSyncTime = millis();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sendNTPPacket()
|
|
|
|
{
|
|
|
|
WiFi.hostByName(ntpServerName, ntpServerIP);
|
2018-01-14 00:53:16 +01:00
|
|
|
DEBUG_PRINTLN("send NTP packet");
|
2017-02-07 16:02:27 +01:00
|
|
|
|
|
|
|
memset(ntpPacketBuffer, 0, NTP_PACKET_SIZE);
|
|
|
|
|
|
|
|
ntpPacketBuffer[0] = 0b11100011; // LI, Version, Mode
|
|
|
|
ntpPacketBuffer[1] = 0; // Stratum, or type of clock
|
|
|
|
ntpPacketBuffer[2] = 6; // Polling Interval
|
|
|
|
ntpPacketBuffer[3] = 0xEC; // Peer Clock Precision
|
|
|
|
// 8 bytes of zero for Root Delay & Root Dispersion
|
|
|
|
ntpPacketBuffer[12] = 49;
|
|
|
|
ntpPacketBuffer[13] = 0x4E;
|
|
|
|
ntpPacketBuffer[14] = 49;
|
|
|
|
ntpPacketBuffer[15] = 52;
|
|
|
|
|
|
|
|
ntpUdp.beginPacket(ntpServerIP, 123); //NTP requests are to port 123
|
|
|
|
ntpUdp.write(ntpPacketBuffer, NTP_PACKET_SIZE);
|
|
|
|
ntpUdp.endPacket();
|
|
|
|
}
|
|
|
|
|
|
|
|
boolean checkNTPResponse()
|
|
|
|
{
|
|
|
|
int cb = ntpUdp.parsePacket();
|
|
|
|
if (cb) {
|
|
|
|
DEBUG_PRINT("packet received, length=");
|
|
|
|
DEBUG_PRINTLN(cb);
|
|
|
|
|
|
|
|
ntpUdp.read(ntpPacketBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
|
|
|
|
|
|
|
|
unsigned long highWord = word(ntpPacketBuffer[40], ntpPacketBuffer[41]);
|
|
|
|
unsigned long lowWord = word(ntpPacketBuffer[42], ntpPacketBuffer[43]);
|
|
|
|
if (highWord == 0 && lowWord == 0) return false;
|
|
|
|
|
|
|
|
unsigned long secsSince1900 = highWord << 16 | lowWord;
|
|
|
|
|
|
|
|
DEBUG_PRINT("Unix time = ");
|
|
|
|
unsigned long epoch = secsSince1900 - seventyYears;
|
|
|
|
setTime(epoch);
|
|
|
|
DEBUG_PRINTLN(epoch);
|
2018-01-14 00:53:16 +01:00
|
|
|
if (countdownTime - now() > 0) countdownOverTriggered = false;
|
2017-02-07 16:02:27 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2016-12-29 00:03:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
String getTimeString()
|
|
|
|
{
|
2016-12-31 00:38:51 +01:00
|
|
|
local = TZ.toLocal(now(), &tcr);
|
2016-12-29 00:03:58 +01:00
|
|
|
String ret = monthStr(month(local));
|
|
|
|
ret = ret + " ";
|
|
|
|
ret = ret + day(local);
|
|
|
|
ret = ret + " ";
|
|
|
|
ret = ret + year(local);
|
|
|
|
ret = ret + ", ";
|
|
|
|
ret = ret + hour(local);
|
|
|
|
ret = ret + ":";
|
|
|
|
if (minute(local) < 10) ret = ret + "0";
|
|
|
|
ret = ret + minute(local);
|
|
|
|
ret = ret + ":";
|
|
|
|
if (second(local) < 10) ret = ret + "0";
|
|
|
|
ret = ret + second(local);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-02-20 22:29:48 +01:00
|
|
|
//returns true if countdown just over
|
|
|
|
bool checkCountdown()
|
|
|
|
{
|
|
|
|
long diff = countdownTime - local;
|
|
|
|
local = abs(diff);
|
|
|
|
if (diff <0 && !countdownOverTriggered)
|
|
|
|
{
|
|
|
|
applyMacro(countdownMacro);
|
|
|
|
countdownOverTriggered = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|