IFTTT does not send anything to Particle

Hello,
I’m trying to control a relay with google assistant from the raspberry. When I flash, the Raspberry responds and turns on the relay, but if I try it through the voice command with google assistant it does not do anything.
This is the code.

#include "Particle.h"
int relay = D1; //pin to which relay is connected
int boardLed = D7;
bool vin = LOW; //a virtual boolean variable

// setup() is run only once, it's where we set up GPIO and initialise peripherals
void setup() {
    
  // Setup GPIO
  pinMode(relay,OUTPUT); // relay pin is set as output
  digitalWrite(relay,HIGH);
  // Subscribe to events published by IFTTT using Particle.subscribe
  Particle.subscribe("mylight_assistant_off", myHandler); //turning off function declaration
  Particle.subscribe("mylight_assistant_on", thisHandler); //turning on function declaration
}

// loop() runs continuously, it's our infinite loop.
void loop() {
     if (vin==HIGH)
     {
         digitalWrite(relay,LOW);
     }
else if (vin==LOW)
      {
         digitalWrite(relay,HIGH);
     }

}

//our events are called when IFTTT applets are triggered
void myHandler(const char *event, const char *data)
{
    vin=LOW;
}
void thisHandler(const char *event, const char *data)
{
     vin=HIGH;
}

And this is the applet

1 Like

Have you seen this thread? It is very similar to what you are doing.

Yes, I’ve seen it but it has not helped me (I’m a beginner). I want to know if the code is correct or there is an error.
Thank you.

I just saw that on the console appears this:

But nothing happens.

Just a few hints to try (and for coding practice)

  • I’d rather go with PRIVATE events and subscribing for MY_DEVICES
  • Try using more descriptive function names than myHandle and thisHandle - e.g. onHandler and offHandler
  • You can get the same behaviour as your current code in loop() via digitalWrite(relay, !vin); - or by just putting the digitalWrite() directly into the handlers
  • If you want it in loop() you may want to consider to only act when the state actually changes - like this
void loop() {
  static int prevState = vin;   // only initialised the first time, after that the previous state is retained

  if (vin == prevState) return; // bail out for no action required

  digialWrite(relay, !vin);     // assign inverted state to GPIO
  prevState = vin;              // remember current state
}

If the event hits the console but the device is not notified, the device is either not connected to the cloud or not running your code. AFAICT your code should be doing what you expect it to do.

For testing, you can also implement some Particle.function() to call from console, to check whether the connection is good or not. You can also test the event handlers by sending the event from console.

1 Like

Sorry, but I’m starting and I’m lost. If I test with basic code like this:

int led1 = D0; 

int led2 = D7; 

void setup() {

  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);

}


void loop() {
 
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);


  delay(1000);

 
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);

  delay(1000);
}

It works and turns on. When I do it through IFTTT I get to the console but the Raspberry does not respond. Could you pass me the code with the arrangements that you have told me? I do not understand how to implement it.
Thank you.

First of, do you only own a RPi or also a real Particle device?
For tests I’d currently cut out the relay and only work with the D7 LED.
Also make sure that your event going to the same account as your device is registered with.
Hence I suggested to use console.particle.io/devices to trigger the event for that device and see if it’s acting as expected when the event is sent by the console.

This would be one of the simplest approaches to start with

#include "Particle.h"  // only required if this code is NOT in an .ino file 
const int pinLED = D7;

void setup() {
  pinMode(pinLED, OUTPUT); // relay pin is set as output
  Particle.subscribe("mylight_assistant_", lightHandler, MY_DEVICES);

  // optional for testing
  Particle.function("test", testFn);   
}

void loop() { } // do nothing 

void lightHandler(const char *event, const char *data)
{
  // when event name contains "_on" the pin will go LOW otherwise HIGH
  digitalWrite(pinLED, strstr(event, "_on") == NULL);
}

// optional test function
int testFn(const char* arg) {
  int i = atoi(arg);
  Particle.publish(i ? "mylight_assistant_on" : "mylight_assistant_off", PRIVATE);
  return i;
}

This works on my Photon, but I haven’t tested with RPi

Thank you,
with the last code it works for me, but I’d like to know why my code does not work. I am a beginner and I would like to understand the code.
Sorry for asking so much.

1 Like

When I try your original code it works just the same.
But when you say, it doesn't work for you, what exactly do you mean by that or how do you judge whether it does or not?

Your code only controls D1 but not D7, hence you won't see any blue LED feedback whenever D1 changes state.

First, thank you for help me.
Now, I have this code that controls pin D7 and I configured IFTTT with this new event and the LED don't change of color.

#include "Particle.h"

int relay = D7; //pin to which relay is connected

bool vin = LOW; //a virtual boolean variable

// setup() is run only once, it's where we set up GPIO and initialise peripherals
void setup() {
    
  // Setup GPIO
  pinMode(relay, OUTPUT);// relay pin is set as output
  digitalWrite(relay, HIGH);
  // Subscribe to events published by IFTTT using Particle.subscribe
  Particle.subscribe("mylight_off", offHandler); //turning off function declaration
  Particle.subscribe("mylight_on", onHandler); //turning on function declaration
}

// loop() runs continuously, it's our infinite loop.
void loop() {
     if (vin==HIGH)
     {
         digitalWrite(relay,LOW);
     }
else if (vin==LOW)
      {
         digitalWrite(relay,HIGH);
     }

}

//our events are called when IFTTT applets are triggered
void offHandler(const char *event, const char *data)
{
    vin=LOW;
}
void onHandler(const char *event, const char *data)
{
     vin=HIGH;
}

(With you code still works)

For me your code works as expected when publishing the event from console as PUBLIC.
But when my code works and yours not without changing the IFTTT recipe then it’s probably because my code subscribes for MY_DEVICES - as mentioned in my first response - and yours doesn’t.

Background:
Particle.subscribe() without a scope parameter does only subscribe to PUBLIC events while Particle.subscribe(..., MY_DEVICES) only subscribes to PRIVATE events.

BTW, your event name you subscribe to has changed, so have you also changed the IFTTT recipe accordingly?

Thank you, I added MY_DEVICES in my code and now run without problems.

Last question, how can I set the GPIO to activate for a certain time in my code?

There are some discussions and multiple ways to do that.
But you could have a look at the TimeAlarms library for one way.