OK, so now that we have this new tool, I wanted to provide an example of how to use a single webhook on multiple devices returning location data. I am using my handy webhook that returns sunrise and sunset times for a city and state in the US:
{
"event": "sun_time",
"url": "http://api.wunderground.com/api/getYourOwnApiKey/astronomy/q/{{my-state}}/{{my-city}}.json",
"requestType": "POST",
"headers": null,
"query": null,
"responseTemplate": "{{#sun_phase}}{{sunrise.hour}}~{{sunrise.minute}}~{{sunset.hour}}~{{sunset.minute}}~{{/sun_phase}}",
"responseTopic": "{{SPARK_CORE_ID}}_sun_time",
"json": null,
"auth": null,
"coreid": null,
"deviceid": null,
"mydevices": true
}
notice the "responseTopic": "{{SPARK_CORE_ID}}_sun_time",
which inserts the unique device name into a webhook response name that includes a filter for the webhook to identify “hey, I need to respond to this since I called for it!!!”
here is an example that you can enter your city and state:
struct deviceTime{
int Hour;
int Minute;
};
const String city = "Weston"; //unique location for my Photon
const String state = "FL"; //unique location for my Photon
deviceTime sunrise = {6,0};
deviceTime sunset = {18,30};
char message[80] = "No Time Values Recieved";
bool startUpFlag = true;
unsigned long myTimer = 0;
void setup()
{
String responseTopic = System.deviceID() + "_sun_time"; //<< define your custom responseTopic using ID and unique appendage
Particle.variable("SunriseTimes", &message, STRING); //<< let's look at the results in the Cloud Variables
//Particle.publish("pushover", responseTopic, 60, PRIVATE); //<< I'm texting the data to my phone for debug
Particle.subscribe(responseTopic, sunTimeHandler, MY_DEVICES); //<< subscribe to the event
}
void loop()
{
if((millis() - myTimer > 60 * 1000UL) || startUpFlag)
{
startUpFlag = false;
String publishString = "{\"my-city\": \"" + city + "\", \"my-state\": \"" + state + "\" }"; //<< kind of complicated, but passes locations into string
Particle.publish("sun_time", publishString, 60, PRIVATE);
myTimer = millis();
}
}
void sunTimeHandler(const char * event, const char * data)
{
String sunriseReturn = String(data);
char sunriseBuffer[25] = "";
sunriseReturn.toCharArray(sunriseBuffer, 25);
sunrise.Hour = atoi(strtok(sunriseBuffer, "\"~"));
sunrise.Minute = atoi(strtok(NULL, "~"));
sunset.Hour = atoi(strtok(NULL, "~"));
sunset.Minute = atoi(strtok(NULL, "~"));
char buffer[80] = "";
sprintf(buffer, " Sunrise: %02d:%02d, Sunset: %02d:%02d", sunrise.Hour, sunrise.Minute, sunset.Hour, sunset.Minute);
String pubString = city + ", " + state + buffer; // why not customize the message, too?
//Particle.publish("pushover", pubString, 60, PRIVATE); // calls another webhook!!!
strcpy (message, pubString);
}
and the response, note the magic happens on the third line:
and in Particle Dev:
Thanks @Dave for the great new tool!!!