Raspberry Pi Motion Sensor IFTTT
by Instructables_2020 in Circuits > Electronics
1077 Views, 4 Favorites, 0 Comments
Raspberry Pi Motion Sensor IFTTT
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:
Cut a small hole in the middle of the top of the box.
Attaching the PIR Sensor:
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:
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!