Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Save New Duplicate & Edit Just Text Twitter
- 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
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
- 171
- 172
- 173
- 174
- 175
- 176
- 177
- 178
- 179
- 180
- 181
- 182
- 183
- 184
- 185
- 186
- 187
- 188
- 189
- 190
- 191
- 192
- 193
- 194
- 195
- 196
- 197
- 198
- 199
- 200
- 201
- 202
- #!/usr/bin/python3.4
- #Imports
- import RPi.GPIO as GPIO
- import time
- import sys
- import os
- import os.path
- import smtplib
- from email.mime.multipart import MIMEMultipart
- from email.mime.text import MIMEText
- from email.mime.base import MIMEBase
- from email import encoders
- from flask import Flask
- from flask import Flask,flash,redirect,render_template,request,session,abort
- #Setup
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- #Red LED
- GPIO.setup(21,GPIO.OUT)
- #Green LED
- GPIO.setup(17,GPIO.OUT)
- #Button
- GPIO.setup(18,GPIO.IN)
- #PIR Sensor
- sensor = 27
- GPIO.setup(sensor, GPIO.IN)
- #temp drivers
- os.system('modprobe w1-gpio')
- os.system('modprobe w1-therm')
- temp_sensor = '/sys/bus/w1/devices/28-0000073b8bb3/w1_slave'
- #Variables
- current = 0
- previous = 0
- #flask
- #app = Flask(__name__)
- #@app.route('/')
- #def home():
- # if not session.get('logged_in'):
- # return render_template('login.html')
- # else:
- # return "Welcome, you may start the program"
- #@app.route('/login',methods =['POST'])
- #def do_admin_login():
- # if request.form['password'] == '123' and request.form['username'] == 'admin':
- # session['logged_in'] = True
- # else:
- # flash('wrong password')
- # return home()
- #if __name__ == "__main__":
- # app.secret_key = os.urandom(12)
- # app.run(debug=True,host ='0.0.0.0',port = 4000) #localhost:4000
- #Start up function
- def startUp():
- #Printing to user for necessary information
- print("Welcome !")
- print("Please log in on: localhost:4000")
- print("Tempretaure in Celsius and Fahrenheit: ")
- #print(read_temp())
- print("Motion will be detected ... Press ctrl+c to exit")
- #LEDs turn on for a few seconds
- GPIO.output(21,GPIO.HIGH)
- GPIO.output(17,GPIO.HIGH)
- time.sleep(5)
- #LEDs turn off
- GPIO.output(21,GPIO.LOW)
- GPIO.output(17,GPIO.LOW)
- #Function to send the email
- def sendEmail():
- #from/to/msg details
- fromaddr = "piprojectest@gmail.com"
- toaddr="piprojectest@gmail.com"
- msg = MIMEMultipart()
- #assigning the previous details plus subject,body and attachment
- msg['From'] = fromaddr
- msg['To'] = toaddr
- msg['Subject'] = "Approval Needed"
- body = "Review the person detected. If approved press the button in 30seconds time"
- msg.attach(MIMEText(body, 'plain'))
- filename = "image.jpg"
- attachment = open('/home/pi/Assignment/image', "rb")
- part = MIMEBase('application', 'octet-stream')
- part.set_payload((attachment).read())
- encoders.encode_base64(part)
- #attaching file with its own details
- part.add_header('Content-Disposition',"attachment; filename = %s" % filename)
- msg.attach(part)
- #connecting to the server
- server = smtplib.SMTP('smtp.gmail.com',587)
- server.starttls()
- #logging into the email
- server.login("piprojectest@gmail.com","embedded2017&")
- text= msg.as_string()
- #sending and quit
- server.sendmail("piprojectest@gmail.com", "piprojectest@gmail.com",msg)
- server.quit()
- #deciding if access was to be given
- def grantAccess():
- print("Press the button for access in less than 30 seconds")
- #setting the time
- timeout = time.time() + 60*0.5 #30s from now
- while time.time() < timeout:
- #if button is pressed
- if(GPIO.input(18) == False):
- print("Access Given")
- #green LED indicating access
- GPIO.output(17,GPIO.HIGH)
- time.sleep(3)
- GPIO.output(17,GPIO.LOW)
- break
- else:
- #if 30s passed the accessed is denied
- print("Access Denied")
- #red LED is lit
- GPIO.output(21,GPIO.HIGH)
- time.sleep(3)
- GPIO.output(21,GPIO.LOW)
- #function for detecting motion
- def detection():
- global current
- global previous
- #continous loop
- while True:
- try:
- time.sleep(0.1)
- #reading the sensor
- current = GPIO.input(sensor)
- #if the variable changed, motion was detected
- if current != previous:
- previous = current
- if(current == 1):
- print("Motion detected")
- #calling the camera function
- snap()
- #giving it time to save the image
- time.sleep(3)
- #function to send the mail
- sendEmail()
- #LED notification
- GPIO.output(17,GPIO.HIGH)
- #function for access
- grantAccess()
- GPIO.output(17,GPIO.LOW)
- else:
- #waiting for detection
- print("Waiting")
- except KeyboardInterrupt:
- #to exit the program interrupting with keyboard
- print("Goodbye")
- GPIO.cleanup()
- sys.exit(0)
- def snap():
- #command to take a photo and save it to the specific directory
- os.system('fswebcam -r 320x240 -S 3 --jpeg 50 --save /home/pi/Assignment/image .jpg')
- #function to read the temp sensor
- def temp_raw():
- f=open(temp_sensor,'r')
- #reading the file lines
- lines = f.readlines()
- #closing file
- f.close()
- #returning the lines read
- return lines
- def read_temp():
- #calling the fucking for the raw readings
- lines = temp_raw()
- #reading the sensor until theres a result
- while lines[0].strip()[-3:] != 'YES':
- time.sleep(0.2)
- lines=temp_raw()
- #result found and reading it
- temp_output =lines[1].find('t=')
- #if valid
- if temp_output != -1:
- #formatting the reading to celcius and farenheit
- temp_string = lines[1].strip()[temp_output+2:]
- temp_c = float(temp_string)/1000.0
- temp_f = temp_c *9.0/5.0 +32.0
- return temp_c,temp_f
- #calling the functions in order from one function
- def main():
- #startUp()
- detection()
- #main function being called
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement