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 +} diff --git a/udplogger.cpp b/udplogger.cpp index 369af22..619afc7 100644 --- a/udplogger.cpp +++ b/udplogger.cpp @@ -10,21 +10,23 @@ UDPLogger::UDPLogger(IPAddress interfaceAddr, IPAddress multicastAddr, int port) _interfaceAddr = interfaceAddr; _name = "Log"; _Udp.beginMulticast(_interfaceAddr, _multicastAddr, _port); + _lastSend = 0; } 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); + // use fixed buffer instead of string concatenation to avoid heap fragmentation + // 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); - logmessage.toCharArray(_packetBuffer, 100); _Udp.print(_packetBuffer); _Udp.endPacket(); _lastSend=millis(); @@ -35,4 +37,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 +} 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;