#!/usr/bin/python
from gpiozero import MotionSensor
from datetime import datetime
import time
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from daemon import runner
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = '/tmp/foo.pid'
self.pidfile_timeout = 5
def run(self):
print("Attivazione sensore movimento")
pir = MotionSensor(4)
contatore = 0
time.sleep(1)
print("Sensore pronto")
while True:
if pir.motion_detected:
contatore += 1
#Console
print("Movimento rilevato : "+str(contatore))
#email
msg = MIMEMultipart()
msg['Subject'] = 'telecamera movimento'
msg['From'] = 'xx@gmail.com'
msg['To'] = 'xx@gmail.com'
text = MIMEText("Movimento rilevato alle "+datetime.now().strftime("%H.%M.%S del %d/%m/%Y"))
msg.attach(text)
s = smtplib.SMTP('smtp.gmail.com', 587)
s.ehlo()
s.starttls()
s.ehlo()
s.login('xx@gmail.com', 'psw')
try:
s.sendmail('xx@gmail.com', 'xx@gmail.com', msg.as_string())
print("Email inviata")
except:
print("Email non inviata")
s.quit()
time.sleep(2)
time.sleep(0.1)
app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()