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;
}
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.
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.
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;
}
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?