String concat / pass string parameters to functions problems

Hi All,
I implemented a gateway to receive IR and RF signals to control Philips Hue lamps
and
and convert some IR commands to RF.

I have some problem in generating the command to be sent to the server.

I need to create a message like:
{
on: true,
bri: 150,
hue: 65,
sat: 20
}

I create two functions.

  • setHue(int hueID, String command) that send the proper command to the light
  • setHue( int hueID, boolean on, int hueBri, long hueHue, long hueSat)
    to concatenate the 4 parameters (on/off, bri, hue, sat)

The first function works only if the command is very short

If I run the function setHue with a short command everything is fine,
otherways Arduino doesn't boot.

//This works
setHue(1, "{   \"on\":true}");

//This doesn't work (but compiles with no problems)  
setHue(1, "{   \"on\":true,  \"hue\": 168,  \"bri\": 24975,  \"sat\": 253}");

Here is the code for the other function

boolean SetHue(int hueID, boolean on, int hueBri, long hueHue, long hueSat)
{
  String hueCmd="{";
  if(on==1){  
    hueCmd = hueCmd + "\n \"on\":true";
    if(hueHue>0){
      hueCmd = hueCmd + ",\n \"hue\": " + String(hueHue);
    }  
     if(hueBri>0){
      hueCmd = hueCmd + ",\n \"bri\": " + String(hueBri);
    }
    if(hueSat>0){ 
      hueCmd = hueCmd + ",\n \"sat\": " + String(hueSat) ;
    }
    hueCmd = hueCmd + "}";
  }    
  else 
  {
    hueCmd = "{\"on\":false}";
  }
  return SetHue(hueID, hueCmd);
}

Here is the complete source code
~~http://pastebin.com/Zv31F6hf~~

Attached you can find the complete source code

IR_RF.ino (6.97 KB)

Here is the complete source code

Wrong place for it. You can attach it to your post, using the Additional Options link.

otherways Arduino doesn't boot.

I'm going to go out on a limb (because I'm betting it isn't very far) and guess that you are running out of memory from pissing resources away on the String class.

PaulS:
Wrong place for it. You can attach it to your post, using the Additional Options link.

Fixed :blush:

PaulS:
I'm going to go out on a limb (because I'm betting it isn't very far) and guess that you are running out of memory from pissing resources away on the String class.

Do you suggest to use a different programming approach (char or other) or to move to an Arduino Mega or Due?

Dump the string class and use arrays of char. Sprintf will be handy.

You have a lot of string literals in your code. Move them to progmem: client.print and serial.print can use the F macro:

    Serial.print(F("Light  : "));

Also, the long strings you're using in mySwitch.send should be stored in progmem.