Sunris/sunset detection.

This commit is contained in:
Blaz Kristan 2021-03-05 23:05:09 +01:00
parent 0a1d04495d
commit f74a45a33e
11 changed files with 872 additions and 796 deletions

View File

@ -305,6 +305,8 @@ void deserializeConfig() {
CJSON(currentTimezone, if_ntp[F("tz")]);
CJSON(utcOffsetSecs, if_ntp[F("offset")]);
CJSON(useAMPM, if_ntp[F("ampm")]);
CJSON(longitude, if_ntp[F("ln")]);
CJSON(latitude, if_ntp[F("lt")]);
JsonObject ol = doc[F("ol")];
CJSON(overlayDefault ,ol[F("clock")]); // 0
@ -620,6 +622,8 @@ void serializeConfig() {
if_ntp[F("tz")] = currentTimezone;
if_ntp[F("offset")] = utcOffsetSecs;
if_ntp[F("ampm")] = useAMPM;
if_ntp[F("ln")] = longitude;
if_ntp[F("lt")] = latitude;
JsonObject ol = doc.createNestedObject("ol");
ol[F("clock")] = overlayDefault;

View File

@ -589,7 +589,7 @@ function populateInfo(i)
${urows}
${inforow("Build",i.vid)}
${inforow("Signal strength",i.wifi.signal +"% ("+ i.wifi.rssi, " dBm)")}
${inforow("Uptime",getRuntimeStr(i.uptime))}
${inforow("Uptime",getRuntimeStr(i.uptime)," <span class='icons'>"+(i.isday?"&#xe333;":"&#xe2a2;")+"</span>")}
${inforow("Free heap",heap," kB")}
${inforow("Estimated current",pwru)}
${inforow("Average FPS",i.leds.fps)}

View File

@ -113,7 +113,8 @@
<option value="18">HST (Hawaii)</option>
</select><br>
UTC offset: <input name="UO" type="number" min="-65500" max="65500" required> seconds (max. 18 hours)<br>
Current local time is <span class="times">unknown</span>.
Current local time is <span class="times">unknown</span>.<br>
Longitude: <input name="LN" type="number" min="-180" max="180" step="0.01"> Latitude: <input name="LT" type="number" min="-90" max="90" step="0.01">
<h3>Clock</h3>
Clock Overlay:
<select name="OL" onchange="Cs()">

View File

@ -135,6 +135,7 @@ bool checkCountdown();
void setCountdown();
byte weekdayMondayFirst();
void checkTimers();
bool isDayTime();
//overlay.cpp
void initCronixie();

View File

@ -316,7 +316,9 @@ US-MST/MDT</option><option value="7">US-AZ</option><option value="8">US-PST/PDT
</option><option value="17">ACST/ACDT</option><option value="18">HST (Hawaii)
</option></select><br>UTC offset: <input name="UO" type="number" min="-65500"
max="65500" required> seconds (max. 18 hours)<br>Current local time is <span
class="times">unknown</span>.<h3>Clock</h3>Clock Overlay: <select name="OL"
class="times">unknown</span>.<br>Longitude: <input name="LN" type="number"
min="-180" max="180" step="0.01"> Latitude: <input name="LT" type="number"
min="-90" max="90" step="0.01"><h3>Clock</h3>Clock Overlay: <select name="OL"
onchange="Cs()"><option value="0" id="cn" selected="selected">None</option>
<option value="1" id="ca">Analog Clock</option><option value="2">
Single Digit Clock</option><option value="3" id="cc">Cronixie Clock</option>

File diff suppressed because it is too large Load Diff

View File

@ -509,6 +509,7 @@ void serializeInfo(JsonObject root)
root[F("freeheap")] = ESP.getFreeHeap();
root[F("uptime")] = millis()/1000 + rolloverMillis*4294967;
root[F("isday")] = daytime;
usermods.addToJsonInfo(root);

View File

@ -263,6 +263,11 @@ void checkTimers()
{
if (lastTimerMinute != minute(localTime)) //only check once a new minute begins
{
daytime = isDayTime();
if (prevDaytime != daytime) {
// sunrise or sunset
DEBUG_PRINTLN(daytime?F("Sunrise"):F("Sunset"));
}
lastTimerMinute = minute(localTime);
for (uint8_t i = 0; i < 8; i++)
{
@ -277,3 +282,52 @@ void checkTimers()
}
}
}
/*
* This program calculates solar positions as a function of location, date, and time.
* The equations are from Jean Meeus, Astronomical Algorithms, Willmann-Bell, Inc., Richmond, VA
* (C) 2015, David Brooks, Institute for Earth Science Research and Education.
* http://www.instesre.org/ArduinoUnoSolarCalculations.pdf
*/
//#define DEG_TO_RAD 0.01745329
//#define PI 3.141592654
#define TWOPI 6.28318531
long JulianDate(int year, int month, int day) {
if (month<=2) {
year--; month+=12;
}
int A=year/100;
int B=2-A+A/4;
return (long)(365.25*(year + 4716)) + (int)(30.6001*(month + 1)) + day + B - 1524;
}
bool isDayTime() {
float JD_frac,T,L0,M,C,L_true,GrHrAngle,Obl,RA,Decl,HrAngle,elev;
long JD_whole,JDx;
float Lon = longitude*DEG_TO_RAD;
float Lat = latitude*DEG_TO_RAD;
// calculate elevation of the sun (>0 daytime, <0 nighttime)
JD_whole = JulianDate(year(localTime), month(localTime), day(localTime));
JD_frac = (hour(localTime) + minute(localTime)/60. + second(localTime)/3600.)/24. - .5;
JDx = JD_whole - 2451545;
T = (JDx + JD_frac)/36525.;
L0 = DEG_TO_RAD*fmod(280.46645 + 36000.76983*T, 360);
M = DEG_TO_RAD*fmod(357.5291 + 35999.0503*T, 360);
C = DEG_TO_RAD*((1.9146-0.004847*T)*sin(M) + (0.019993-0.000101*T)*sin(2*M) + 0.00029*sin(3*M));
Obl = DEG_TO_RAD*(23 + 26/60. + 21.448/3600. - 46.815/3600*T);
GrHrAngle = 280.46061837 + (360*JDx)%360 + .98564736629*JDx + 360.98564736629*JD_frac;
GrHrAngle = fmod(GrHrAngle, 360.);
L_true = fmod(C + L0, TWOPI);
RA = atan2(sin(L_true)*cos(Obl), cos(L_true));
Decl = asin(sin(Obl)*sin(L_true));
HrAngle = DEG_TO_RAD*GrHrAngle + Lon - RA;
elev = asin(sin(Lat)*sin(Decl) + cos(Lat)*(cos(Decl)*cos(HrAngle)));
// Azimuth measured eastward from north.
// azimuth = PI+atan2(sin(HrAngle),cos(HrAngle)*sin(Lat)-tan(Decl)*cos(Lat));
return elev > 0.; // if elevation is gt 0 then it is a day
}

View File

@ -294,6 +294,8 @@ void handleSettingsSet(AsyncWebServerRequest *request, byte subPage)
//start ntp if not already connected
if (ntpEnabled && WLED_CONNECTED && !ntpConnected) ntpConnected = ntpUdp.begin(ntpLocalPort);
longitude = request->arg(F("LN")).toFloat();
latitude = request->arg(F("LT")).toFloat();
if (request->hasArg(F("OL"))) {
overlayDefault = request->arg(F("OL")).toInt();

View File

@ -8,7 +8,7 @@
*/
// version code in format yymmddb (b = daily build)
#define VERSION 2103050
#define VERSION 2103051
//uncomment this if you have a "my_config.h" file you'd like to use
//#define WLED_USE_MY_CONFIG
@ -515,6 +515,10 @@ WLED_GLOBAL unsigned long ntpPacketSentTime _INIT(999000000L);
WLED_GLOBAL IPAddress ntpServerIP;
WLED_GLOBAL uint16_t ntpLocalPort _INIT(2390);
WLED_GLOBAL uint16_t rolloverMillis _INIT(0);
WLED_GLOBAL bool daytime _INIT(false);
WLED_GLOBAL bool prevDaytime _INIT(false);
WLED_GLOBAL float longitude _INIT(0.0);
WLED_GLOBAL float latitude _INIT(0.0);
// Temp buffer
WLED_GLOBAL char* obuf;

View File

@ -448,6 +448,10 @@ void getSettingsJS(byte subPage, char* dest)
sappend('i',SET_F("TZ"),currentTimezone);
sappend('v',SET_F("UO"),utcOffsetSecs);
char tm[32];
dtostrf(longitude,5,2,tm);
sappends('s',SET_F("LN"),tm);
dtostrf(latitude,5,2,tm);
sappends('s',SET_F("LT"),tm);
getTimeString(tm);
sappends('m',SET_F("(\"times\")[0]"),tm);
sappend('i',SET_F("OL"),overlayCurrent);