This project was made for extra credit in my measurement and instrumentation class but I also wanted it to be something that I would get some use out of. This allows me to make sure nobody has been into my apartment when I am out of town.
This code is only set up to work with Gmail addresses. If you do not have a Gmail address make one here
The first thing were going to do is wire our PIR sensor to the Rasberry Pi. On the PIR Sensor there are three pins. A VCC, OUT, and GND. Using female to female jumper cables connect the VCC to 5V, the OUT to GPIO pin4, and the GND to GND. You should also note the 2 orange phillips screw heads. These are the potentiometers that control the sensitivity (Sx) and timing (Tx). You will want the sensitivity on max (max clockwise) and the time on minimum (minimum clockwise). This will be the baseline and can be tuned later to your preference.
Boot your Raspberry PI like normal.
Open up the terminal and create a new file called pirsensor.py by typing:
>> nano pirsensor.py
Enter/ Copy the code into the file.
Edit the to_email_address, from_email_address, and to_email_password to correspond with your desired emails.
Grant you Gmail permission to use less secure apps by following the instructions here.
Press ctrl+O to save and then ctrl+X to quit.
Run your code by typing the following into the terminal:
>> sudo python3 pirsensor.py
You should notice that when motion is detected that the GPIO pin 4 reads HIGH, but when it is not detected GPIO pin 4 reads LOW. When the code reads HIGH your destination email should get an email saying "INTRUDER!".
Press ctrl+C when your done to halt the program. Now Your Done!
If your interested in attaching a camera to capture the photographic evidence there are instructions here.
import RPi.GPIO as GPIO import time sensor = 4 GPIO.setmode(GPIO.BCM) GPIO.setup(sensor, GPIO.IN, GPIO.PUD_DOWN) previous_state = False current_state = False while True: time.sleep(0.1) previous_state = current_state current_state = GPIO.input(sensor) if current_state != previous_state: new_state = "HIGH" if current_state else "LOW" print("GPIO pin %s is %s" % (sensor, new_state)) import smtplib server = smtplib.SMTP('smtp.gmail.com', 587) server.starttls() server.login("from_email_address", "from_email_password") msg = "INTRUDER!" server.sendmail("from_email_address", "to_email_address", msg) server.quit()
Did you replicate this project? Share it!I made one
Love this project? Think it could be improved? Tell us what you think!