Hook-response not calling handler function

Hi everyone. I’m using some webhooks for weather data, and I’m having an issue with the latest one I added to my code.

I can verify that the event occurs, and the data from the weather API makes it to the particle cloud; however, it never makes it to my device when hook-response is triggered.

Here’s the relevant parts of my code.

//CODE - WEBHOOKS//
Serial.println("Retrieving webhook data...");
Serial.println("");

Particle.publish("getWeather1");
Particle.subscribe("hook-response/getWeather1",gotWeather1,MY_DEVICES);
delay(4000);

Particle.publish("getForecast1");
Particle.subscribe("hook-response/getForecast1",gotForecast1,MY_DEVICES);
delay(4000);

Particle.publish("getWeather2");
Particle.subscribe("hook-response/getWeather2",gotWeather2,MY_DEVICES);
delay(4000);

Particle.publish("getWeather3");
Particle.subscribe("hook-response/getWeather3",gotWeathergotForecast3,MY_DEVICES);
delay(4000);

Particle.publish("getForecast4");
Particle.subscribe("hook-response/getForecast4",gotForecast4,MY_DEVICES);


//CODE - HANDLER "gotForecast4"//
void gotForecast4(const char *name, const char *data)
{
	String str4f = String(data);
	Serial.println("Forecast 4:");
	Serial.println(str4f);
	Serial.println("");
}

//END CODE//

To be clear, it is only the last subscription that doesn’t work; the rest are functioning perfectly. And, as I said, I can see hook-response/getForecast4 on the console event monitor, and it does indeed return JSON data, just like all my other webhooks.

Anyone have any clue why this one doesn’t get the data, though the other ones do?
Thanks in advance,
Brad

Are you setting up the subscriptions in setup() and publishing the events in loop()?
Subscriptions shall only be set up once, events can be published multiple times.

There is also a documented limit of four subscriptions, but you can subscribe to hook-response/get and then filter in the one handler.
https://docs.particle.io/reference/firmware/photon/#particle-subscribe-

Checking the return value would have revealed the false outcome.

You should also subscribe before you publish to avoid race conditions.

1 Like

That 4 subscription limit would do it. I’ll work on that.
Duly noted on the order of operations for subscribing and publishing.

To double check because I’ve not used the feature before, hook-response/Forecast would return data for events Forecast1, Forecast2, Forecast3, etc., correct?

Exactly that, and the event name (first parameter in handler) can be used to distinguish.

1 Like

Sounds good. Thanks for your help!

1 Like