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 1/4] 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 2/4] 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 3/4] 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 4/4] 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);