Are there limits on string length in Arduino?

arduino, string

Solution

Atmel processor has fairly limited memory management so its easy to end up with fragmented memory. Remember the runtime stack and heap also limited.

Static string can be put into PROGMEM also

There is also a freememory function on the arduino.cc that will show you how much free memory you have.

Problem

I've been trying for hours to put together a simple JSON object string on an arduino to send to a Raspberry Pi running node. I cannot seem to successfully build the string. I have tried building the string all in one go: ``` "{" + string1 + "," + string2 + "," + string3 + "}" etc... ``` I've also tried using a the String.replace function. Each time I end up with a bit of my string, or none at all. The below code shows what's happening: ``` String msg = "{ \"message\" : \"statusUpdate\", "; String active = " \"active\" : TOKEN, "; String intakeTemp = " \"intakeTemp\" : TOKEN, "; String intakeHumid = " \"intakeHumid\" : TOKEN, "; String exhaustTemp = " \"exhaustTemp\" : TOKEN, "; String exhaustHumid = " \"exhaustHumid\" : TOKEN, "; String targetHumid = " \"targetHumid\" : TOKEN, "; String completed = " \"taskCompleted\" : TOKEN }"; if(isActive) active.replace("TOKEN","true"); else active.replace("TOKEN","false"); intakeTemp.replace("TOKEN",floatToString(intakeTemperature,0)); intakeHumid.replace("TOKEN",floatToString(intakeHumidity,0)); exhaustTemp.replace("TOKEN",floatToString(exhaustTemperature,0)); exhaustHumid.replace("TOKEN",floatToString(exhaustHumidity,0)); targetHumid.replace("TOKEN",floatToString(targetHumidity,0)); if(taskFinished) taskCompleted.replace("TOKEN","true"); else taskCompleted.replace("TOKEN","false"); String body = msg; Serial.println(body); body += active; Serial.println(body); body += intakeTemp; Serial.println(body); body += intakeHumid; Serial.println(body); body += exhaustTemp; Serial.println(body); body += exhaustHumid; Serial.println(body); body += targetHumid; Serial.println(body); body += taskCompleted; Serial.println(body); ``` You can see from the last bit of code above that as the string is being built, i'm spitting it out to the serial monitor. However, here is my serial output: ``` { "message" : "statusUpdate", { "message" : "statusUpdate", "active" : false, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0, { "message" : "statusUpdate", "active" : false, "intakeTemp" : 0.0, "intakeHumid" : 0.0, "exhaustTemp" : 0.0, ``` Is there a limit to the string length? I haven't found any mention of such limits in the docs. There's not much else to the sketch except the standard `Ethernet` library and the code to send it via an HTTP request (from the sample project). Any idea what might be happening? EDIT: Ok, I've shortened my string like so: ``` String msg = "{ \"m\" : \"status\", "; String active = " \"a\" : TOKEN, "; String intakeTemp = " \"iT\" : TOKEN, "; String intakeHumid = " \"iH\" : TOKEN, "; String exhaustTemp = " \"eT\" : TOKEN, "; String exhaustHumid = " \"eH\" : TOKEN, "; String targetHumid = " \"tH\" : TOKEN, "; String dryerJustFinished = " \"f\" : TOKEN }"; ``` and sure enough, it's started to work: ``` { "m" : "status", { "m" : "status", "a" : false, { "m" : "status", "a" : false, "iT" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, "eH" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, "eH" : 0.0, "tH" : 0.0, { "m" : "status", "a" : false, "iT" : 0.0, "iH" : 0.0, "eT" : 0.0, "eH" : 0.0, "tH" : 0.0, "f" : false } ``` Which means there is a limitation. Is this a memory limitation? By the way, the hardware is an Arduino Uno R3

Original source