Spark Events + IFTTT

Hi,
I need some help regarding integrating IFTTT and Spark. I’m trying to use the new IFTTT DO app from my iOS to turn on and off a pin on Spark. Here is my script. However, it’s not working. Can someone tell me what is wrong with it?

int ledStatus(String command);

int onTime = -10000;
void ledOn(const char *event, const char *data) {
  onTime = millis();
}

void ledClose(const char *event, const char *data){
   onTime = millis(); 
}


void setup() {
    Spark.function(“led”, ledStatus);
    pinMode(D7,OUTPUT);
    Spark.subscribe("light-on”, ledOn, MY_DEVICES);
    Spark.subscribe(“light-off”, ledOff, MY_DEVICES);
}


void loop() {
  bool isOn = digitalRead(D7);
  if (millis() - onTime < 500) {
    if (!isOn) {
      digitalWrite(D7, HIGH);
    }
  } else {
    if (isOn) {
      digitalWrite(D7, LOW);
    }
  }
}


int ledStatus(String command)
{
    if (command == “on”)
    {
        digitalWrite(D7,HIGH);
        return 1;
    }
    else if (command == “off”)
    {
        digitalWrite(D7,LOW);
        return 1;
    }
    else return -2;
}

At the IFTTT end, I’ve one “Spark to Spark Recipe” which will actually monitor for event name and data to activate the function stored on the Spark. On the IFTTT DO, there is a private recipe to publish the event with event name and data.

I couldn’t get your code to even compile, was that what didn’t work?

the character ” (sometimes referred to as smart quotes in a Rich document) are not the same as " you have smart quotes in the code you posted, but that may be just a cut/paste problem.

it looks like you changed the variable name here…

void ledClose(const char *event, const char *data){
   onTime = millis(); 
}
...
...
Spark.subscribe(“light-off”, ledOff, MY_DEVICES);
...

so this will now compile:

int ledStatus(String command);

int onTime = -10000;
void ledOn(const char *event, const char *data) {
  onTime = millis();
}

void ledOff(const char *event, const char *data){
   onTime = millis(); 
}


void setup() {
    Spark.function("led", ledStatus);
    pinMode(D7,OUTPUT);
    Spark.subscribe("light-on", ledOn, MY_DEVICES);
    Spark.subscribe("light-off", ledOff, MY_DEVICES);
}


void loop() {
  bool isOn = digitalRead(D7);
  if (millis() - onTime < 500) {
    if (!isOn) {
      digitalWrite(D7, HIGH);
    }
  } else {
    if (isOn) {
      digitalWrite(D7, LOW);
    }
  }
}


int ledStatus(String command)
{
    if (command == "on")
    {
        digitalWrite(D7,HIGH);
        return 1;
    }
    else if (command == "off")
    {
        digitalWrite(D7,LOW);
        return 1;
    }
    else return -2;
}

@BulldogLowell, Sorry, that was a mistake. I was working on some modifications and I posted this question in the interim. I’ve already corrected the mistake and it compiled properly. The problem was not about getting it to compile. I couldn’t get the IFTTT to trigger the event and get the LED to turn ON. I can do it via CLI via the functions.

Here is a slightly modified version which works partially. I was able to use IFTTT DO app to actually turn the LED ON. Unfortunately it won’t go OFF. Not sure what’s wrong.

int doorStatus(String command);

int onTime = -10000;
void ledOn(const char *event, const char *data) {
  onTime = millis();
}

void doorClose(const char *event, const char *data) {
  onTime = millis();
}


void setup() {
    Spark.function("door", doorStatus);
    pinMode(D7,OUTPUT);
    Spark.subscribe("light-up", ledOn, MY_DEVICES);
    Spark.subscribe("door-close", doorClose, MY_DEVICES);
}




void loop() {
    if (millis() - onTime <1000) {
       if (ledOn) {
           digitalWrite(D7,HIGH);
       }
       else {
           if (doorClose) {
               digitalWrite(D7,LOW);
           }
       }
    }
}




int doorStatus(String command)
{
    if (command == "open")
    {
        digitalWrite(D7,HIGH);
        return 1;
    }
    else if (command == "close")
    {
        digitalWrite(D7,LOW);
        return 1;
    }
    else return -2;
}

Unrelated to the actual problem; why would you want to use a Spark.subscribe()? You could just trigger the function directly by using a function trigger. This eliminates having to do DO -> Event -> Function but rather DO -> function. Seems less prone to errors.

1 Like

It looks like you are trying to power a solenoid to unlock a door for a second:

It also looks as if you are wishing to see the status of the door lock using Spark.subscribe( ).

So, try this, but I am replacing your Strings with integers 'cuz I like simplicity… I compiled it but it is not tested:

int lockState = -1;

unsigned long onTime; 
void ledOn(const char *event, const char *data) {  //<<<<<<I didn't change these, but they need a look
  onTime = millis();
}

void doorClose(const char *event, const char *data) {
  onTime = millis();
}

void setup() {
    Spark.function("door", doorStatus);
    pinMode(D7,OUTPUT);
    Spark.subscribe("light-up", ledOn, MY_DEVICES);
    Spark.subscribe("door-close", doorClose, MY_DEVICES);
    Spark.variable("DoorState", &lockState, INT); // help you debug
}

void loop() 
{
    if (millis() - onTime > 1000UL)  
      digitalWrite(D7,LOW);
    //
    lockState = digitalRead(D7);
}

int doorStatus(String command)
{
    if (command.toInt() == 1)
    {
        digitalWrite(D7,HIGH);
        onTime = millis();
        return 1;
    }
    else if (command.toInt() == 0)
    {
        digitalWrite(D7,LOW);
        return 0;
    }
    else return -1;
}
1 Like

@Moors7, I don’t see a direct function trigger on the new IFTTT DO app. All I see is creating events. I’m not sure if one can create a custom DO recipe yet like the rest of IFTTT recipes. If there is one, please do point me to that link. Thanks in advance.

@BulldogLowell, exactly! It’s a mini project to control door lock and get status of the door via the subscribe events. Let me try out your suggestion and will update soon. Thanks for the instructions.

Have you tried the fairly big “+” button?

1 Like

yes, it seems you have to create a DO button on your phone, not on the IFTTT website :frowning:

1 Like

Seeing as the apps are meant to be used on mobile devices, that’s not really strange, is it? I think a lot of the confusion lies in the fact that people use the search function for Spark, and then find the “event” example.

Hi,
I’m a newbie to Spark. I’m making a small project where I need to turn ON a pin (say D7) on spark via the new IFTTT DO feature. Here is the following code that I’ve been trying to use in conjunction with the IFTTT DO feature. It’s not working as expected. Can someone tell me what’s wrong?

int onTime = -10000;

void ledOn(const char *event, const char *data) {
  onTime = millis();
}

void setup() {
  pinMode(D7, OUTPUT);
  Spark.subscribe("light-up", ledOn);
}

void loop() {
  bool isOn = digitalRead(D7);
  if (millis() - onTime < 500) {
    if (!isOn) {
      digitalWrite(D7, HIGH);
    }
  } else {
    if (isOn) {
      digitalWrite(D7, LOW);
    }
  }
}

On the IFTTT DO, I’ve the following:

Then Publish (Event Name): light-up
The event includes (Data): ledOn

Upon clicking the DO on my iPhone, nothing happens on my Spark. What am I doing wrong?

Hi @sriram155,

Make sure the public/private settings in the IFTTT form match your subscription on your device. If the IFTTT publish is set to private, then your subscribe should be:

Spark.subscribe("light-up", ledOn, MY_DEVICES);

http://docs.spark.io/firmware/#spark-subscribe

I hope that helps! :slight_smile:

Thanks,
David

1 Like

Oh weird, my old message appeared! Sorry about the duplicate response :slight_smile:

2 Likes

Yes. Yesterday I was looking at their site and there was option only to create recipes for ifttt and not for do. Looks like it’s only possible from their mobile app.

@BulldogLowell , thanks for the snippet. However, it is not working the way I wanted the program to function. I’m going to use the suggestion @Moors7 highlighted (use DO -> Function for the time being.

Thanks all of you guys for helping me out. Much appreciated.

1 Like