-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathteste_sensor_camera.py
More file actions
executable file
·65 lines (58 loc) · 1.85 KB
/
teste_sensor_camera.py
File metadata and controls
executable file
·65 lines (58 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import RPi.GPIO as GPIO
from datetime import datetime
import time
from picamera import PiCamera
import datetime
import json
from email.mime.multipart import MIMEMultipart
from email.mime.image import MIMEImage
from email.mime.text import MIMEText
import smtplib
msg = MIMEMultipart()
password = 'pihomedsoft'
msg['From'] = 'pihomedigital@gmail.com'
msg['To'] = 'pedroluiz51@gmail.com'
#msg['To'] = 'rafael.libertini@gmail.com'
msg['Subject'] = 'Intruso Detectado'
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(13, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(17, GPIO.OUT)
camera = PiCamera()
with open("historico.json","r") as arquivo:
leitura = arquivo.read()
if len(leitura) == 0:
historico = {}
else:
historico = json.loads(leitura)
while True:
if GPIO.input(17) == GPIO.LOW:
i=GPIO.input(13)
if i == GPIO.LOW:
print("Sem intrusos")
time.sleep(0.5)
elif i == GPIO.HIGH:
data = datetime.datetime.now().strftime("%y-%m-%d-%H-%M-%S")
geral_separado = data.split("-")
dia = "{}/{}".format(geral_separado[2],geral_separado[1])
data_str = str(data)
data_jpg = data_str + ".jpg"
if dia not in historico.keys():
historico[dia] = []
historico[dia].append(data_jpg)
with open("historico.json","w") as arquivo:
arquivo.write(json.dumps(historico))
camera.start_preview()
time.sleep(2)
camera.capture("/home/pi/projetofinal/static/intrusos/{}.jpg".format(data))
print("Intruso detectado.Foto tirada {}".format(data))
camera.stop_preview()
foto = open("/home/pi/projetofinal/static/intrusos/{}.jpg".format(data) , 'rb')
#msg.attach(MIMEText(''))
msg.attach(MIMEImage(foto.read()))
server = smtplib.SMTP('smtp.gmail.com: 587')
server.starttls()
server.login(msg['From'], password)
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
time.sleep(0.5)