So I am using a single webhook to retrieve weather from weatherunderground, but the devices are in different locations. I pass an argument(s) during the publish event to the webhook. So, let’s first look at the webhook (which I use all the time).
{
"event": "current_weather",
"url": "http://api.wunderground.com/api/getYourOwnApiKey/conditions/q/{{my-state}}/{{my-city}}.json",
"requestType": "POST",
"headers": null,
"query": null,
"responseTemplate": "{{#response}}{{#current_observation}}{{#estimated}}{{weather}}~{{temp_f}}~{{relative_humidity}}~{{wind_dir}}~{{wind_gust_mph}}~{{dewpoint_f}}~{{/estimated}}{{/current_observation}}{{/response}}",
"responseTopic": "{{SPARK_CORE_ID}}_current_weather",
"json": null,
"auth": null,
"coreid": null,
"deviceid": null,
"mydevices": true
}
Notice in particular the {{my-state}}
and {{my-city}}
which are passed along in the url. Also, look where {{SPARK_CORE_ID}}
is passed along as the first part of the responseTopic.
So, taking snippets from my code, I make calls to the same webhook from different devices with different deviceID’s (obviously) and the device itself is looking for any event that it asked for, like this:
Particle.subscribe(responseTopic, webhookHandler, MY_DEVICES);
you see… webhookHandler() is called for every event that that particular device ‘asks’ for.
I build the publish event like this, which you could easily dynamically set with the press of a button:
sprintf(publishString, "{\"my-city\": \"%s\", \"my-state\": \"%s\" }", cityLocation, stateLocation);
...
...
Particle.publish("current_weather", publishString, 60, PRIVATE); //<<<< note the event name here!
and then webHookHandler()
looks like this:
void webhookHandler(const char *event, const char *data)
{
if(strstr(event, "current_weather"))
{
gotWeather(event, data);
}
else if (strstr(event, "sun_time"))
{
sunTimeHandler(event, data);
}
}
FYI, sun_time
is a similarly structured webhook that gets me Sunrise, Sunset and DST offset, so here you go:
void sunTimeHandler(const char * event, const char * data)
{
String sunriseReturn = String(data);
char sunriseBuffer[125] = "";
sunriseReturn.toCharArray(sunriseBuffer, 125); // example: \"5~37~20~30~\"
webhookSunrise.theHour = atoi(strtok(sunriseBuffer, "\"~"));
webhookSunrise.theMinute = atoi(strtok(NULL, "~"));
webhookSunset.theHour = atoi(strtok(NULL, "~"));
webhookSunset.theMinute = atoi(strtok(NULL, "~"));
tvOnTime = webhookSunset;
//Particle.publish("pushover", "Got times", 60, PRIVATE);
}
and the webhook, if you are interested there too… the same structure:
{
"event": "sun_time",
"url": "http://api.wunderground.com/api/getYouOwnApiKey/astronomy/q/{{my-state}}/{{my-city}}.json",
"requestType": "POST",
"headers": null,
"query": null,
"responseTemplate": "{{#sun_phase}}{{sunrise.hour}}~{{sunrise.minute}}~{{sunset.hour}}~{{sunset.minute}}~{{#moon_phase}}{{current_time.hour}}~{{current_time.minute}}{{/moon_phase}}~{{/sun_phase}}",
"responseTopic": "{{SPARK_CORE_ID}}_sun_time",
"json": null,
"auth": null,
"coreid": null,
"deviceid": null,
"mydevices": true
}
I hope that helps!