Help with IFTTT to fade LED from sunset to sunrise


#1

I am trying to turn on an LED at sunset and off at sunrise, but I am having trouble wrapping my head around what I need to do to set it up in IFTTT. Here is my code to make the LED fade “breath”. Any help would be much appreciated. Thanks!

int led = D0;           // the pin that the LED is attached to
int brightness = 30;    // how bright the LED is
int fadeAmount = 3;    // how many points to fade the LED by

// the setup routine runs once when you press reset:
void setup()  { 
  // declare pin D0 to be an output:
  pinMode(led, OUTPUT);
} 

// the loop routine runs over and over again forever:
void loop()  { 
  // set the brightness of pin D0:
  analogWrite(led, brightness);    

  // change the brightness for next time through the loop:
  brightness = brightness + fadeAmount;

  // reverse the direction of the fading at the ends of the fade: 
  if (brightness == 30 || brightness == 255) {
    fadeAmount = -fadeAmount ; 
  }     
  // wait for 60 milliseconds to see the dimming effect    
  delay (60);                          
}

#2

Here is a project that may interest you:


#3

You’d need both the sunrise and sunset trigger to each trigger a Particle function, which in turn will do the thing you want it to do. So, IF {sunrise/sunset} THEN {particle function}.


#4

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…


#5

Thanks for the reply! Could you point me to a bit of example code so I can understand how it’s done. I have a hard time writing code but for some reason once I see it I can usually figure out how it works.
Thanks so much!