Trying to get particlePublish to publish data contained in a float type variable

I’ve successfully computed temp_F and am trying to publish it using particlePublish… particlePublish apparently only works with strings. How do I go about publishing the temperature?

@scraphound, take a look a the sprintf() command. It will print to a char string array which you can then publish, like this:

char pub[30];
sprintf(pub, "Termperature is %2.4f", temp_F);
Particle.publish("MyTemperature", pub, 60, PRIVATE);

The format used by sprintf() is described in the printf() documentation. Just be sure to allocate enough room in the char array for the entire text that will be produced plus a terminating NULL. :smile:


I dug this up reading an example program for Particle.publish() Apparently the "String() function does type conversion: This appears to be working:

String temperature = String(farenheit);

I’m still not even getting close to getting the right numbers out… but at least I’m getting something!
I’ll look at the docs for the TMP36G i/c and see if I can figure it out. thanks for the info.

Still, the snprintf() is the safer and better style.

1 Like