In today's lab, we will learn how to wire the sensor and handle it’s input boolean value which will trigger other events.
Steps
Import the gpiozero LED and MotionSensor modules
Connect and wire your circuit
Make sure to check the leads on the PIR sensor (remove the plastic cover to show VCC, GND and signal pins).
Make a while loop to check the status of the pins
Declare variables for the led and the pir, assign them the correct GPIO pin value based on your wiring.
Make a conditional that checks status or motion using one of the API commands.
Run and debug, adjusting your PIR sensor as needed.
The circuit uses GPIO4 for the motion sensor, but uses GPIO16 for the LED, and a ground connection on pin on the bottom pin (board pin 39, not GPIO)
Motion sensors use infrared light to detect the distance from the object in front of it. The sensor will will be using is the PIR sensor, which stands for Passive Infrared. Many of these sensors are built into motion activated systems or alarm systems. These sensors detect objects that produce heat and calculates the distance between the object and the sensor.
Once an event has been detected, (ie. the motion sensor detected an object approaching) we can trigger other events to handle the next steps.
from datetime import datetime
now = datetime.now()
#date and time format: dd/mm/YYYY #H:M:S
format = "%d/%m/%Y %H:%M:%S"
#format datetime using strftime()
time_formatted = now.strftime(format)
print(time_formatted)
import smtplib
from email.message import EmailMessage
#build the email with the EmailMessage() object.
msg = EmailMessage()
msg['Subject'] = "Hello!"
msg['From'] = "my.email@gmail.com"
msg['To'] = "somebody.else@email.com"
#set the body with .set_content()
msg.set_content("This is an email sent with Python!")
#use smptlib to send using gmail server, on port 465.
server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
#replace with your own credentials
server.login("<your gmail>", "<your password>")
#send email
server.send_message(msg)
print("sending email...")
server.quit()