Another approach would be avoid the potential latency of IFTTT by using a webhook to a service like weatherunderground. Your webhook would look like this:
{
"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}}~{{#moon_phase}}{{current_time.hour}}~{{current_time.minute}}{{/moon_phase}}~{{/sun_phase}}",
"responseTopic": "{{PARTICLE_DEVICE_ID}}_sun_time",
"json": null,
"auth": null,
"coreid": null,
"deviceid": null,
"mydevices": true
}
in setup() you would declare like this:
responseTopic = System.deviceID(); // responseTopic is a Global String
// and...
Particle.subscribe(responseTopic, webhookHandler, MY_DEVICES);
// and...
strcpy(publishString, "{\"my-city\": \""); // publichString is a Global char array
strcat(publishString, cityLocation); // your City
strcat(publishString, "\", \"my-state\": \"");
strcat(publishString, stateLocation); //your state/province ... or refer to WU API for international cities
strcat(publishString, "\" }");
call it in loop() like this"
[]() {
static unsigned long lastMillis = 0;
if (millis() - lastMillis > 60 * 60 * 1000UL) {
Particle.publish("sun_time", publishString, 60, PRIVATE);
lastMillis = millis();
}
}();
and handle it like this:
void webhookHandler(const char *event, const char *data)
{
if (strstr(event, "sun_time"))
{
gotSunTime(event, data);
}
}
void gotSunTime(const char * event, const char * data)
{
char sunriseBuffer[125] = "";
strcpy(sunriseBuffer, data); // data is immutable, so we have to make a copy to tokenize
Sunrise.theHour = atoi(strtok(sunriseBuffer, "\"~"));
Sunrise.theMinute = atoi(strtok(NULL, "~"));
Sunset.theHour = atoi(strtok(NULL, "~"));
Sunset.theMinute = atoi(strtok(NULL, "~"));
int currentHour = atoi(strtok(NULL, "~"));
int currentMinute = atoi(strtok(NULL, "~"));
Time.zone(0);
int newTimeZone = utcOffset(Time.hour(), currentHour);
if(newTimeZone != savedTimeZone) // using EEPROM here to save the time zone for startup...
{
savedTimeZone = newTimeZone;
Time.zone(savedTimeZone);
EEPROM.put(2 * sizeof(int), savedTimeZone);
Particle.publish("pushover", "Time Zone updated...r");
}
Time.zone(savedTimeZone);
}
int utcOffset(int utcHour, int localHour) // sorry Baker Island, this won't work for you (UTC-12)
{
if (utcHour == localHour)
{
return 0;
}
else if (utcHour > localHour)
{
if (utcHour - localHour >= 12)
{
return 24 - utcHour + localHour;
}
else
{
return localHour - utcHour;
}
}
else
{
if (localHour - utcHour > 12)
{
return localHour - 24 - utcHour;
}
else
{
return localHour - utcHour;
}
}
}
which would also adjust for your local time zone…