Spark.publish not triggering on IFTTT[SOLVED]

I have the following code setup so when the LED is on, the Spark Core publishes a message that is supposed to trigger IFTTT to send a text:

int led1 = D7;
bool state = 0;
int LedState = 0;

void setup() {
    Spark.function("toggle", toggleLed);
    //Spark.variable("LedState", &LedState, INT);
    pinMode(led1, OUTPUT);
}

void loop() {

}

int toggleLed (String inputstate)
{
    if (inputstate == "On")
    {
        digitalWrite(led1, 1);
        LedState = 1;
        Spark.publish("LEDon");
        return 1;
    }
    if (inputstate == "Off")
    {
        digitalWrite(led1, 0);
        LedState = 0;
        return 0;
    }
    else
    {
        return -1;
    }
}

Is there an issue with IFTTT? This is basically how the examples are written. I can turn the LED on with an IFTTT “Do” but no event is triggered by the publish?

@camow7,

Can you point me to the document that says this is how it should be coded in the example? I didn’t managed to locate what you mentioned :wink:

You are calling Spark.publish() in the process of handling Spark.function() and i don’t think that’s possible.

You can in fact do that. I've been doing it for months, although it's not a recommend practice. It'd be better to set a flag in the function, and then handle that in the loop where it check for the flag.

1 Like

I’m lagging too far behind… haha!

Any reason why it didn’t work with IFTTT then? I would try the code above when i have time

1 Like

I’m guessing it’s an error in the configuration of the IFTTT settings. I just made a new recipe with one of my existing publishes (also called from within a function), and it works just fine.

@camow7, could you show us how you’ve set up IFTTT?

1 Like

Okay, I tried the exact code above, and it’s working perfectly over here. I send the “On” command, the LED toggles on, and after a few seconds IFTTT triggers a function call on a different Core.
Since your code is okay, it’s most likely a problem with the IFTTT setup.

1 Like

Ok so it was an issue on the IFTTT side. It turns out that if you publish something with only 1 argument:

Spark.publish("CurrentTime");

But then have text in the “is(Event Contents)” on IFTTT the event will not fire.
I didn’t realise your “is(Event Contents)” had to match your second argument exactly. I thought it stored the value of the second argument or ignored it if one wasn’t sent. I just cleared the “is(Event Contents)” field on my IFTTT recipe.

Problem solved!

1 Like