From 57b5d33b713a6dd3b20d9d12897fd1cfca88dfb9 Mon Sep 17 00:00:00 2001 From: Techniccontroller Date: Sat, 4 Apr 2026 12:17:27 +0200 Subject: [PATCH 1/8] fix: improve daylight saving time handling in updateSWChange method, fix #79 --- ntp_client_plus.cpp | 40 ++++++++++++++++++++++++++++++++++------ 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/ntp_client_plus.cpp b/ntp_client_plus.cpp index 2dadd9f..b35504b 100644 --- a/ntp_client_plus.cpp +++ b/ntp_client_plus.cpp @@ -585,6 +585,15 @@ bool NTPClientPlus::updateSWChange() unsigned int dateDay = this->_dateDay; unsigned int dateMonth = this->_dateMonth; + // Calculate local STANDARD time (without DST) for exact switch-hour handling. + // EU rule: DST starts on last Sunday in March at 02:00 (standard time), + // and ends on last Sunday in October at 03:00 local summer time + // (== 02:00 standard time). + unsigned long secsSince1900NoDST = this->_utcx * this->secondperminute + + this->_secsSince1900 + + ((millis() - this->_lastUpdate) / 1000); + unsigned int hourNoDST = (secsSince1900NoDST % this->secondperday) / this->secondperhour; + bool summertimeActive = false; if (this->_swChange) @@ -607,10 +616,19 @@ bool NTPClientPlus::updateSWChange() //Calculation: 31 - 30 = 1; 1 + 2 = 3; //Result: Last day in March is a Wednesday. Changeover to summer time already done => set summer time - // If today is Sunday (dayOfWeek == 7) then this is already the last sunday in march -> set summer time + // If today is Sunday (dayOfWeek == 7) then this is the last Sunday in March. + // Switch to summer time at 02:00 standard time (clock jumps to 03:00). if(dayOfWeek == 7){ - this->setSummertime(1); - summertimeActive = true; + if (hourNoDST >= 2) + { + this->setSummertime(1); + summertimeActive = true; + } + else + { + this->setSummertime(0); + summertimeActive = false; + } } //There follows within the last week in March one more Sunday => set winter time @@ -652,10 +670,20 @@ bool NTPClientPlus::updateSWChange() //Calculation: 31 - 26 = 5; 5 + 2 = 7; //Result: Last day in October is a Sunday. There follows another Sunday in October => set summer time - // If today is Sunday (dayOfWeek == 7) then this is already the last sunday in october -> winter time + // If today is Sunday (dayOfWeek == 7) then this is the last Sunday in October. + // Switch back to winter time at 02:00 standard time + // (which corresponds to 03:00 local summer time). if(dayOfWeek == 7){ - this->setSummertime(0); - summertimeActive = false; + if (hourNoDST >= 2) + { + this->setSummertime(0); + summertimeActive = false; + } + else + { + this->setSummertime(1); + summertimeActive = true; + } } // There follows within the last week in October one more Sunday => summer time From 7afb73c857117795a6cd0d174a4401247465cd1b Mon Sep 17 00:00:00 2001 From: Edgar W <36072504+techniccontroller@users.noreply.github.com> Date: Sat, 4 Apr 2026 12:35:25 +0200 Subject: [PATCH 2/8] fix signed time calculations Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- ntp_client_plus.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ntp_client_plus.cpp b/ntp_client_plus.cpp index b35504b..8dc6742 100644 --- a/ntp_client_plus.cpp +++ b/ntp_client_plus.cpp @@ -589,10 +589,16 @@ bool NTPClientPlus::updateSWChange() // EU rule: DST starts on last Sunday in March at 02:00 (standard time), // and ends on last Sunday in October at 03:00 local summer time // (== 02:00 standard time). - unsigned long secsSince1900NoDST = this->_utcx * this->secondperminute + - this->_secsSince1900 + - ((millis() - this->_lastUpdate) / 1000); - unsigned int hourNoDST = (secsSince1900NoDST % this->secondperday) / this->secondperhour; + int64_t currentUtcSeconds = static_cast(this->_secsSince1900) + + static_cast((millis() - this->_lastUpdate) / 1000); + int64_t secsSince1900NoDST = currentUtcSeconds + + (static_cast(this->_utcx) * this->secondperminute); + int64_t secondsIntoDay = secsSince1900NoDST % this->secondperday; + if (secondsIntoDay < 0) + { + secondsIntoDay += this->secondperday; + } + unsigned int hourNoDST = static_cast(secondsIntoDay / this->secondperhour); bool summertimeActive = false; From 491c35f329b62b65ce7be21c59316c6edab29c61 Mon Sep 17 00:00:00 2001 From: Techniccontroller Date: Sat, 4 Apr 2026 12:41:41 +0200 Subject: [PATCH 3/8] fix: improve calculation of seconds since 1 Jan 1900 to consider signed/unsigned variables --- ntp_client_plus.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/ntp_client_plus.cpp b/ntp_client_plus.cpp index 8dc6742..f272461 100644 --- a/ntp_client_plus.cpp +++ b/ntp_client_plus.cpp @@ -128,11 +128,12 @@ void NTPClientPlus::setPoolServerName(const char *poolServerName) * @return unsigned long seconds since 1. Jan. 1900 */ unsigned long NTPClientPlus::getSecsSince1900() const -{ - return this->_utcx * this->secondperminute + // UTC offset - this->_summertime * this->secondperhour + // Summer time offset - this->_secsSince1900 + // seconds returned by the NTP server - ((millis() - this->_lastUpdate) / 1000); // Time since last update +{ + int64_t secsSince1900WithDST = static_cast(this->_secsSince1900) + // seconds returned by the NTP server + static_cast((millis() - this->_lastUpdate) / 1000) + // Time since last update + this->_summertime * this->secondperhour + // Summer time offset + (static_cast(this->_utcx) * this->secondperminute); // UTC offset + return static_cast(secsSince1900WithDST); } /** From 6d3ebc36023d26bbe9c370f941e3812d0924628b Mon Sep 17 00:00:00 2001 From: Edgar W Date: Sun, 26 Apr 2026 19:20:40 +0200 Subject: [PATCH 4/8] fix: add timeout for WiFi disconnection handling --- wordclock_esp8266.ino | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/wordclock_esp8266.ino b/wordclock_esp8266.ino index 2dc2dbb..b3fe8c9 100644 --- a/wordclock_esp8266.ino +++ b/wordclock_esp8266.ino @@ -94,6 +94,7 @@ #define PERIOD_SNAKE 50 #define PERIOD_PONG 10 #define TIMEOUT_LEDDIRECT 5000 +#define TIMEOUT_WIFI_DISCONNECTED 30000 #define PERIOD_STATECHANGE 10000 #define PERIOD_NTPUPDATE 30000 #define PERIOD_TIMEVISUUPDATE 1000 @@ -189,6 +190,7 @@ long lastNTPUpdate = millis() - (PERIOD_NTPUPDATE-3000); // time of last NTP up long lastAnimationStep = millis(); // time of last Matrix update long lastNightmodeCheck = millis() - (PERIOD_NIGHTMODECHECK-3000); // time of last nightmode check long buttonPressStart = 0; // time of push button press start +long wifiDisconnectedSince = 0; // time since WiFi connection was lost (0 = connected) uint16_t behaviorUpdatePeriod = PERIOD_TIMEVISUUPDATE; // holdes the period in which the behavior should be updated // Create necessary global objects @@ -456,11 +458,22 @@ void loop() { lastheartbeat = millis(); // Check wifi status (only if no apmode) - if(!apmode && WiFi.status() != WL_CONNECTED){ - Serial.println("connection lost"); - ledmatrix.gridAddPixel(0, 5, colors24bit[1]); - ledmatrix.drawOnMatrixInstant(); - delay(1000); + if(!apmode){ + if(WiFi.status() != WL_CONNECTED){ + if(wifiDisconnectedSince == 0){ + wifiDisconnectedSince = millis(); + Serial.println("connection lost"); + } + + if(millis() - wifiDisconnectedSince >= TIMEOUT_WIFI_DISCONNECTED){ + ledmatrix.gridAddPixel(0, 5, colors24bit[1]); + ledmatrix.drawOnMatrixInstant(); + delay(1000); + } + } + else { + wifiDisconnectedSince = 0; + } } } From 26c47ad4ce1e0c6f6d244c8b2985bf7562cc703d Mon Sep 17 00:00:00 2001 From: deepstahl77 <129783157+deepstahl77@users.noreply.github.com> Date: Wed, 8 Jul 2026 23:28:52 +0200 Subject: [PATCH 5/8] Optimize getFormattedTime method with snprintf Refactor time formatting to use snprintf for efficiency and avoid heap fragmentation. --- ntp_client_plus.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/ntp_client_plus.cpp b/ntp_client_plus.cpp index f272461..6892941 100644 --- a/ntp_client_plus.cpp +++ b/ntp_client_plus.cpp @@ -200,15 +200,14 @@ int NTPClientPlus::getSeconds() const String NTPClientPlus::getFormattedTime() const { unsigned long rawTime = this->getEpochTime(); unsigned long hours = (rawTime % 86400L) / 3600; - String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); - unsigned long minutes = (rawTime % 3600) / 60; - String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); - unsigned long seconds = rawTime % 60; - String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); - return hoursStr + ":" + minuteStr + ":" + secondStr; + // Fester Puffer statt mehrfacher String-Verkettung, um Heap-Fragmentierung zu vermeiden + // Format identisch zu vorher: hh:mm:ss (jeweils 2-stellig, mit fuehrender Null) + char buffer[9]; // "hh:mm:ss" + '\0' + snprintf(buffer, sizeof(buffer), "%02lu:%02lu:%02lu", hours, minutes, seconds); + return String(buffer); } /** @@ -732,4 +731,4 @@ bool NTPClientPlus::updateSWChange() } return summertimeActive; -} \ No newline at end of file +} From f1f6db32626b84ba78e99422c93677a0c247478e Mon Sep 17 00:00:00 2001 From: deepstahl77 <129783157+deepstahl77@users.noreply.github.com> Date: Wed, 8 Jul 2026 23:47:42 +0200 Subject: [PATCH 6/8] Refactor logString method for efficiency Refactor logString to use snprintf for better performance and avoid heap fragmentation. --- udplogger.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/udplogger.cpp b/udplogger.cpp index 369af22..0e7c34a 100644 --- a/udplogger.cpp +++ b/udplogger.cpp @@ -16,15 +16,16 @@ void UDPLogger::setName(String name){ _name = name; } -void UDPLogger::logString(String logmessage){ +void UDPLogger::logString(const String& logmessage){ // wait 5 milliseconds if last send was less than 5 milliseconds before if(millis() < (_lastSend + 5)){ delay(5); } - logmessage = _name + ": " + logmessage; - Serial.println(logmessage); + // Fester Puffer statt String-Verkettung, um Heap-Fragmentierung zu vermeiden + // (identisches Format und identische Kuerzung bei 100 Zeichen wie vorher) + snprintf(_packetBuffer, sizeof(_packetBuffer), "%s: %s", _name.c_str(), logmessage.c_str()); + Serial.println(_packetBuffer); _Udp.beginPacketMulticast(_multicastAddr, _port, _interfaceAddr); - logmessage.toCharArray(_packetBuffer, 100); _Udp.print(_packetBuffer); _Udp.endPacket(); _lastSend=millis(); @@ -35,4 +36,4 @@ void UDPLogger::logColor24bit(uint32_t color){ uint8_t resultGreen = color >> 8 & 0xff; uint8_t resultBlue = color & 0xff; logString(String(resultRed) + ", " + String(resultGreen) + ", " + String(resultBlue)); -} \ No newline at end of file +} From d49386d8aabcf5628565a50a905bb8caf30a026c Mon Sep 17 00:00:00 2001 From: Edgar W Date: Sat, 11 Jul 2026 23:04:57 +0200 Subject: [PATCH 7/8] fix signature of logString() --- udplogger.cpp | 4 ++-- udplogger.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/udplogger.cpp b/udplogger.cpp index 0e7c34a..00ecb02 100644 --- a/udplogger.cpp +++ b/udplogger.cpp @@ -21,8 +21,8 @@ void UDPLogger::logString(const String& logmessage){ if(millis() < (_lastSend + 5)){ delay(5); } - // Fester Puffer statt String-Verkettung, um Heap-Fragmentierung zu vermeiden - // (identisches Format und identische Kuerzung bei 100 Zeichen wie vorher) + // use fixed buffer instead of string concatenation to avoid heap fragmentation + // cut message to 100 characters of length (buffer size) snprintf(_packetBuffer, sizeof(_packetBuffer), "%s: %s", _name.c_str(), logmessage.c_str()); Serial.println(_packetBuffer); _Udp.beginPacketMulticast(_multicastAddr, _port, _interfaceAddr); diff --git a/udplogger.h b/udplogger.h index c26fa49..e4cac28 100644 --- a/udplogger.h +++ b/udplogger.h @@ -22,7 +22,7 @@ class UDPLogger{ UDPLogger(); UDPLogger(IPAddress interfaceAddr, IPAddress multicastAddr, int port); void setName(String name); - void logString(String logmessage); + void logString(const String& logmessage); void logColor24bit(uint32_t color); private: String _name; From 18393ac9660680b111ce0ac29b48b90795224eae Mon Sep 17 00:00:00 2001 From: Edgar W Date: Sat, 11 Jul 2026 23:12:59 +0200 Subject: [PATCH 8/8] fix: initialize _lastSend in UDPLogger constructor and update logString comment --- udplogger.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/udplogger.cpp b/udplogger.cpp index 00ecb02..619afc7 100644 --- a/udplogger.cpp +++ b/udplogger.cpp @@ -10,6 +10,7 @@ UDPLogger::UDPLogger(IPAddress interfaceAddr, IPAddress multicastAddr, int port) _interfaceAddr = interfaceAddr; _name = "Log"; _Udp.beginMulticast(_interfaceAddr, _multicastAddr, _port); + _lastSend = 0; } void UDPLogger::setName(String name){ @@ -22,7 +23,7 @@ void UDPLogger::logString(const String& logmessage){ delay(5); } // use fixed buffer instead of string concatenation to avoid heap fragmentation - // cut message to 100 characters of length (buffer size) + // message will be truncated to fit into _packetBuffer (99 chars + NUL) snprintf(_packetBuffer, sizeof(_packetBuffer), "%s: %s", _name.c_str(), logmessage.c_str()); Serial.println(_packetBuffer); _Udp.beginPacketMulticast(_multicastAddr, _port, _interfaceAddr);