Somewhat perplexed with concatenation

I have a problem (that has clearly been posted here before)

I am trying to hit a webserver, where I send a number value to a simple uri (it's not properly formed REST)

This code works:

           if (client.connect(server, 8888)) {
              Serial.print("connected to ");
              Serial.println(client.remoteIP());
              // Make a HTTP request:
              client.println("GET /classicui/CMD?Echo_Kitchen_TTS=62 HTTP/1.1");
              client.println("Host: www.myuri.com");
              client.println("Connection: close");
              client.println();
            } else {
              // if you didn't get a connection to the server:
              Serial.println("connection failed");
            }

This code doesn't

           if (client.connect(server, 8888)) {
              Serial.print("connected to ");
              Serial.println(client.remoteIP());
              // Make a HTTP request:
              int t1 = 62;
              client.println("GET /classicui/CMD?Echo_Kitchen_TTS=" + t1 + " HTTP/1.1");
              client.println("Host: www.myuri.com");
              client.println("Connection: close");
              client.println();
            } else {
              // if you didn't get a connection to the server:
              Serial.println("connection failed");
            }

A few posts suggest sprintf, but others say this is 'using a hammer to squash a mosquito'

So 2 questions,

    • What is the actual correct way to handle this concatenation (I get that the + is an addition, not a concatenator in C - so, I understand why it is wrong)
    • If sprintf is deemed too 'heavyweight', what is the soft touch solution?

"+" works as a concatentation operator for String objects, but on 8 bit Arduinos, use of Strings causes memory problems and program crashes.

You have three safe choices: snprintf(), break up the client.print() calls or use C-strings (zero terminated character arrays) and the <string.h> library. <string.h> offers lots of routines to format numbers to character arrays (e.g. itoa()), to concatenate strings (strcat()), etc.

I would do it like this:

change this line

client.println("GET /classicui/CMD?Echo_Kitchen_TTS=" + t1 + " HTTP/1.1");

to

client.print("GET /classicui/CMD?Echo_Kitchen_TTS=");
client.print(t1);
client.println(" HTTP/1.1");

Edit: just like jremington wrote: "break up the client.print() calls"; sorry... :wink:

Perfect guys,
Guess too much staring at the screen meant I missed the obvious (d’oh)