44#include " SerialOut.h"
55
66#include < ArduinoJson.h>
7+ #include < ESP8266HTTPClient.h>
78#include < ESPEssentials.h>
89#include < Ticker.h>
910#include < time.h>
@@ -18,12 +19,12 @@ uint16_t sunsetMaximumBrightness = 0;
1819Ticker fadeTicker;
1920Ticker hasBeenStartedResetTicker;
2021bool hasBeenStarted = false ;
21- bool hasSunsetTime = false ;
2222ConfigClass *config;
2323
2424void initialize ()
2525{
2626 configTime (Config.timeZone * 3600 , Config.summerTime * 3600 , " pool.ntp.org" , " time.nist.gov" );
27+ getSunsetTime ();
2728}
2829
2930void handle ()
@@ -38,13 +39,6 @@ void handle()
3839 return ;
3940 struct tm *now = gmtime (&n);
4041
41- // Get sunset time if not yet done
42- if (!hasSunsetTime && now->tm_year != 70 )
43- {
44- getSunset (now->tm_yday , Config.latitude , Config.longitude );
45- hasSunsetTime = true ;
46- }
47-
4842 // Check for alarm
4943 if (Config.alarmEnabled && now->tm_hour == Config.alarmHour && now->tm_min == Config.alarmMinute )
5044 {
@@ -92,14 +86,14 @@ void begin(FadeMode fadeMode)
9286 {
9387 FastLEDManager.begin (FastLEDManager.getAnimation (Config.alarmAnimation ));
9488 fadeTicker.attach_ms (Config.alarmDuration * 60 * 1000 / 1024 , tick);
95- PRINTLN (" [Fade ] Start fade 'Alarm'" );
89+ PRINTLN (" [FastLEDManager ] Start fade 'Alarm'" );
9690 }
9791 else if (fadeMode == Fade::FadeMode::SUNSET)
9892 {
9993 FastLEDManager.begin (FastLEDManager.getAnimation (Config.sunsetAnimation ));
10094 sunsetMaximumBrightness = FastLEDManager.brightness10 ;
10195 fadeTicker.attach_ms (Config.sunsetDuration * 60 * 1000 / sunsetMaximumBrightness, tick);
102- PRINTLN (" [Fade ] Start fade 'Sunset'" );
96+ PRINTLN (" [FastLEDManager ] Start fade 'Sunset'" );
10397 }
10498}
10599
@@ -119,12 +113,12 @@ void tick()
119113 if (Config.postAlarmAnimation != Config.alarmAnimation )
120114 FastLEDManager.begin (FastLEDManager.getAnimation (Config.postAlarmAnimation ));
121115 fadeTicker.detach ();
122- PRINTLN (" [Fade ] End fade 'Alarm'" );
116+ PRINTLN (" [FastLEDManager ] End fade 'Alarm'" );
123117 }
124118 else if (currentFade == Fade::FadeMode::SUNSET && fadeBrightness == sunsetMaximumBrightness)
125119 {
126120 fadeTicker.detach ();
127- PRINTLN (" [Fade ] End fade 'Sunset'" );
121+ PRINTLN (" [FastLEDManager ] End fade 'Sunset'" );
128122 }
129123 else
130124 {
@@ -133,68 +127,39 @@ void tick()
133127 else
134128 fadeTicker.detach ();
135129
136- PRINTLN (" [Fade ] Fade brightness: " + String (fadeBrightness));
130+ PRINTLN (" [FastLEDManager ] Fade brightness: " + String (fadeBrightness));
137131 }
138132
139133 FastLEDManager.brightness10 = fadeBrightness;
140134}
141135
142- float rad ( float deg )
136+ void getSunsetTime ( )
143137{
144- return PI * deg / 180 ;
145- }
146-
147- // https://quantitative-ecology.blogspot.com/2007/10/approximate-sunrise-and-sunset-times.html
148- void getSunset (uint16_t d, float Lat, float Long)
149- {
150- // This function is copied from:
151- // Teets, D.A. 2003. Predicting sunrise and sunset times.
152- // The College Mathematics Journal 34(4):317-321.
153-
154- // At the default location the estimates of sunrise and sunset are within
155- // seven minutes of the correct times (http://aa.usno.navy.mil/data/docs/RS_OneYear.php)
156- // with a mean of 2.4 minutes error.
157-
158- // Radius of the earth (km)
159- float R = 6378 ;
160-
161- // Radians between the xy-plane and the ecliptic plane
162- float epsilon = rad (23.45 );
163-
164- // Convert observer's latitude to radians
165- float L = rad (Lat);
166-
167- // Calculate offset of sunrise based on longitude (min)
168- // If Long is negative, then the mod represents degrees West of
169- // a standard time meridian, so timing of sunrise and sunset should
170- // be made later.
171- float timezone = -4 * (abs (Long) % 15 ) * (Long >= 0 ? 1 : -1 );
172-
173- // The earth's mean distance from the sun (km)
174- float r = 149598000 ;
175-
176- float theta = 2 * PI / 365.25 * (d - 80 );
177-
178- float zs = r * sin (theta) * sin (epsilon);
179- float rp = sqrt (r * r - zs * zs);
180-
181- float t0 = 1440 / (2 * PI) * acos ((R - zs * sin (L)) / (rp * cos (L)));
182-
183- // A kludge adjustment for the radius of the sun
184- float that = t0 + 5 ;
185-
186- // Adjust "noon" for the fact that the earth's orbit is not circular:
187- float n = 720 - 10 * sin (4 * PI * (d - 80 ) / 365.25 ) + 8 * sin (2 * PI * d / 365.25 );
188-
189- // Now sunrise and sunset are:
190- // float sunrise = (n - that + timezone) / 60;
191- float sunset = (n + that + timezone) / 60 ;
192-
193- Config.sunsetHour = (int )floor (sunset) % 24 ;
194- Config.sunsetMinute = (sunset - floor (sunset)) * 60 ;
195- Config.save ();
196-
197- PRINTLN (" [Sunset] Got sunset time: " + String (Config.sunsetHour ) + " :" + String (Config.sunsetMinute ));
138+ PRINT (" [FastLEDManager] Getting sunset time..." );
139+
140+ WiFiClient client;
141+ HTTPClient http;
142+ String url = " http://api.sunrise-sunset.org/json?lat=" + String (Config.latitude ) + " &lng=" + String (Config.longitude ) + " &date=today&formatted=0" ;
143+ http.begin (client, url);
144+ String payload = " " ;
145+ if (http.GET () > 0 )
146+ payload = http.getString ();
147+ http.end ();
148+
149+ DynamicJsonDocument doc (2048 );
150+ deserializeJson (doc, payload);
151+ if (doc.containsKey (" results" ) && doc[" results" ].containsKey (" sunset" ))
152+ {
153+ String sunset = doc[" results" ][" sunset" ].as <String>();
154+ Config.sunsetHour = (sunset.substring (11 , 13 ).toInt () + Config.timeZone + Config.summerTime + 24 ) % 24 ;
155+ Config.sunsetMinute = sunset.substring (14 , 16 ).toInt ();
156+ Config.save ();
157+ PRINTLN (" " + String (Config.sunsetHour ) + " :" + String (Config.sunsetMinute ));
158+ }
159+ else
160+ {
161+ PRINTLN (" failed. Using last known time instead." );
162+ }
198163}
199164
200165} // namespace Fade
0 commit comments