Author Topic: Automated IFTTT and Particle.io functions  (Read 4093 times)

Joelbear5

  • Newbie
  • *
  • Posts: 2
Automated IFTTT and Particle.io functions
« on: March 18, 2016, 09:05:33 am »
The automation site IFTTT.com can connect with particle.io and the functions you register in the code. I have successfully connected IFTTT (pronounced 'ift') to the Oak. Full plan is to have multiple IFTTT 'recipes' that send values to the Oak. I'll start with Sunset and Sunrise triggers to send value to particle function, as well as a trigger when my phone is near my home, to trigger my living room lights to come on when I come home after sunset, or if I'm home and sunset arrives.

exeng

  • Sr. Member
  • ****
  • Posts: 452
Re: Automated IFTTT and Particle.io functions
« Reply #1 on: March 18, 2016, 11:01:14 am »
Would love to see your example of this.

PeterF

  • Hero Member
  • *****
  • Posts: 877
Re: Automated IFTTT and Particle.io functions
« Reply #2 on: April 03, 2016, 03:18:17 am »
How about this for an incredibly boring yet enlightening example? Is basically a Oak vampire... blinks when the sun is down! 8) I have two recipes on IFTTT that simply call the function on particle with either "sunset" or "sunrise" as the arguments when the weather event triggers for sunrise or sunset. And for testing, if you have the Particle CLI installed, you can call the function using particle call <devicename> <functionname> <arguments> ('particle call Oak4 blinkLED sunset' or 'particle call Oak4 blinkLED sunrise' in my case).

btw, variables and functions appear to be an incredible PITA... they don't seem to register half the time... and at first I thought it was just me... :-[  but then I see quite a few mentions of it on the Particle community forums...

Edit: As an addendum, if I instead do a Particle.disconnect(); then register the function Particle.function("blinkLED", sunriseOrSunset); and then reconnect Particle.connect(); the function appears to register every time. Very strange. I originally had a do-while loop as Particle.function() is supposed to return a true/false status flag, but it wasn't doing squat as it would always return true even when the function wasn't registered. Anyway, it works great now with a disconnect before and a connect afterwards!

Code: [Select]
bool blinkLED = false;

int sunriseOrSunset(String command);

void setup()
{
  pinMode(BUILTIN_LED, OUTPUT);
  digitalWrite(BUILTIN_LED, LOW);

  // register the cloud function - should be able to do this without a disconnect and connect, but seems to be
  //   buggy/intermittent if I don't - may sometimes the device connects to particle before function() is
  //   called and doesn't work properly when connect has already been called?
  Particle.disconnect();

  Particle.function("blinkLED", sunriseOrSunset);

  Particle.connect();
}

void loop()
{
  if (blinkLED == true)
  {
    digitalWrite(BUILTIN_LED, HIGH);
    delay(100);
    digitalWrite(BUILTIN_LED, LOW);
    delay(100);

    digitalWrite(BUILTIN_LED, HIGH);
    delay(100);
    digitalWrite(BUILTIN_LED, LOW);
    delay(1700);
  }
  else
  {
    digitalWrite(BUILTIN_LED, HIGH);
    delay(100);
    digitalWrite(BUILTIN_LED, LOW);
    delay(100);

    delay(9800);
  }
}


int sunriseOrSunset(String command)
{
  //argument max of 64 characters long
  if (command == "sunset")
  {
    blinkLED = true;
  }
  else if (command == "sunrise")
  {
    blinkLED = false;
  }
  else
  {
    return -1;
  }

  return 1;
}
« Last Edit: April 05, 2016, 02:03:09 am by pfeerick »

driffster

  • Newbie
  • *
  • Posts: 42
Re: Automated IFTTT and Particle.io functions
« Reply #3 on: May 15, 2016, 06:20:26 pm »
This works great, I wanted to do repeated task at a specific time, and with IFTTT you can call a function at any time, no need to keep track of time internally with a RTC.

Thank you for pointing out that service!