Wierd problem using Strings() ????

i am happy to join your group and very happy to discover Arduino!

Unfortunately i am facing a problem that i can not solve by my own. I am giving you code below and explaining my issue.

void loop()
    // 0. CM initial state
    // 1. Read the client cmd
    // 2. Parse the cmd
    // 3. Fetch my cmd
    // 4. Execute and print
    // 5. Close
    // 6. Determine the color of the led, red or blue
  // Set the variables to zero
  // Set the cm led on

  // listen for incoming clients
  EthernetClient client = server.available();
  if (client) {
    boolean sentHeader = false;
    String readStr = String(30);
    boolean first = false;
      // Read incoming data from the client
        // Take data from the client
          // send a standard http response header
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/xml");
          sentHeader = true;
        char c = client.read();
        if(readStr.indexOf("GET") > 0){
          readStr = "";
        }else if(readStr.length() < 2 && ( readStr.indexOf(" ") != -1 || readStr.indexOf("/") != -1 ) ){
          readStr = "";
          first = true;
        }else if(first && c == ' ' ){
           String cmdStr = String(readStr);
           // Remove any empty spaces from the end of my command string
           client.println("<?xml-stylesheet type=\"text/css\" href=\"data:text/css,");
           client.println("Response{display:block;  padding-top:15%;  text-align:center; background-color:%23336699; color:%23FFFFFF; font-family:Tahoma,Arial; font-size:40px;}");
           client.println("Info,Measurement{display:block; padding-top:15%; text-align:center; background-color:%23336699; color:%23ff9933; font-family:Tahoma,Arial; font-size:40px;}type{display:block; padding-bottom:5px; clear:right;}");
           // Use the command string to obtain the command number
           int command = CmdFromStr(cmdStr);
           Serial.print("command num:");

           // switch using the command number and execute
           String result = ChooseCmdAndExecute(command);
           // Keep for debugging purposes
        readStr += c;
      } // client.available  
    }// while / client.connected() && client.available()
    // Close the connection to the client because there is nothing else to read or write
  } // if(client)
   // Nodes led red or blue 
} // loop

Above you see the main loop functions. What i do is parse the http request and keep only the staff i need. After that i want to create a string, through another function and print it backto the client.

String command_12(){
String ip_str = byte_ip_to_str(ip,4);
String str ="firmvare ";
str += firmware_version;
str += "cm ";
str += cm_version;
str += "ip ";
str += ip_str;
str += "mac ";
str += mac_str;
str += “”;

return str;

The above function creates a string with staff i want to send to the client. Ufortunately i can not seem to get any return from the function. Or the staff i get is garbage(!?).
I searched my code up and down and i can not find anything to explain the results. It seems that the String library is not working properly.

Any suggestions would be welcome as i am facing a deadend!


An info i forgot to mention. I am using :

  • Arduino 1.0.3 ide
  • Ardudino Board Model R3


Don’t use the String library. The Arduino does not have enough ram (only 2kB) to do anything other than trivial things with Strings.
You will have to convert your code to use character string arrays (C-style null-terminated strings).


Thank you for your quick reply!

Is there a possibility to use an sd card to extend the capabilities of the ram!


Is there a possibility to use an sd card to extend the capabilities of the ram!

No, an SD card is flash memory and not RAM. RAM is the working memory of the processor, it's reading and writing the RAM thousands of times in a second. Flash memory would wear out in a few seconds (limited write cycles).

The limited RAM is the "beginner" reason for not using the String class.

The implementation of the String class is done in a way suitable for PCs and other computers with lots of RAM and especially: memory management units. The ATmega328 of the Arduino has neither of it. Additionally there is a bug in the low level C library of the AVR utilities (in the free() routine) which makes the bad-for-Arduino programming of the String class even worse. This was the bit lengthy explanation of why not using the String class.

So: Don't use the String class! You can do everything with good old C strings (character arrays), just not as convenient.

Thanx again! Your feedback is very helpfull !!! :slight_smile: