Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions ntp_client_plus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -732,4 +731,4 @@ bool NTPClientPlus::updateSWChange()
}

return summertimeActive;
}
}
12 changes: 7 additions & 5 deletions udplogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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));
}
}
2 changes: 1 addition & 1 deletion udplogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading