WLED/wled00/ntp.cpp

383 lines
12 KiB
C++
Raw Normal View History

#include "src/dependencies/timezone/Timezone.h"
#include "wled.h"
/*
* Acquires time from NTP server
*/
2020-09-20 01:18:31 +02:00
Timezone* tz;
#define TZ_UTC 0
#define TZ_UK 1
#define TZ_EUROPE_CENTRAL 2
#define TZ_EUROPE_EASTERN 3
#define TZ_US_EASTERN 4
#define TZ_US_CENTRAL 5
#define TZ_US_MOUNTAIN 6
#define TZ_US_ARIZONA 7
#define TZ_US_PACIFIC 8
#define TZ_CHINA 9
#define TZ_JAPAN 10
#define TZ_AUSTRALIA_EASTERN 11
#define TZ_NEW_ZEALAND 12
#define TZ_NORTH_KOREA 13
#define TZ_INDIA 14
#define TZ_SASKACHEWAN 15
#define TZ_AUSTRALIA_NORTHERN 16
#define TZ_AUSTRALIA_SOUTHERN 17
#define TZ_HAWAII 18
2020-09-20 01:18:31 +02:00
#define TZ_INIT 255
byte tzCurrent = TZ_INIT; //uninitialized
void updateTimezone() {
delete tz;
TimeChangeRule tcrDaylight = {Last, Sun, Mar, 1, 0}; //UTC
TimeChangeRule tcrStandard = tcrDaylight; //UTC
switch (currentTimezone) {
case TZ_UK : {
tcrDaylight = {Last, Sun, Mar, 1, 60}; //British Summer Time
tcrStandard = {Last, Sun, Oct, 2, 0}; //Standard Time
break;
}
case TZ_EUROPE_CENTRAL : {
tcrDaylight = {Last, Sun, Mar, 2, 120}; //Central European Summer Time
tcrStandard = {Last, Sun, Oct, 3, 60}; //Central European Standard Time
break;
}
case TZ_EUROPE_EASTERN : {
tcrDaylight = {Last, Sun, Mar, 3, 180}; //East European Summer Time
tcrStandard = {Last, Sun, Oct, 4, 120}; //East European Standard Time
break;
}
case TZ_US_EASTERN : {
tcrDaylight = {Second, Sun, Mar, 2, -240}; //EDT = UTC - 4 hours
tcrStandard = {First, Sun, Nov, 2, -300}; //EST = UTC - 5 hours
break;
}
case TZ_US_CENTRAL : {
tcrDaylight = {Second, Sun, Mar, 2, -300}; //CDT = UTC - 5 hours
tcrStandard = {First, Sun, Nov, 2, -360}; //CST = UTC - 6 hours
break;
}
case TZ_US_MOUNTAIN : {
tcrDaylight = {Second, Sun, Mar, 2, -360}; //MDT = UTC - 6 hours
tcrStandard = {First, Sun, Nov, 2, -420}; //MST = UTC - 7 hours
break;
}
case TZ_US_ARIZONA : {
tcrDaylight = {First, Sun, Nov, 2, -420}; //MST = UTC - 7 hours
tcrStandard = {First, Sun, Nov, 2, -420}; //MST = UTC - 7 hours
break;
}
case TZ_US_PACIFIC : {
tcrDaylight = {Second, Sun, Mar, 2, -420}; //PDT = UTC - 7 hours
tcrStandard = {First, Sun, Nov, 2, -480}; //PST = UTC - 8 hours
break;
}
case TZ_CHINA : {
tcrDaylight = {Last, Sun, Mar, 1, 480}; //CST = UTC + 8 hours
tcrStandard = tcrDaylight;
break;
}
case TZ_JAPAN : {
tcrDaylight = {Last, Sun, Mar, 1, 540}; //JST = UTC + 9 hours
tcrStandard = tcrDaylight;
break;
}
case TZ_AUSTRALIA_EASTERN : {
tcrDaylight = {Second, Sun, Oct, 2, 660}; //AEDT = UTC + 11 hours
tcrStandard = {First, Sun, Apr, 3, 600}; //AEST = UTC + 10 hours
break;
}
case TZ_NEW_ZEALAND : {
tcrDaylight = {Second, Sun, Sep, 2, 780}; //NZDT = UTC + 13 hours
tcrStandard = {First, Sun, Apr, 3, 720}; //NZST = UTC + 12 hours
break;
}
case TZ_NORTH_KOREA : {
tcrDaylight = {Last, Sun, Mar, 1, 510}; //Pyongyang Time = UTC + 8.5 hours
tcrStandard = tcrDaylight;
break;
}
case TZ_INDIA : {
tcrDaylight = {Last, Sun, Mar, 1, 330}; //India Standard Time = UTC + 5.5 hours
tcrStandard = tcrDaylight;
break;
}
case TZ_SASKACHEWAN : {
tcrDaylight = {First, Sun, Nov, 2, -360}; //CST = UTC - 6 hours
tcrStandard = tcrDaylight;
break;
}
case TZ_AUSTRALIA_NORTHERN : {
tcrStandard = {First, Sun, Apr, 3, 570}; //ACST = UTC + 9.5 hours
tcrStandard = tcrDaylight;
break;
}
case TZ_AUSTRALIA_SOUTHERN : {
tcrDaylight = {First, Sun, Oct, 2, 630}; //ACDT = UTC + 10.5 hours
tcrStandard = {First, Sun, Apr, 3, 570}; //ACST = UTC + 9.5 hours
break;
}
case TZ_HAWAII : {
tcrDaylight = {Last, Sun, Mar, 1, -600}; //HST = UTC - 10 hours
tcrStandard = tcrDaylight;
break;
}
2020-09-20 01:18:31 +02:00
}
2020-09-20 01:18:31 +02:00
tzCurrent = currentTimezone;
2020-09-20 01:18:31 +02:00
tz = new Timezone(tcrDaylight, tcrStandard);
}
void handleNetworkTime()
{
2019-10-18 13:26:39 +02:00
if (ntpEnabled && ntpConnected && millis() - ntpLastSyncTime > 50000000L && WLED_CONNECTED)
2017-02-07 16:02:27 +01:00
{
if (millis() - ntpPacketSentTime > 10000)
{
sendNTPPacket();
ntpPacketSentTime = millis();
}
if (checkNTPResponse())
{
ntpLastSyncTime = millis();
}
}
}
void sendNTPPacket()
{
if (!ntpServerIP.fromString(ntpServerName)) //see if server is IP or domain
{
#ifdef ESP8266
WiFi.hostByName(ntpServerName, ntpServerIP, 750);
#else
WiFi.hostByName(ntpServerName, ntpServerIP);
#endif
}
2020-09-20 01:18:31 +02:00
DEBUG_PRINTLN(F("send NTP"));
2019-03-11 17:57:06 +01:00
byte pbuf[NTP_PACKET_SIZE];
memset(pbuf, 0, NTP_PACKET_SIZE);
pbuf[0] = 0b11100011; // LI, Version, Mode
pbuf[1] = 0; // Stratum, or type of clock
pbuf[2] = 6; // Polling Interval
pbuf[3] = 0xEC; // Peer Clock Precision
2017-02-07 16:02:27 +01:00
// 8 bytes of zero for Root Delay & Root Dispersion
2019-03-11 17:57:06 +01:00
pbuf[12] = 49;
pbuf[13] = 0x4E;
pbuf[14] = 49;
pbuf[15] = 52;
2017-02-07 16:02:27 +01:00
ntpUdp.beginPacket(ntpServerIP, 123); //NTP requests are to port 123
2019-03-11 17:57:06 +01:00
ntpUdp.write(pbuf, NTP_PACKET_SIZE);
2017-02-07 16:02:27 +01:00
ntpUdp.endPacket();
}
bool checkNTPResponse()
2017-02-07 16:02:27 +01:00
{
int cb = ntpUdp.parsePacket();
if (cb) {
2020-09-20 01:18:31 +02:00
DEBUG_PRINT(F("NTP recv, l="));
2017-02-07 16:02:27 +01:00
DEBUG_PRINTLN(cb);
2019-03-11 17:57:06 +01:00
byte pbuf[NTP_PACKET_SIZE];
ntpUdp.read(pbuf, NTP_PACKET_SIZE); // read the packet into the buffer
2017-02-07 16:02:27 +01:00
2019-03-11 17:57:06 +01:00
unsigned long highWord = word(pbuf[40], pbuf[41]);
unsigned long lowWord = word(pbuf[42], pbuf[43]);
2017-02-07 16:02:27 +01:00
if (highWord == 0 && lowWord == 0) return false;
unsigned long secsSince1900 = highWord << 16 | lowWord;
2020-09-20 01:18:31 +02:00
DEBUG_PRINT(F("Unix time = "));
unsigned long epoch = secsSince1900 - 2208988799UL; //subtract 70 years -1sec (on avg. more precision)
2017-02-07 16:02:27 +01:00
setTime(epoch);
DEBUG_PRINTLN(epoch);
if (countdownTime - now() > 0) countdownOverTriggered = false;
2017-02-07 16:02:27 +01:00
return true;
}
return false;
}
void updateLocalTime()
{
2020-09-20 01:18:31 +02:00
if (currentTimezone != tzCurrent) updateTimezone();
unsigned long tmc = now()+ utcOffsetSecs;
2020-09-20 01:18:31 +02:00
localTime = tz->toLocal(tmc);
}
2019-03-16 02:09:37 +01:00
void getTimeString(char* out)
{
updateLocalTime();
byte hr = hour(localTime);
if (useAMPM)
{
if (hr > 11) hr -= 12;
if (hr == 0) hr = 12;
}
2021-03-07 11:14:16 +01:00
sprintf_P(out,PSTR("%i-%i-%i, %02d:%02d:%02d"),year(localTime), month(localTime), day(localTime), hr, minute(localTime), second(localTime));
2019-03-11 17:57:06 +01:00
if (useAMPM)
{
strcat(out,(hour(localTime) > 11)? " PM":" AM");
2019-03-11 17:57:06 +01:00
}
}
void setCountdown()
{
2020-09-20 01:18:31 +02:00
if (currentTimezone != tzCurrent) updateTimezone();
countdownTime = tz->toUTC(getUnixTime(countdownHour, countdownMin, countdownSec, countdownDay, countdownMonth, countdownYear));
if (countdownTime - now() > 0) countdownOverTriggered = false;
}
2018-02-20 22:29:48 +01:00
//returns true if countdown just over
bool checkCountdown()
{
2020-01-01 01:04:54 +01:00
unsigned long n = now();
if (countdownMode) localTime = countdownTime - n + utcOffsetSecs;
2020-01-01 01:04:54 +01:00
if (n > countdownTime) {
if (countdownMode) localTime = n - countdownTime + utcOffsetSecs;
2020-01-01 01:04:54 +01:00
if (!countdownOverTriggered)
{
2021-01-09 00:35:48 +01:00
if (macroCountdown != 0) applyPreset(macroCountdown);
2020-01-01 01:04:54 +01:00
countdownOverTriggered = true;
return true;
}
2018-02-20 22:29:48 +01:00
}
return false;
}
2019-01-31 00:09:44 +01:00
byte weekdayMondayFirst()
{
byte wd = weekday(localTime) -1;
2019-01-31 00:09:44 +01:00
if (wd == 0) wd = 7;
return wd;
}
void checkTimers()
{
if (lastTimerMinute != minute(localTime)) //only check once a new minute begins
{
lastTimerMinute = minute(localTime);
// calculate sunrise and sunset at midnight (if longitude and latitude are set)
2021-03-07 11:14:16 +01:00
if (((int)longitude || (int)latitude) && ((!hour(localTime) && !minute(localTime)) || (!sunrise && !sunset))) {
struct tm tim_0;
tim_0.tm_year = year(localTime)-1900;
tim_0.tm_mon = month(localTime)-1;
tim_0.tm_mday = day(localTime);
tim_0.tm_sec = 0;
tim_0.tm_isdst = 0;
int minUTC = getSunriseUTC(year(localTime), month(localTime), day(localTime), latitude, longitude);
2021-03-07 11:14:16 +01:00
if (minUTC) {
// there is a sunrise
tim_0.tm_hour = minUTC / 60;
tim_0.tm_min = minUTC % 60;
sunrise = tz->toLocal(mktime(&tim_0) - utcOffsetSecs);
DEBUG_PRINTF("Sunrise: %02d:%02d\n", hour(sunrise), minute(sunrise));
} else {
sunrise = 0;
}
minUTC = getSunriseUTC(year(localTime), month(localTime), day(localTime), latitude, longitude, true);
2021-03-07 11:14:16 +01:00
if (minUTC) {
// there is a sunset
tim_0.tm_hour = minUTC / 60;
tim_0.tm_min = minUTC % 60;
sunset = tz->toLocal(mktime(&tim_0) - utcOffsetSecs);
DEBUG_PRINTF("Sunset: %02d:%02d\n", hour(sunset), minute(sunset));
} else {
sunset = 0;
}
}
if (sunrise && sunset) daytime = difftime(localTime, sunrise) > 0 && difftime(localTime, sunset) < 0;
for (uint8_t i = 0; i < 8; i++)
{
if (timerMacro[i] != 0
&& (timerHours[i] == hour(localTime) || timerHours[i] == 24) //if hour is set to 24, activate every hour
&& timerMinutes[i] == minute(localTime)
2019-01-31 00:09:44 +01:00
&& (timerWeekday[i] & 0x01) //timer is enabled
&& timerWeekday[i] >> weekdayMondayFirst() & 0x01) //timer should activate at current day of week
{
2020-11-08 23:44:10 +01:00
applyPreset(timerMacro[i]);
}
}
// sunrise macro
if (sunrise && timerMacro[8] != 0
&& (hour(sunrise) == hour(localTime))
&& (minute(sunrise) + timerMinutes[8]) == minute(localTime)
&& (timerWeekday[8] & 0x01) //timer is enabled
&& timerWeekday[8] >> weekdayMondayFirst() & 0x01) //timer should activate at current day of week
{
applyPreset(timerMacro[8]);
}
// sunset macro
if (sunset && timerMacro[9] != 0
&& (hour(sunrise) == hour(localTime))
&& (minute(sunrise) + timerMinutes[9]) == minute(localTime)
&& (timerWeekday[9] & 0x01) //timer is enabled
&& timerWeekday[9] >> weekdayMondayFirst() & 0x01) //timer should activate at current day of week
{
applyPreset(timerMacro[9]);
}
}
}
2021-03-05 23:05:09 +01:00
2021-03-07 11:14:16 +01:00
#define ZENITH -0.83
2021-03-07 12:43:13 +01:00
// get sunrise (or sunset) time (in minutes) for a given day at a given geo location
int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunset) {
//1. first calculate the day of the year
float N1 = floor(275 * month / 9);
float N2 = floor((month + 9) / 12);
float N3 = (1 + floor((year - 4 * floor(year / 4) + 2) / 3));
float N = N1 - (N2 * N3) + day - 30;
//2. convert the longitude to hour value and calculate an approximate time
float lngHour = lon / 15.0;
float t = N + (((sunset ? 18 : 6) - lngHour) / 24);
//3. calculate the Sun's mean anomaly
float M = (0.9856 * t) - 3.289;
//4. calculate the Sun's true longitude
2021-03-07 11:14:16 +01:00
float L = fmod(M + (1.916 * sin(DEG_TO_RAD*M)) + (0.020 * sin(2*DEG_TO_RAD*M)) + 282.634, 360.0);
//5a. calculate the Sun's right ascension
2021-03-07 11:14:16 +01:00
float RA = fmod(RAD_TO_DEG*atan(0.91764 * tan(DEG_TO_RAD*L)), 360.0);
//5b. right ascension value needs to be in the same quadrant as L
float Lquadrant = floor( L/90) * 90;
float RAquadrant = floor(RA/90) * 90;
RA = RA + (Lquadrant - RAquadrant);
//5c. right ascension value needs to be converted into hours
2021-03-07 11:14:16 +01:00
RA /= 15.;
//6. calculate the Sun's declination
2021-03-07 11:14:16 +01:00
float sinDec = 0.39782 * sin(DEG_TO_RAD*L);
float cosDec = cos(asin(sinDec));
//7a. calculate the Sun's local hour angle
2021-03-07 11:14:16 +01:00
float cosH = (sin(DEG_TO_RAD*ZENITH) - (sinDec * sin(DEG_TO_RAD*lat))) / (cosDec * cos(DEG_TO_RAD*lat));
if (cosH > 1 && !sunset) return 0; // the sun never rises on this location (on the specified date)
if (cosH < -1 && sunset) return 0; // the sun never sets on this location (on the specified date)
//7b. finish calculating H and convert into hours
2021-03-07 11:14:16 +01:00
float H = sunset ? RAD_TO_DEG*acos(cosH) : 360 - RAD_TO_DEG*acos(cosH);
H /= 15.;
//8. calculate local mean time of rising/setting
float T = H + RA - (0.06571 * t) - 6.622;
//9. adjust back to UTC
2021-03-07 11:14:16 +01:00
float UT = fmod(T - lngHour, 24.0);
2021-03-05 23:05:09 +01:00
// return in minutes from midnight
return UT*60;
2021-03-05 23:05:09 +01:00
}