Help with Spark.variable String

Hi !

I defined a char* variable and declare it in the cloud, like this

char* rgb = "0;0;0";

void setup(){
    Spark.variable("rgb", rgb, STRING);

    Spark.subscribe("upColor", colorUpdate);

When the event upColor is published, this is what happened

void colorUpdate(const char *event, const char* data){
    rgb = strdup(data);
    // rgb = "10;10;10"; Doesn't work

I want to copy ‘data’ into ‘rgb’, but the value stay at “0;0;0” and is not updated. Even if I used the second line…

What’s wrong with my code ??

Thank you :smile:

You want to declare your string like this

char rgb[16] = "0;0;0";

Otherwise it will just be a pointer pointing to flash memory (unchangable) and this address will be stored in your Spark.variable().
Later when you reassign the pointer to a new string (location), your Spark.variable() won’t follow.

If you do it as above, you get a pointer in RAM and when updating the string, you can copy your new content there and hence the Spark.variable() will “see” the new content.

BTW: I wouldn’t use strdup() but sprintf() or strcpy() :wink:


It works with the char array, thank you :wink: