How to combine strings and text together? I'm trying to combine two strings (strings/int) together with text in below code:

int foo = String((char*)payload).indexOf(' ');
int message1; 
int message2; 

message1 = String((char*)payload).substring(0, foo).toInt(); 
Serial.print(message1); // Returns 1

message2 = String((char*)payload).substring(foo + 1, length).toInt();
Serial.print(message2); // Returns 0

client.publish(msg, String(message1 + " " + message2).c_str(), true);
// published value: 1 0


Instead of publishing "1 0" its publishing some weird values, what am i missing?
I wouldn't use the String class, but instead use the C functions to concatenate strings (like strcat, strncat).

You can use the itoa function to convert an integer to a string, see:


  https://stackoverflow.com/questions/9655202/how-to-convert-integer-to-string-in-c


Or use the sprintf function.

These are all standard C functions which gives you complete control of the buffer sizes to use. Handy because the Arduino Uno has just 2 KB.