Spark.publish variable

When using the spark.publish() call, how do I insert a variable into the data type? If I’m reading the docs correct, the first condition is the name of the event you what to publish. The second is the data being published where I’d like to insert a variable fahrenheit

If it helps, I’m trying to publish the temps from a one wire temp sensor using the code from here:

Hi @Jackalopes

The actual second argument to Spark.publish() is a char* C string, so you can use sprintf or any other method to get your value into string format.

Have you seen this tutorial?


Hi @bko

Yes, I did read through both of your tutorials and tried to use sprintf(publishString, fahrenheit); but was greeted with compile errors.

I think it might have something to do with float celsius, fahrenheit; instead of char or int

Still pretty new to all of this and thanks for the help!

1 Like

So you want something more like this:

char tmpStr[64];
sprintf(tmpStr, "%f", fahrenheit);  //assuming fahrenheit is a float variable
Spark.publish("eventname", tmpStr);

That did the trick @bko!