Turning Webhook Data into Variable

Hi everyone! I’m pretty new to Particle and C++ in general, but I just can’t seem to figure this out. I’m a little dense, so forgive me, but I’ve tried every tutorial I can find and can’t figure this out. My hook works, and when I check the console I can see that the hook response returns with the data I need, but I can’t for the life of me figure out how to take the response data and turn it into a variable. I’m trying to make a display that shows how long it is until the next bus arrives using the API provided by the city. Here’s my code:

int bus;
void setup() {
  // Subscribe to the integration response event
  Particle.subscribe("hook-response/bustime", busResponse, MY_DEVICES);
}


void loop() {
  // Trigger the integration
  Particle.publish("bustime");
  // Wait 10 seconds
  delay(10000);
}
      
void busResponse(const char *event, const char *data) {
    //I've only been able to try stuff from tutorials here but to no avail
}

The Webhook definitely works, and I’m getting the response I need (It’s only one number). I would, in theory, make the variable bus equivalent to the time until the next bus arrives. I just can’t wrap my head around how to take the data from the hook response, and nothing I’ve tried has worked so far. At least from what I’ve seen. Maybe my testing methods are flawed. Thanks for the help, and sorry for the stupid question!

1 Like

@dpriester2, the const char *data part of your subscription callback is a pointer to the ASCII data returned by the webhook. You just need to convert it to a suitable format, which I assume is an integer. Here is an example:

// This is a global variable
int bus_time;

void busResponse(const char *event, const char *data) {
    //I've only been able to try stuff from tutorials here but to no avail
    bus_time = atoi(data);
}

The atoi() function converts the ASCII text representing the number into an integer value. Take a look here: http://www.cplusplus.com/reference/cstdlib/atoi/

If your webhook data contains more values, you can parse those numbers using functions such as strtok() for example. The forum is full of examples of folks parsing webhook return data.

2 Likes

Just slightly back-tracking from @peekay123's "solution".

One of the first steps when you are expecting/receiving data is to print it out and have a look at what you get.

void busResponse(const char *event, const char *data) {
  // show me what we've got
  Serial.println(event);
  Serial.println(data);
  Serial.println();
}

With that info, you can either find out how to move on or repost that output for others to have a look at and actually see what you need instead of having to guess

2 Likes

Hi! I put in your code for the serial. It worked, and now I think this next part might be a problem on my end with being unfamiliar with the Desktop IDE. My code at the moment is:

 int bus_time;
 void setup() {
   // Subscribe to the integration response event
   Particle.subscribe("hook-response/bustime", myHandler, MY_DEVICES);

 }


 void loop() {
   // Trigger the integration
   Particle.publish("bustime");
   // Wait 10 seconds
   delay(10000);
 }

 void myHandler(const char *event, const char *data) {
     bus_time = atoi(data);
     Serial.print(bus_time);
 }

As you can see, i replaced your serial prints with one that displays the variable. However, it has not stopped printing event followed by data. I’m sure this is another newbie mistake but I can’t check to make sure the value of the variable is changing as I’d like it. The output is

hook-response/bustime/0
21

The number does keep changing so I think it might just be that for some reason my serial hasn’t updated and is just perpetually spitting out my initial input. Any thoughts would be appreciated, but I’m thankful to both of you for helping me figure this out

I guess the new code wasn't successfully flashed to your device.
How did you flash your device?