-
Notifications
You must be signed in to change notification settings - Fork 0
/
SensorDataMail.py
53 lines (41 loc) · 1.44 KB
/
SensorDataMail.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
#Abu Taj
#Alert MailSender.
import RPi.GPIO as GPIO
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
mail=MIMEMultipart()
ser=smtplib.SMTP('smtp.gmail.com',587) #SMTP Server and Port Number
ser.starttls() #Server Starting
ser.login('[email protected]','senderpassword') #Authentication Point
GPIO.setmode(GPIO.BCM) #Numbering the GPIO pin
TRIG = 23
ECHO = 24
pulse_start=0
pulse_end=0
GPIO.setup(TRIG,GPIO.OUT) #GPIO Mapping
GPIO.setup(ECHO,GPIO.IN)
while True:
GPIO.output(TRIG, False)
time.sleep(0.1)
GPIO.output(TRIG, True)
time.sleep(0.00001)
GPIO.output(TRIG, False)
while GPIO.input(ECHO)==0:
pulse_start = time.time()
while GPIO.input(ECHO)==1:
pulse_end = time.time()
pulse_duration = pulse_end - pulse_start
distance = pulse_duration * 17150
distance = round(distance)
if distance < 50 : #Only Sends mail when sensor reads Less than 50 cm
mail['From']='Home Automation Systems'
mail['To']='[email protected]'
mail['Subject']='Sensor Data - Alert'
body='Sensor Read the Critical Data Distance of '+str(distance)+' cm'
mail.attach(MIMEText(body,'plain'))
msg=mail.as_string()
ser.sendmail('[email protected]','[email protected]',msg)
print('Mail Sent')
GPIO.cleanup()