-
Notifications
You must be signed in to change notification settings - Fork 0
/
sound_shot_cayenne.py
123 lines (96 loc) · 3.74 KB
/
sound_shot_cayenne.py
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import RPi.GPIO as GPIO
import time
import wiringpi2
import time
import os
import smtplib
import imghdr
from email.message import EmailMessage
# https://mydevices.com/cayenne/docs_stage/cayenne-mqtt-api/#cayenne-mqtt-api-manually-publishing-subscribing
# Manually Publishing / Subscribing
# Biblioteca para o protocolo MQTT
import paho.mqtt.client as mqtt
# Variáveis para acesso ao servidor
user = 'd66e1d40-8ebb-11e9-be3b-372b0d2759ae'
password = '3e62d1f65b466aefb93b74a6cbc25568562c72a9'
client_id = 'f89c75f0-8ebc-11e9-beb3-736c9e4bf7d0'
server = 'mqtt.mydevices.com'
port = 1883
# Cria o objeto para acessar o servidor (Client ID)
client = mqtt.Client(client_id)
# Informa ao objeto o usuário e senha para acesso ao servidor (MQTT username, MQTT password)
client.username_pw_set(user, password)
# Estabelece a conexão com o servidor (MQTT Server, MQTT Port)
client.connect(server, port)
channel_3 = 12
def send_email():
msg = EmailMessage()
msg['Subject'] = 'Camera capture'
msg['From'] = "[email protected]"
msg['To'] = "[email protected]"
msg.preamble = 'Foto capturada'
img_data = open("captura.jpg", 'rb').read()
msg.add_attachment(img_data, maintype='image', subtype=imghdr.what(None, img_data))
s=smtplib.SMTP("smtp.gmail.com", 587)
s.ehlo()
s.starttls()
s.login("[email protected]", "pucbes@19")
s.send_message(msg)
# MQTT Server, MQTT Port,
client.connect(server, port)
# Envia a informação 123.4 para o canal 0
client.publish('v1/'+user+'/things/'+client_id+'/data/0', 1)
client.publish('v1/'+user+'/things/'+client_id+'/data/0', 0)
def servo (angulo):
valor = 60 + (190*angulo)/180
wiringpi.pwmWrite(18, int(valor))
# Nome dos pinos no modo 'GPIO'
wiringpi.wiringPiSetupGpio()
# Configura pino18 como saida PWM
wiringpi.pinMode(channel_3, wiringpi.GPIO.PWM_OUTPUT)
# configura temporizacao PWM
wiringpi.pwmSetMode(wiringpi.GPIO.PWM_MODE_MS)
wiringpi.pwmSetClock(192)
wiringpi.pwmSetRange(2000)
def reset():
servo(90)
def virar_direita():
servo(45)
def virar_esquerda():
servo(135)
channel_1 = 17 # sensor 1
channel_2 = 22 # sensor 2
GPIO.setmode(GPIO.BCM)
GPIO.setup(channel_1, GPIO.IN)
GPIO.setup(channel_2, GPIO.IN)
tempos_1 = []
tempos_2 = []
def callback_1(channel_1):
tempos_1.append(int(round(time.time() * 1000000)))
print("Sensor 1: " + str(int(round(time.time() * 1000000))))
def callback_2(channel_2):
tempos_2.append(int(round(time.time() * 1000000)))
print("Sensor 2: " + str(int(round(time.time() * 1000000))))
GPIO.add_event_detect(channel_1, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(channel_1, callback_1)
GPIO.add_event_detect(channel_2, GPIO.BOTH, bouncetime=300)
GPIO.add_event_callback(channel_2, callback_2)
reset()
while True:
tempo_atual = int(round(time.time() * 1000000))
if tempos_1 and tempos_2: # Se ambos os sensores detectaram o som
if abs(tempos_1[-1] - tempos_2[-1]) < 100000: # Se a diferença entre os tempos dos sensores for menor que 10ms
if abs(tempos_1[-1] - tempo_atual) < 100000: # Se o som foi detectado no loop atual
if tempos_1[-1] - tempos_2[-1] < 0:
virar_direita()
time.sleep(0.1)
os.system("sudo fswebcam 640x480 captura.jpg")
send_email()
reset()
else:
virar_esquerda()
time.sleep(0.1)
os.system("sudo fswebcam 640x480 captura.jpg")
send_email()
reset()
time.sleep(0.1)