Skip to content

Commit 7bac3c5

Browse files
committed
Use sunset api
1 parent 54bbaf1 commit 7bac3c5

File tree

4 files changed

+38
-74
lines changed

4 files changed

+38
-74
lines changed

Config.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ class ConfigClass
3232
/// Sunset duration in minutes
3333
uint16_t sunsetDuration = 0;
3434
/// Sunset time hour
35-
uint8_t sunsetHour = 0;
35+
int8_t sunsetHour = 0;
3636
/// Sunset time minute
37-
uint8_t sunsetMinute = 0;
37+
int8_t sunsetMinute = 0;
3838
/// Sunset time offset in minutes relative to the
3939
/// automatically obtained local sunset time
4040
int16_t sunsetOffset = 0;

Fade.cpp

Lines changed: 33 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
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;
1819
Ticker fadeTicker;
1920
Ticker hasBeenStartedResetTicker;
2021
bool hasBeenStarted = false;
21-
bool hasSunsetTime = false;
2222
ConfigClass *config;
2323

2424
void initialize()
2525
{
2626
configTime(Config.timeZone * 3600, Config.summerTime * 3600, "pool.ntp.org", "time.nist.gov");
27+
getSunsetTime();
2728
}
2829

2930
void 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

Fade.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ void begin(FadeMode fadeMode);
2424
void stop();
2525
void tick();
2626
void initialize();
27-
void getSunset(uint16_t d, float Lat, float Long);
28-
float rad(float deg);
27+
void getSunsetTime();
2928

3029
} // namespace Fade

FastLEDManager.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#include <ESPEssentials.h>
88

9-
#define NUM_LEDS 100
9+
#define NUM_LEDS 6
1010
#define LIGHTSTRIP_PIN 5
1111

1212
template <uint8_t DATA_PIN, EOrder RGB_ORDER = RGB, int WAIT_TIME = 5>
@@ -19,7 +19,7 @@ void setup()
1919
initESPEssentials("Lightstrip");
2020

2121
FastLEDManager.initialize(NUM_LEDS);
22-
FastLEDManager.addLeds<WS2812B_noflicker, LIGHTSTRIP_PIN>(FastLEDManager.hardwareLeds, NUM_LEDS);
22+
FastLEDManager.addLeds<WS2812B_noflicker, LIGHTSTRIP_PIN, GRB>(FastLEDManager.hardwareLeds, NUM_LEDS);
2323
FastLEDManager.registerAnimation(new Color("Color"));
2424
FastLEDManager.registerAnimation(new RbWave("RB Wave"));
2525
FastLEDManager.registerAnimation(new RgbWave("RGB Wave"));

0 commit comments

Comments
 (0)