Help combining strings and integers into an output

Hi, I’m new to programming in general and would like to know how to combine a string and an integer in a variable output. So, my integer comes from the sensor value directly and the string is for combining 2 sensor values into a readable JSON. My code is something like this…

Spark.variable("TotalValue", "{sensor1:" + &value1 + ", " + "sensor2:" + &value2 + "}", INT);

(I know that the end should not be INT but I’m not sure what to put)
So what I’m trying to output would be
{sensor1:200, sensor2:100}
On the assumption that &value1 = 200 and & value2 = 100.

Not sure I’m clear enough with my question but hopefully you do understand and many thanks!

The only way to have a string and a numeric value combined in one variable is to have both as string.


char returnString[63];
String someString;
uint16_t a0value;

void setup()
  // fill the string with 63 '/0' characters
  memset(returnString, 0, 63);  // not required but to show off ;-)
  pinMode(A0, INPUT_PULLUP);

  // setup a variable "TotalValue" with a buffer "returnString" of type STRING
  Spark.variable("TotalValue", returnString, STRING);

void loop()
  a0value = analogRead(A0);
  someString = "SomePrefix: " + String(a0value);
  // copy all characters of the String object to the
  //   buffer for Spark.variable "TotalValue"
  strcpy(returnString, someString.c_str());

// Expected output:
// "SomePrefix: 4095"

But as @Leonardo points out below - please read the docs carefully and try to figure out why and how the given examples work.


Sorry as I said I’m quite new so even after looking up memset and strcpy I’m afraid I still don’t quite understand what they do. Also, in this case with the code that you’ve given what would be the output given if you were to serial print it?

Lastly, in my case, it would mean that I need to convert the sensor value to a string and store it in another variable and only then can I call the new variable into the Spark.variable to output everything as a string?

I think you should read this to understand more about Spark.variable

This is basically copied off the spark documents:

int analogvalue = 0;  //This is just to declare the type
double tempC = 0;
char *message = "my name is spark";

void setup()
  // variable name max length is 12 characters long
  Spark.variable("analogvalue", &analogvalue, INT);
  Spark.variable("temp", &tempC, DOUBLE);
  Spark.variable("mess", message, STRING);
  pinMode(A0, INPUT);

void loop()
  // Read the analog value of the sensor (TMP36)
  analogvalue = analogRead(A0);
  //Convert the reading into degree celcius
  tempC = (((analogvalue * 3.3)/4095) - 0.5) * 100;

# The bottom few lines should be your example results
# In return you'll get something like this:
my name is spark

Notice the mistake you made for the Spark.variable.

It should have been Spark.variable(“TotalValue”, something, STRING); You should have done something like something = "Sensor1:" + value1 + "Sensor2:" + value2;

If you see @ScruffR post you will notice his Spark.variable and the String is like the one I have mention.

1 Like

Hey, sorry to revive this topic, but what if I read out an RTC and get a byte with Dec which I want to send with spark.variable?
I can write Serial.print(second, DEC); to check my variable and it works great. Seen on the serial monitor.
Every time I try to get it into a char to send it as a string I receive something stupid like an “!” or “&”.
Any idea how to convert it?
My try was:
bigString[0] = (char) second, DEC; … doesn’t work.

Spark.variable("bigString", bigString, STRING);    

void readDS3231time(byte *second,byte *minute,byte *hour,byte *dayOfWeek,byte *dayOfMonth,byte *month,byte *year)
  Wire.write(0); // set DS3231 register pointer to 00h
  Wire.requestFrom(DS3231_I2C_ADDRESS, 7);
  // request seven bytes of data from DS3231 starting from register 00h
  *second = bcdToDec( & 0x7f);
  // * bedeutet Pointer
  *minute = bcdToDec(;
  *hour = bcdToDec( & 0x3f);
  *dayOfWeek = bcdToDec(;
  *dayOfMonth = bcdToDec(;
  *month = bcdToDec(;
  *year = bcdToDec(;

void displayTime()
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  // retrieve data from DS3231
  readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  // send it to the serial monitor
  Serial.print(hour, DEC);
  // convert the byte variable to a decimal number when displayed
  if (minute<10)
  Serial.print(minute, DEC);
  if (second<10)
  Serial.print(second, DEC);
  Serial.print(" ");
  Serial.print(dayOfMonth, DEC);
  Serial.print(month, DEC);
  Serial.print(year, DEC);
  Serial.print(" Day of week: ");
  case 1:
  case 2:
  case 3:
  case 4:
  case 5:
  case 6:
  case 7:
bigString[0] = (char) second, DEC;

byte decToBcd(byte val)
  return( (val/10*16) + (val%10) );
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
  return( (val/16*10) + (val%16) );

You’ll need to convert the integer to printable ACSII. There are a few ways to do it, like itoa(), but sprintf() is easy. There are many formats to choose from, just look up the reference for it, but here’s an example to print a simple signed byte. You can use %u for unsigned or %x for hex, and a lot of other options.

char string[32];  // Don't go over 31 characters!
sprintf( string, "The value is %d\r\n", value );
Serial.print( string );
1 Like