from machine import Pin
from datetimetime import datetime
import smtplib
from email.mime.text import MIMEText
# GPIO setup
motion_sensor_pin = 28
red_led_pin = 15
Pin.setup(motion_sensor_pin, Pin.IN)
Pin.setup(red_led_pin, Pin.OUT)
# Email setup
sender_email = "[email protected]"
sender_password = "07790"
receiver_email = "[email protected]"
smtp_server = "smtp.example.com"
smtp_port = 587
def send_email(subject, body):
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(sender_email, sender_password)
msg = MIMEText(body)
msg['From'] = sender_email
msg['To'] = receiver_email
msg['Subject'] = subject
server.sendmail(sender_email, receiver_email, msg.as_string())
server.quit()
# Main loop
try:
while True:
if Pin.input(motion_sensor_pin):
print("Motion detected!")
Pin.output(red_led_pin, GPIO.HIGH)
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
subject = "Shed Intrusion Detected"
body = f"Intrusion detected at {current_time}. The red LED has been activated."
send_email(subject, body)
# Wait for 60 seconds before checking again
time.sleep(60)
GPIO.output(red_led_pin, GPIO.LOW)
time.sleep(1) # Check for motion every second
except KeyboardInterrupt:
Pin.cleanup()