Integer to string (Spark.publish)

A code example for Spark.publish gives "Spark.publish("temperature, “19 F”);

It would be useful to see example code where a derived integer value is being published. Ideally an example that includes creating a string array, allocating a pointer and publishing.

Sorry, but this is for a C novice struggling with finding/choosing an appropriate library function and wrestling with pointers - all via the web IDE.

Have a look at these:

http://docs.spark.io/firmware/

My thanks, it’s OK I understand what Publish is all about and have used it for events. I just want to send one value in this case.

From the example I think I need:

char publishString[40];
unsigned long lastTime = 0UL;
int my_value;
void setup() {
}

void loop() {
    unsigned long now = millis();
    //Every 15 seconds publish uptime
    if (now-lastTime>15000UL) {
 sprintf(publishString,my_value);
 Spark.publish("Uptime",publishString);
}

Is that right and do I need to load a specific library for sprintf?

My thanks.

Even though you think you understand the events, I’d still highly recommend that you try out the above mentioned tutorials. They do, and explain, the exact thing you’re trying to do. It’s not hard to go through, and very informative!
There are some syntax errors in your code, which will keep it from compiling in the first place.
Whether or not you need a library for Sprintf is also in the above tutorials, so please do try them out before going any further.

Let us know if you don’t understand any specifics. Good luck!

1 Like

sprintf is in the IDE's C++ implementation... and a few minor changes/typos in your code...

char publishString[40];
unsigned long lastTime = 0UL;
int my_value = 1;
void setup() 
{
}

void loop() 
{
    unsigned long now = millis();
    if (now - lastTime > 15000UL) 
    {
      sprintf(publishString, "%d", my_value); //  see what I did there?
      Spark.publish("Uptime", publishString);
      lastTime=millis();
    }
}
3 Likes

Thanks for the reply. It was late at night when I posted and I marked up the code without testing, primarily because I was really only interested in the string handling element. The code now compiles; whether or not my wider code works is another matter.

Where do I find out what is included within the default core code library (things like sprintf)?

Also I now know how to find the tutorials. I think it can only be done via the home page (Community), whereas I spend most of my time in the IDE.

1 Like

The Core uses Wiring, the same programming language that Arduino uses.

you can read about that here. A head's-up would be that there are some components that are still not as evolved at Arduino, so if you come across a problem (like String class) you can always come back here to see if others have shared your issue.

Also, the docs contain quite a bit of information on the functions you can use. Worthwhile checking out.