-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMDv2.py
109 lines (87 loc) · 3.54 KB
/
MDv2.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
#!/usr/bin/python
import RPi.GPIO as GPIO
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import picamera
import time
import urllib.request
from datetime import datetime
#GPIO Configuration
GPIO.setmode(GPIO.BCM)
#GPIO Pins in use
Ch_MSensor =6
Ch_Buzzer = 12
Ch_Light = 16
camera = picamera.PiCamera()
#Global Variables
email_user = '[email protected]'
msg_body = 'Hi There, sending email from Python'
filename = 'example.jpg' #'c:\Coding\Send eMail\IMG_1673.JPG'
subject = 'Generated by RPi Camera'
current_state = 0
sleep_time= 2
GPIO.setup (Ch_MSensor,GPIO.IN)
GPIO.setup (Ch_Buzzer,GPIO.OUT)
GPIO.setup (Ch_Light, GPIO.OUT)
def Send_eMail(email_filename, email_to ,email_from, email_subject):
msg=MIMEMultipart()
msg['To']=",".join(email_to)
msg['From'] =email_from
msg['Subject'] = email_subject
attachment = open(email_filename, 'rb')
part = MIMEBase ('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename ="+filename)
msg.attach(part)
msg.attach(MIMEText(msg_body,'plain'))
text=msg.as_string()
server = smtplib.SMTP('smtp.live.com',587)
server.starttls()
server.login(email_from, "1million!")
server.sendmail(email_from, email_to,text)
server.quit()
def AlarmLight (OnOff): #Generic code to be used to activate anything
print ("Light State", OnOff)
GPIO.output(Ch_Light,OnOff)
return
def Buzzer(buzzer_OnOff): #Generic code to be used to activate anything
print ("Buzzer State", buzzer_OnOff)
GPIO.output(Ch_Buzzer,buzzer_OnOff)
return
def connected(host='https://www.google.com'): # checks for a valid internet connection
internetUp = urllib.request.urlopen(host).getcode()
if internetUp == 200: #print ("Should be return code 200",internetUp)
return True
else:
return False
def photo():
#camera.vflip = True
camera.capture('example.jpg')
# MAIN BODY of the program
if __name__ == '__main__':
try:
while True:
print ("Current State", current_state) #print ("GPIO pin %s is %s" % (P_Sensor, current_state))
if current_state ==1:
AlarmLight(False)
Buzzer(True)
photo()
time.sleep(4) #Allows time for the sensor to reset
if connected(): # Checks there is an Internet connection
Send_eMail(filename, email_rec,email_user,subject) # thhen sends an email alert, then turns on
else:
print ("No Internet Connection - data written to log") # or writes the event to the log file (pending)
else:
Buzzer(False)
AlarmLight(True)
time.sleep(0.1)
current_state = GPIO.input(Ch_MSensor)
except KeyboardInterrupt:
pass
finally:
GPIO.cleanup()