In this section, we will use the Particle Photon to send a notification using IFTTT. When the motion sensor detects something, it will send a notification. First, we will have the following diagram for this project:
The Particle.publish() event will later act as an IFTTT trigger.
The code for the motion sensor is as follows:
#define PIR_PIN D0 // Replace D0 with the pin you used
#define MIN_TIME_BETWEEN_TRIGGERS 2000 // Time (in milliseconds) of no motion before a new trigger can occur
void setup() {
pinMode(PIR_PIN, INPUT);
}
void loop() {
// PIR_PIN goes HIGH when motion is detected, stays HIGH for a few seconds
if (digitalRead(PIR_PIN)) {
Particle.publish("motion-detected");
// store current time in variable
unsigned long motionTime = millis();
// wait until no motion has been detected for MIN_TIME_BETWEEN_TRIGGERS milliseconds before a new trigger can occur
while(millis() - motionTime < MIN_TIME_BETWEEN_TRIGGERS) {
if (digitalRead(PIR_PIN)) motionTime = millis();
}
}
}