Creating Webhook for Weather Conditions for Fleet Of Products

You really want to pass the location, but you also want to the device to subscribe only to weather calls it made!

if you build your web hook using the response topic, each device can trigger the webhook and only respond to those calls it made. I call one webhook from many devices like this:

{
	"event": "current_weather",
	"url": "http://api.wunderground.com/api/yourAPIkey/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": "{{PARTICLE_DEVICE_ID}}_current_weather",
	"json": null,
	"auth": null,
	"coreid": null,
	"deviceid": null,
	"mydevices": true
}

the device must subscribe to all webhooks that contain its device ID in the response topic:

deviceID.toCharArray(responseTopic,125); // responseTopic is a global char array
Particle.subscribe(responseTopic, webhookHandler, MY_DEVICES);
sprintf(publishString, "{\"my-city\": \"%s\", \"my-state\": \"%s\" }", cityLocation, stateLocation);  

calling a webhook like this"

Particle.publish("current_weather", publishString, 60, PRIVATE);

your location specific variables are passed along to the webhook and your device knows it made that request, ignoring any other device’s use of that webhook. You merely sort it all out in the webhook handler:

void webhookHandler(const char *event, const char *data)
{
   if(strstr(event, "current_weather"))
  {
    gotWeather(event, data);
  }
  else if (strstr(event, "forecast_weather"))
  {
    gotForecast(event, data);
  }
  else if (strstr(event, "sun_time"))
  {
    gotSunTime(event, data);
  }
}
2 Likes