Skip to content

Instantly share code, notes, and snippets.

@IdrisCytron
Last active January 2, 2020 08:22
Show Gist options
  • Save IdrisCytron/e1e159789713d967ca4a8119d9fda5c6 to your computer and use it in GitHub Desktop.
Save IdrisCytron/e1e159789713d967ca4a8119d9fda5c6 to your computer and use it in GitHub Desktop.
Particle event handler for IFTTT Google Assistant.
/*
Program with Particle Web IDE
Board (tested with):
- Raspberry Pi Zero WH
- Raspberry Pi 3 Model A+
*/
const int led1 = D1; // D1 is refer to GPIO17
const int led8 = D2; // D2 is refer to GPIO27
const int buzzer = D8;
bool led1State = LOW; // a virtual boolean variable
bool led8State = LOW; // a virtual boolean variable
bool led8PrevState = LOW;
long prevMillis = 0;
int interval = 500;
void setup()
{
pinMode(led1, OUTPUT); // led1 pin is set as output
pinMode(led8, OUTPUT); // led8 pin is set as output
pinMode(buzzer, OUTPUT);
digitalWrite(led8, LOW);
// Subscribe to events published by IFTTT using Particle.subscribe
Particle.subscribe("Light_On_Event", lightOnHandler); // Turning on function declaration
Particle.subscribe("Light_Off_Event", lightOffHandler); // Turning off function declaration
beep(2, 70);
}
void loop()
{
if (millis() - prevMillis > interval) {
led1State = !led1State;
digitalWrite(led1, led1State);
prevMillis = millis();
}
if (led8State != led8PrevState) {
led8PrevState = led8State;
digitalWrite(led8, led8State);
beep(1, 70);
}
}
// Our events are called when IFTTT applets are triggered
void lightOffHandler(const char *event, const char *data)
{
led8State = LOW;
}
void lightOnHandler(const char *event, const char *data)
{
led8State = HIGH;
}
void beep(int times, int delayMs)
{
do {
digitalWrite(buzzer, HIGH);
delay(delayMs);
digitalWrite(buzzer, LOW);
delay(delayMs);
} while (--times);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment