diff --git a/wled00/data/settings_time.htm b/wled00/data/settings_time.htm index b6cf00f9..8b131f19 100644 --- a/wled00/data/settings_time.htm +++ b/wled00/data/settings_time.htm @@ -118,7 +118,7 @@
UTC offset: seconds (max. 18 hours)
Current local time is unknown.
- Longitude: Latitude: + Longitude: Latitude:

Clock

Clock Overlay: Latitude:

Clock

Clock Overlay:
First LED: Last LED: 11) hr -= 12; if (hr == 0) hr = 12; } - sprintf(out,"%i-%i-%i, %02d:%02d:%02d",year(localTime), month(localTime), day(localTime), hr, minute(localTime), second(localTime)); + sprintf_P(out,PSTR("%i-%i-%i, %02d:%02d:%02d"),year(localTime), month(localTime), day(localTime), hr, minute(localTime), second(localTime)); if (useAMPM) { strcat(out,(hour(localTime) > 11)? " PM":" AM"); @@ -266,7 +266,7 @@ void checkTimers() lastTimerMinute = minute(localTime); // calculate sunrise and sunset at midnight (if longitude and latitude are set) - if (((int)longitude || (int)latitude) && ((!hour(localTime) && !minute(localTime)) || !sunrise)) { + 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; @@ -275,16 +275,26 @@ void checkTimers() tim_0.tm_isdst = 0; int minUTC = getSunriseUTC(year(localTime), month(localTime), day(localTime), latitude, longitude); - 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)); + 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); - 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)); + 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; @@ -320,7 +330,7 @@ void checkTimers() } } -#define ZENITH -.83 +#define ZENITH -0.83 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); @@ -336,10 +346,10 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse float M = (0.9856 * t) - 3.289; //4. calculate the Sun's true longitude - float L = fmod(M + (1.916 * sin((PI/180)*M)) + (0.020 * sin(2 *(PI/180) * M)) + 282.634,360.0); + 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 - float RA = fmod(180/PI*atan(0.91764 * tan((PI/180)*L)),360.0); + 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; @@ -347,28 +357,26 @@ int getSunriseUTC(int year, int month, int day, float lat, float lon, bool sunse RA = RA + (Lquadrant - RAquadrant); //5c. right ascension value needs to be converted into hours - RA = RA / 15; + RA /= 15.; //6. calculate the Sun's declination - float sinDec = 0.39782 * sin((PI/180)*L); + float sinDec = 0.39782 * sin(DEG_TO_RAD*L); float cosDec = cos(asin(sinDec)); //7a. calculate the Sun's local hour angle - float cosH = (sin((PI/180)*ZENITH) - (sinDec * sin((PI/180)*lat))) / (cosDec * cos((PI/180)*lat)); - /* - if (cosH > 1) the sun never rises on this location (on the specified date) - if (cosH < -1) the sun never sets on this location (on the specified date) - */ + 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 - float H = sunset ? (180/PI)*acos(cosH) : 360 - (180/PI)*acos(cosH); - H = H / 15; + 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 - float UT = fmod(T - lngHour,24.0); + float UT = fmod(T - lngHour, 24.0); // return in minutes from midnight return UT*60; diff --git a/wled00/wled.h b/wled00/wled.h index 1c950a25..9b4151b3 100644 --- a/wled00/wled.h +++ b/wled00/wled.h @@ -8,7 +8,7 @@ */ // version code in format yymmddb (b = daily build) -#define VERSION 2103070 +#define VERSION 2103071 //uncomment this if you have a "my_config.h" file you'd like to use //#define WLED_USE_MY_CONFIG