Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from flask import Flask, render_template,request,redirect,url_for #importing flask for webpage
- from flask_mail import Mail,Message #importing flask for email
- import os #importing os library
- import glob #glob library
- import time #import time
- import RPi.GPIO as GPIO #GPIO pins
- app=Flask(__name__) #declaring app
- app.config.update( #configuring app for email
- DEBUG=True,
- #EMAIL SETTINGS
- MAIL_SERVER='smtp.gmail.com',
- MAIL_PORT=465,
- MAIL_USE_SSL=True,
- MAIL_USERNAME = 'raspguzi@gmail.com',
- MAIL_PASSWORD = 'Callofduty6'
- )
- mail = Mail(app) #declaring mail variable
- GPIO.setwarnings(False) #ignores unnecessary errors
- GPIO.setmode(GPIO.BCM) #physical pin numbering
- sensor=20 #pin 20
- GPIO.setup(sensor,GPIO.IN) #reads input from PIR motion sensor
- try:
- def read_motion():
- while True:
- i=GPIO.input(20)
- if i==False: #When output from motion sensor is LOW
- return("No intruders") #display "no intruders"
- else: #when OUTPUT FROM MOTION SENSOR IS HIGH
- return("intruder detected") #displays "inruders"
- def read_temp_raw(): #defining the temperrature sensor
- base_dir = '/sys/bus/w1/devices/'
- device_folder = glob.glob(base_dir + '28*')[0]
- device_file = device_folder + '/w1_slave'
- f = open(device_file, 'r')
- lines = f.readlines()
- f.close()
- return lines
- def read_temp(): #reading the temperature
- lines = read_temp_raw()
- while lines[0].strip()[-3:] != 'YES':
- time.sleep(0.2)
- lines = read_temp_raw()
- equals_pos = lines[1].find('t=')
- if equals_pos != -1:
- temp_string = lines[1][equals_pos+2:]
- temp_c = int((float(temp_string) / 1000.0))
- return temp_c
- @app.route("/",methods=['GET','POST']) #1st page, admin panel
- def login(): #function for entry
- error="" #defining error
- try:
- if request.method=="POST": #if loaded
- username=request.form['username'] # user type username
- password=request.form['password'] # user type password
- if(username=="admin")and(password == "serverSecurity"): #if entries match
- return redirect(url_for('motionTemp')) #redirects to next page for readings
- else:
- error="Username and/or Password is invalid" # else error message appears
- return render_template('Login.html',error=error) #links error to html
- except Exception as ex:
- print(ex)
- @app.route("/server") #2nd page, control panel
- def motionTemp():
- os.system('modprobe w1-gpio') #sensor settings
- os.system('modprobe w1-therm') #sensor settings
- read_motion() #calling motion function to use
- read_temp() #calling temperature function to use
- templateData={
- 'motion':read_motion(), #links motion reading w/ html
- 'tempc':read_temp() #links temperature reading w/ html
- }
- return render_template('index.html',**templateData) #returns to index.html
- @app.route("/server/sendMail") #3rd page sends email
- def email():
- msg = Message("Sensor Readings", #subject
- sender="raspguzi@gmail.com", #email from
- recipients=["jasonsch2000@gmail.com"]) #email to
- msg.body = "Dear System Administrator,/nThe Motion and Temperature sensor readings are respectively as follows: "+read_motion()+read_motion() #email sent
- mail.send(msg) #sends email
- return 'Mail Sent!' #confirmation
- if __name__ == "__main__":
- app.run(debug=True,host="0.0.0.0") #hosts website
- except Exception as ex:
- print("error occurred",ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement