LED-Clock/realtime.ino
2022-11-10 20:03:13 +01:00

181 lines
5.1 KiB
C++

// https://github.com/esp8266/Arduino/commit/a05a71fa9d2e6b143cb34f01b47e22c4b66b80a1
void debugSNTP() {
// lwIP v2 is able to list more details about the currently configured SNTP servers
for (int i = 0; i < SNTP_MAX_SERVERS; i++) {
IPAddress sntp = *sntp_getserver(i);
const char* name = sntp_getservername(i);
if (sntp.isSet()) {
Serial.printf("sntp%d: ", i);
if (name) {
Serial.printf("%s (%s) ", name, sntp.toString().c_str());
} else {
Serial.printf("%s ", sntp.toString().c_str());
}
Serial.printf("- IPv6: %s - Reachability: %o\n", sntp.isV6() ? "Yes" : "No", sntp_getreachability(i));
}
}
}
String printLocalTime() {
if (!getLocalTime(&timeinfo, 200)) {
return "N/A";
}
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
char timeStringBuff[50]; //50 chars should be enough
strftime(timeStringBuff, sizeof(timeStringBuff), "%A, %B %d %Y %H:%M:%S", &timeinfo);
return timeStringBuff;
}
/*
* Check for DHCP supplied NTP
*/
void ntpReachableCheckCallback() {
// lwIP v2 is able to list more details about the currently configured SNTP servers
int usuableServer = 0;
int serverCount = 0;
for (int i = 0; i < SNTP_MAX_SERVERS; i++) {
IPAddress sntp = *sntp_getserver(i);
const char* name = sntp_getservername(i);
if (sntp.isSet()) {
serverCount++;
if (sntp_getreachability(i) > 0) {
NTPreachable = true;
usuableServer++;
getLocalTime(&timeinfo, 200);
Serial.println("NTP reachable");
currentNTP=sntp.toString();
}
else {
NTPreachable = false;
Serial.println("ntp NOT reachable");
}
}
if (serverCount == 0) {
setFallbackNTP();
}
}
}
/*
refresh the internal time struct
*/
void timeRefreshCallback( bool from_sntp ) {
getLocalTime(&timeinfo, 200);
Serial.print(asctime(&timeinfo));
Serial.printf("timezone: %s\n", getenv("TZ") ? : "(none)");
//set new time only when from sntp
//no manual way to set time
if (!from_sntp) {
Serial.println("Not SNTP time");
}
else {
debugSNTP();
Serial.println("SNTP time used");
if (clockwiseRing) {
currentSec = timeinfo.tm_sec;
currentMin = timeinfo.tm_min;
currentHour = MOD(timeinfo.tm_hour, 12);
}
else {
//inverted logic - remaining time
currentSec = 60 - timeinfo.tm_sec;
currentMin = 60 - timeinfo.tm_min;
currentHour = 12 - MOD(timeinfo.tm_hour, 12);
}
Serial.println("timestruct mapped: " + String(int(currentHour * NUM_LEDS / 12)) + ":" + String(int(currentMin * NUM_LEDS / 60)) + ":" + String(int(currentSec * NUM_LEDS / 60)));
bootAnim.disable();
//iotWebConfConfigSaved(); //ToDo why?
ledRefresh.enableIfNot();
#ifdef LDR_PIN
brightnessFading.enableIfNot();
#endif
clockTick.enableIfNot();
brightnessAdjustment.enableIfNot();
}
}
void setTimeZone() {
//init and get the time
#ifdef ESP8266
//sntp_servermode_dhcp(0); // 0: disable obtaining SNTP servers from DHCP (enabled by default)
configTime(MYTZ,currentNTP);
yield();
#endif
#ifdef ESP32
setenv("TZ", "AEST-10", 1);
tzset(); // save the TZ variable
#endif
yield();
}
/*
function to set NTP server
maybe not needed
*/
void setFallbackNTP() {
//init and get the time
#ifdef ESP8266
//sntp_servermode_dhcp(0); // 0: disable obtaining SNTP servers from DHCP (enabled by default)
//configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2, const char* server3 )
configTime(MYTZ, "pool.ntp.org"); //ToDo skip TZ
//setTZ(MYTZ);
#endif
#ifdef ESP32
configTime(0, 0, "pool.ntp.org");
#endif
yield();
}
/*
Callback to handle the separate
loose time structure for the mapping
to the analog representation of a clock
*/
void clockTickCallback() {
#ifdef LDR_PIN
ldrValue = (ldrValue + analogRead(LDR_PIN));
#endif
if (clockwiseRing) {
if (currentSec >= 60) {
currentSec = 0;
currentMin++;
if (currentMin >= 60) {
currentMin = 0;
currentHour++;
if (currentHour >= 12) {
currentHour = 0;
}
}
if (followingHour) {
hourOffset = int(currentMin * NUM_LEDS / 60 / 12);
//Serial.println("clockwise hourOffset: " + String(hourOffset));
}
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour / (12 / NUM_LEDS)) + ":" + String(currentMin / (60 / NUM_LEDS)) + ":" + String(currentSec / (60 / NUM_LEDS)));
}
currentSec++;
}
else {
if (currentSec < 0) {
currentSec = 59;
currentMin--;
if (currentMin < 0) {
currentMin = 59;
currentHour--;
if (currentHour < 0) {
currentHour = 11;
}
}
if (followingHour) {
hourOffset = 0 - int((60 - currentMin * NUM_LEDS / 60) / 12); //negative value
//Serial.println("Anticlockwise hourOffset: " + String(hourOffset));
}
//Serial.println(String(interruptCounter) + " | Ring Index: " + String(currentHour / (12 / NUM_LEDS)) + ":" + String(currentMin / (60 / NUM_LEDS)) + ":" + String(currentSec / (60 / NUM_LEDS)));
}
currentSec--;
}
}