Skip to content

Commit

Permalink
Merge pull request #1507 from stelas/mega
Browse files Browse the repository at this point in the history
Add sunset and sunrise variables...
  • Loading branch information
TD-er committed Jun 17, 2018
2 parents 808ab85 + c51b78b commit c7c46d2
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/ESPEasy-Globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -763,6 +763,8 @@ struct SettingsStruct
//its safe to extend this struct, up to several bytes, default values in config are 0
//look in misc.ino how config.dat is used because also other stuff is stored in it at different offsets.
//TODO: document config.dat somewhere here
float Latitude;
float Longitude;

// FIXME @TD-er: As discussed in #1292, the CRC for the settings is now disabled.
// make sure crc is the last value in the struct
Expand Down
2 changes: 2 additions & 0 deletions src/StringConverter.ino
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,8 @@ void parseSystemVariables(String& s, boolean useURLencode)
SMART_REPL(F("%lcltime_am%"), getDateTimeString_ampm('-',':',' '))
SMART_REPL(F("%uptime%"), String(wdcounter / 2))
SMART_REPL(F("%unixtime%"), String(getUnixTime()))
SMART_REPL(F("%sunrise%"), getSunriseTimeString(':'))
SMART_REPL(F("%sunset%"), getSunsetTimeString(':'))

repl(F("%tskname%"), ExtraTaskSettings.TaskDeviceName, s, useURLencode);
if (s.indexOf(F("%vname")) != -1) {
Expand Down
67 changes: 67 additions & 0 deletions src/TimeESPeasy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,63 @@ uint32_t syncInterval = 3600; // time sync will be attempted after this many se
uint32_t sysTime = 0;
uint32_t prevMillis = 0;
uint32_t nextSyncTime = 0;
timeStruct sunRise;
timeStruct sunSet;

byte PrevMinutes = 0;

float sunDeclination(int doy) {
// Declination of the sun in radians
// Formula 2008 by Arnold(at)Barmettler.com, fit to 20 years of average declinations (2008-2027)
return 0.409526325277017 * sin(0.0169060504029192 * (doy - 80.0856919827619));
}

float diurnalArc(float dec, float lat) {
// Duration of the half sun path in hours (time from sunrise to the highest level in the south)
float rad = 0.0174532925; // = pi/180.0
float height = -50.0 / 60.0 * rad;
float latRad = lat * rad;
return 12.0 * acos((sin(height) - sin(latRad) * sin(dec)) / (cos(latRad) * cos(dec))) / 3.1415926536;
}

float equationOfTime(int doy) {
// Difference between apparent and mean solar time
// Formula 2008 by Arnold(at)Barmettler.com, fit to 20 years of average equation of time (2008-2027)
return -0.170869921174742 * sin(0.0336997028793971 * doy + 0.465419984181394) - 0.129890681040717 * sin(0.0178674832556871 * doy - 0.167936777524864);
}

int dayOfYear(int year, int month, int day) {
// Algorithm borrowed from DateToOrdinal by Ritchie Lawrence, www.commandline.co.uk
int z = 14 - month;
z /= 12;
int y = year + 4800 - z;
int m = month + 12 * z - 3;
int j = 153 * m + 2;
j = j / 5 + day + y * 365 + y / 4 - y / 100 + y / 400 - 32045;
y = year + 4799;
int k = y * 365 + y / 4 - y / 100 + y / 400 - 31738;
return j - k + 1;
}

void calcSunRiseAndSet() {
int doy = dayOfYear(tm.Year, tm.Month, tm.Day);
float eqt = equationOfTime(doy);
float dec = sunDeclination(doy);
float da = diurnalArc(dec, Settings.Latitude);
float rise = 12 - da - eqt - Settings.Longitude / 15.0;
float set = 12 + da - eqt - Settings.Longitude / 15.0;
timeStruct tsRise, tsSet;
tsRise.Hour = (int)rise;
tsRise.Minute = (rise - (int)rise) * 60.0;
tsSet.Hour = (int)set;
tsSet.Minute = (set - (int)set) * 60.0;
tsRise.Day = tsSet.Day = tm.Day;
tsRise.Month = tsSet.Month = tm.Month;
tsRise.Year = tsSet.Year = tm.Year;
breakTime(toLocal(makeTime(tsRise)), sunRise);
breakTime(toLocal(makeTime(tsSet)), sunSet);
}

void breakTime(unsigned long timeInput, struct timeStruct &tm) {
uint8_t year;
uint8_t month, monthLength;
Expand Down Expand Up @@ -88,8 +142,17 @@ uint32_t getUnixTime() {
return sysTime;
}

String getSunriseTimeString(char delimiter) {
return getTimeString(sunRise, delimiter, false, false);
}

String getSunsetTimeString(char delimiter) {
return getTimeString(sunSet, delimiter, false, false);
}

unsigned long now() {
// calculate number of seconds passed since last call to now()
bool timeSynced = false;
const long msec_passed = timePassedSince(prevMillis);
const long seconds_passed = msec_passed / 1000;
sysTime += seconds_passed;
Expand All @@ -98,11 +161,15 @@ unsigned long now() {
// nextSyncTime & sysTime are in seconds
unsigned long t = getNtpTime();
if (t != 0) {
timeSynced = true;
setTime(t);
}
}
uint32_t localSystime = toLocal(sysTime);
breakTime(localSystime, tm);
if (timeSynced) {
calcSunRiseAndSet();
}
return (unsigned long)localSystime;
}

Expand Down
30 changes: 30 additions & 0 deletions src/WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,26 @@ void addFormNumericBox(const String& label, const String& id, int value)
addFormNumericBox(label, id, value, INT_MIN, INT_MAX);
}

void addFloatNumberBox(const String& id, float value, float min, float max)
{
TXBuffer += F("<input type='number' name='");
TXBuffer += id;
TXBuffer += F("'");
TXBuffer += F(" min=");
TXBuffer += min;
TXBuffer += F(" max=");
TXBuffer += max;
TXBuffer += F(" step=0.01");
TXBuffer += F(" style='width:5em;' value=");
TXBuffer += value;
TXBuffer += F(">");
}

void addFormFloatNumberBox(const String& label, const String& id, float value, float min, float max)
{
addRowLabel(label);
addFloatNumberBox(id, value, min, max);
}


void addTextBox(const String& id, const String& value, int maxlength)
Expand Down Expand Up @@ -3776,6 +3796,8 @@ void handle_advanced() {
String ArduinoOTAEnable = WebServer.arg(F("arduinootaenable"));
String UseRTOSMultitasking = WebServer.arg(F("usertosmultitasking"));
String MQTTUseUnitNameAsClientId = WebServer.arg(F("mqttuseunitnameasclientid"));
String latitude = WebServer.arg(F("latitude"));
String longitude = WebServer.arg(F("longitude"));


if (edit.length() != 0)
Expand Down Expand Up @@ -3810,6 +3832,8 @@ void handle_advanced() {
Settings.ArduinoOTAEnable = (ArduinoOTAEnable == F("on"));
Settings.UseRTOSMultitasking = (UseRTOSMultitasking == F("on"));
Settings.MQTTUseUnitNameAsClientId = (MQTTUseUnitNameAsClientId == F("on"));
Settings.Latitude = latitude.toFloat();
Settings.Longitude = longitude.toFloat();

addHtmlError(SaveSettings());
if (Settings.UseNTP)
Expand Down Expand Up @@ -3843,6 +3867,12 @@ void handle_advanced() {
addUnit(F("minutes"));
addFormCheckBox(F("DST"), F("dst"), Settings.DST);

addFormSubHeader(F("Location Settings"));
addFormFloatNumberBox(F("Latitude"), F("latitude"), Settings.Latitude, -90.0, 90.0);
addUnit(F("&deg;"));
addFormFloatNumberBox(F("Longitude"), F("longitude"), Settings.Longitude, -180.0, 180.0);
addUnit(F("&deg;"));

addFormSubHeader(F("Log Settings"));

addFormIPBox(F("Syslog IP"), F("syslogip"), Settings.Syslog_IP);
Expand Down

0 comments on commit c7c46d2

Please sign in to comment.