Setting a spark.variable from a webhook response

i have a webhook set up and working, mostly based off of your get_weather example.
I would like to take the response from the webhook and store it in a spark.variable.
Since i have set my spark.variable up as “char *tempStr;” and Spark.variable(“tempStr”, tempStr, STRING); similar to how you do it in your variable examples i am getting an error when trying to assign the webhook String back to the variable which is a char *.
any help would be great.

tix.cpp: In function ‘void gotWeatherData(const char*, const char*)’:
tix.cpp:244:13: error: cannot convert ‘String’ to ‘char*’ in assignment

@b3ko, you can’t “set” a Spark.variable() as it is a “read only” function. You need to use Spark.function() to set an internal variable with a pass string. :smile:

@peekay123 what i mean is this.

i want to do this:

char *weather;
void setup()
{
  Spark.variable("weather", weather, STRING);
....
//webhook code.....
String condition = tryExtractString(str, "<weather>", "</weather>");
weather = condition;
.....

this will allow me to call “spark monitor weather” from the CLI and see the data on the command line.
So i will set it in the code and monitor from the CLI.

however when i try something like this it complains because i have a char * and a string. I just don’t know enough about c++ to get that corrected.
If i had a double coming back from the webhook and a double spark.variable i would be able to get that to work but some of what i need is in string.

thanks for the help.

Hi @b3ko

You should not declare a Spark.variable with a pointer like this. What happens is that the Spark.variable remembers the memory address pointed to by weather, but later on you change what weather points to and the Spark.variable does not change.

Try something like this:

char weather[256];  //however long you need upto around 622 bytes

//in setup
  Spark.variable("weather",weather,STRING);

//in webhook handler
  String condition = tryExtractString(str, "<weather>", "</weather>");
  condition.toCharArray(weather,256);  //copy into weather

thanks @bko!

You should not declare a Spark.variable with a pointer like this.

good to know, i was following the spark docs:
http://docs.spark.io/firmware/

char *message = "my name is spark";
void setup()
{
  // variable name max length is 12 characters long
  ...
  Spark.variable("mess", message, STRING);
  ...
}

i will try your code tonight, when they unchain me from my desk.

Right but your example does not allocate any storage while the Spark doc does. In other words, your Spark variable doesn’t point to anything.

1 Like

@bko AHhhhhhh! hand slap to my head. of course. thanks for setting me straight.

2 Likes

@bko So i got my code working. thanks so much for your help.
One thing i wanted to point out is that i was still using your SparkTime library to grab the time.
I was having issues getting my webhook to work with my spark.variable and it was driving me nuts.
I gutted my code so it was just a spark.variable and a webhook. the webhook callback method sets the variable and as soon as i ripped out all the other code it started to work as expected. i started adding the rest of my code back in, method by method and as soon as i added this little gem:

currentTime = rtc.now();
hour = rtc.hour(currentTime);
min = rtc.minute(currentTime);

it broke again. all i saw when i monitored the variable was empty strings. if i subscribe mine i see the webhooks returning my xml but the variable is blank. if i comment out the rtc code, the spark.variable works fine.

a little google search brought me to this: > https://community.spark.io/t/sparktime-vs-time-library/9810
and i removed the sparkTime library, updated my code to Time.Hour(), etc and now all is working.
I had to add the Time.Zone(-4) to get the time correct and now will have to manage time changes due to DST but at least it is working.

I just wanted to let you know because i know you wrote that library and because others may experience the same issue.

May be worth looking into why exactly this happened as other libraries may have similar issues.

let me know if want my complete code so you can look into to it more.

best,
jon

Hi @b3ko

Glad you got it working and thanks for the report. If you would like to send me your code, I will look into it. With Spark variables, it is easy to get confused about what is being pointed to in memory so it is likely a memory issue

1 Like

@bko tonight i will take my bare bones script, with 1 webhook, 1 spark.variable, and some sparkTime function and see if it works. this will be the minimum amount of code to reproduce the issue if it is indeed due to the sparkTime lib. I will then comment out th esparkTime stuff, but the native Time stuff in and see if it works. once i do this little experiment i will send it to you if my thoughts are confirmed.
have a good day.
-b3ko

1 Like