Raspberry Pi Motion Sensor IFTTT

by Instructables_2020 in Circuits > Electronics

1077 Views, 4 Favorites, 0 Comments

Raspberry Pi Motion Sensor IFTTT

86DF3B94-B402-40FD-88DE-1CE5DA5D0E02.jpeg
406B1BA6-A30B-493D-9597-D08D5FA50907.jpeg

Hello. I am a 4th grader and today we are going to make a IFTTT motion sensor

Supplies

1x little cardboard box

1x raspberry pi model b+ or a+ https://www.canakit.com/raspberry-pi-3-model-a-pl...

1x PIR sensor https://www.amazon.com/gp/product/B07KZW86YR/ref=p...
(Various) jumper wires

1x smart lightbulb (optional) https://wyze.com/wyze-bulb.html

Duct tape (optional) https://www.amazon.com/Duck-Brand-283264-Inches-Si...

ifttt account https://ifttt.com/

Cut:

79D2BC96-3F7D-4C50-9773-986511A8CD8D.jpeg

Cut a small hole in the middle of the top of the box.

Attaching the PIR Sensor:

30CEE509-C410-4EA9-8EBF-1C4C99793093.png
E5BF9FE0-1472-45B1-8F22-653DEB4C5A91.jpeg

Connect the PIR sensor’s pin labelled VCC to the 5V pin on the Raspberry Pi. This provides power to the PIR sensor.Connect the one labelled GND to a ground pin on the Pi (also labelled GND). This completes the circuit.Connect the one labelled OUT to any numbered GPIO pin on the Pi. In this example, we have chosen GPIO 4. The OUT pin will output a voltage when the sensor detects motion. The voltage will then be received by the Raspberry Pi.

Setup Webhooks:

2E04E769-B286-4FE6-A010-0B5F7BE9AFF7.jpeg

make a new applet, and then Select Webhooks. and then do the same with notifications.

Code:

For python 3 or thonny

#! /usr/bin/python # Importsimport RPi.GPIO as GPIOimport timeimport requests # Set the GPIO naming conventionGPIO.setmode(GPIO.BCM) # Turn off GPIO warningsGPIO.setwarnings(False) # Set a variable to hold the GPIO Pin identitypinpir = 17 # Set GPIO pin as inputGPIO.setup(pinpir, GPIO.IN) # Variables to hold the current and last statescurrentstate = 0previousstate = 0 try: print("Waiting for PIR to settle ...") # Loop until PIR output is 0 while GPIO.input(pinpir) == 1: currentstate = 0 print(" Ready") # Loop until users quits with CTRL-C while True: # Read PIR state currentstate = GPIO.input(pinpir) # If the PIR is triggered if currentstate == 1 and previousstate == 0: print("Motion detected!") # Your IFTTT URL with event name, key and json parameters (values) r = requests.post('https://maker.ifttt.com/trigger/YOUR_EVENT_NAME/with/key/YOUR_KEY_HERE', params={"value1":"none","value2":"none","value3":"none"}) # Record new previous state previousstate = 1 #Wait 120 seconds before looping again print("Waiting 120 seconds") time.sleep(120) # If the PIR has returned to ready state elif currentstate == 0 and previousstate == 1: print("Ready") previousstate = 0 # Wait for 10 milliseconds time.sleep(0.01) except KeyboardInterrupt: print(" Quit") # Reset GPIO settings GPIO.cleanup()

Enjoy!