Best way to build "string"

Hi all, thanks to help by CountryPaul, my UDP setup is up and working. I have a code snippet that builds a string that I send to the UDP server:

            //UDP setup was successful. Build and send
            //server ip request.
            String sIpReqCmd = "[[a" + WiFi.macAddress();
            char sSend[16];
            sIpReqCmd.replace(":", "");

            sIpReqCmd.toCharArray(sSend, 16);
            //Serial.println(sIpReqCmd);  

            //Send unicast
            udp.broadcastTo(sSend, uServerPORT);

This works fine. I have read various posts that recommend to not use the String type. I was not able to find out how to get the WiFi.macAddress() into a char array, hence the above solution. So I ask, is there a better way to do what I demonstrated in the code sample? Thanks! Saga

The Arduino Uno has only 2kbyte of SRAM. If there is heap fragmentation, then it can crash when it reaches the stack: https://learn.adafruit.com/memories-of-an-arduino/measuring-free-memory.
If the String class is used, it will allocate and free blocks from the heap many times. There is a chance that heap fragmentation will eat all of the availble SRAM.

The ESP32S should be no problem.

1 Like

you can print into the udp object with multiple print calls. the complete message is then send with endPacket

In your case Strings are completely safe and free from fragmentation just add an enclosing block { }

Strings are very very safe on small memory AVR boards. Using reserve() and String& avoids memory problems
Even if you run Out of Memory, the Uno/Meg2560 just keeps running with the Strings holding as much as they can. This makes it very easy to debug as there is not 'crash' and you can still see your debug print() output.

See my tutorial on Taming Arduino Strings for the details.

2 Likes

The ESP32 underneath uses Strings, as the OP has discovered. If you intend to run the board forever, my tutorial suggests an auto reboot once a day to clean up any memory leaks from the underlying WiFi library. See Adding Periodic Reboots to ESP32 and ESP8266

2 Likes

You could always look at the example in the docs: Arduino - WiFiMACAddress - doesn't use Strings at all.

1 Like

I did not know that! Thanks, helpful. Saga

Thanks. This is interesting. I will have to try this. I am sure it will be helpful and in future projects. Saga

This is extremely useful. I have been thinking of auto rebooting the ESP 32, but that is still in the future. This will give me a big push, thanks! Saga

PS: I read the relevant part of the referenced article and have questions. When the time comes I'll post a new thread. For now, just to understand, is the reboot that is mentioned in the article equivalent to pressing the reboot button on the ESP32? Thanks.

This is a great idea! Thanks! Saga

I saw it. I was hesitant to implement a solution around this idea because it seemed like I needed more code to format the string the way I needed it, I'll have to revisit this and do some experimentation to see how it works for my scenario. Thanks, much appreciated! Saga