#Project 5 - intruder_alarm.py #import lib from gpiozero import LED, Button, MotionSensor import smtplib from email.mime.text import MIMEText from signal import pause #PI PINs led_triggered = LED(18) button = Button(2) pir = MotionSensor(4) #Variablen motion_sensor_status = False email_sent = False #PIR Alarm/OK def arm_motion_sensor(): global email_sent global motion_sensor_status if motion_sensor_status == True: motion_sensor_status = False led_status.off() led_triggered.off() else: motion_sensor_status = True email_sent = False led_status.on() #Email bei Alarm senden def send_email(): global email_sent global motion_sensor_status if(motion_sensor_status == True and email_sent == False): #Eigenen Account eintragen from_email_addr = 'YOUR_EMAIL@gmail.com' from_email_password = 'YOUR_EMAIL_PASSWORD' to_email_addr = 'TO_YOUR_OTHER_EMAIL@gmail.com' #set your email message body = 'Bewegung erkannt' msg = MIMEText(body) #set send and recipient msg['From'] = from_email_addr msg['To'] = to_email_addr #set your email subject msg['Subject'] = 'Warnung!' #connect to server and send email #edit this line with your provider's SMTP server details server = smtplib.SMTP('smtp.gmail.com', 587) #comment out this line if your provider doesn't use TLS server.starttls() server.login(from_email_addr, from_email_password) server.sendmail(from_email_addr, to_email_addr, msg.as_string()) server.quit() email_sent = True led_triggered.on() print('Email Sent') #Funktion, wenn die Taste gedrückt wird button.when_pressed = arm_motion_sensor #Funktion, wenn Bewegung erkannt wird pir.when_motion = send_email pause()