Advertisement
Guest User

Untitled

a guest
May 15th, 2017
633
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.13 KB | None | 0 0
  1. from flask import Flask, render_template,request,redirect,url_for #importing flask for webpage
  2. from flask_mail import Mail,Message #importing flask for email
  3. import os #importing os library
  4. import glob #glob library
  5. import time #import time
  6. import RPi.GPIO as GPIO #GPIO pins
  7.  
  8. app=Flask(__name__) #declaring app
  9.  
  10. app.config.update( #configuring app for email
  11. DEBUG=True,
  12. #EMAIL SETTINGS
  13. MAIL_SERVER='smtp.gmail.com',
  14. MAIL_PORT=465,
  15. MAIL_USE_SSL=True,
  16. MAIL_USERNAME = 'raspguzi@gmail.com',
  17. MAIL_PASSWORD = 'Callofduty6'
  18. )
  19. mail = Mail(app) #declaring mail variable
  20.  
  21.  
  22.  
  23. GPIO.setwarnings(False) #ignores unnecessary errors
  24. GPIO.setmode(GPIO.BCM) #physical pin numbering
  25. sensor=20 #pin 20
  26. GPIO.setup(sensor,GPIO.IN) #reads input from PIR motion sensor
  27.  
  28.  
  29. try:
  30. def read_motion():
  31. while True:
  32. i=GPIO.input(20)
  33. if i==False: #When output from motion sensor is LOW
  34. return("No intruders") #display "no intruders"
  35.  
  36.  
  37. else: #when OUTPUT FROM MOTION SENSOR IS HIGH
  38. return("intruder detected") #displays "inruders"
  39.  
  40.  
  41. def read_temp_raw(): #defining the temperrature sensor
  42.  
  43. base_dir = '/sys/bus/w1/devices/'
  44. device_folder = glob.glob(base_dir + '28*')[0]
  45. device_file = device_folder + '/w1_slave'
  46. f = open(device_file, 'r')
  47. lines = f.readlines()
  48. f.close()
  49. return lines
  50.  
  51. def read_temp(): #reading the temperature
  52. lines = read_temp_raw()
  53. while lines[0].strip()[-3:] != 'YES':
  54. time.sleep(0.2)
  55. lines = read_temp_raw()
  56. equals_pos = lines[1].find('t=')
  57. if equals_pos != -1:
  58. temp_string = lines[1][equals_pos+2:]
  59. temp_c = int((float(temp_string) / 1000.0))
  60. return temp_c
  61.  
  62.  
  63. @app.route("/",methods=['GET','POST']) #1st page, admin panel
  64. def login(): #function for entry
  65. error="" #defining error
  66. try:
  67. if request.method=="POST": #if loaded
  68. username=request.form['username'] # user type username
  69. password=request.form['password'] # user type password
  70.  
  71.  
  72.  
  73. if(username=="admin")and(password == "serverSecurity"): #if entries match
  74. return redirect(url_for('motionTemp')) #redirects to next page for readings
  75. else:
  76. error="Username and/or Password is invalid" # else error message appears
  77. return render_template('Login.html',error=error) #links error to html
  78.  
  79. except Exception as ex:
  80. print(ex)
  81.  
  82.  
  83.  
  84. @app.route("/server") #2nd page, control panel
  85. def motionTemp():
  86. os.system('modprobe w1-gpio') #sensor settings
  87. os.system('modprobe w1-therm') #sensor settings
  88. read_motion() #calling motion function to use
  89. read_temp() #calling temperature function to use
  90.  
  91. templateData={
  92. 'motion':read_motion(), #links motion reading w/ html
  93. 'tempc':read_temp() #links temperature reading w/ html
  94. }
  95.  
  96. return render_template('index.html',**templateData) #returns to index.html
  97.  
  98.  
  99. @app.route("/server/sendMail") #3rd page sends email
  100. def email():
  101.  
  102. msg = Message("Sensor Readings", #subject
  103. sender="raspguzi@gmail.com", #email from
  104. recipients=["jasonsch2000@gmail.com"]) #email to
  105. msg.body = "Dear System Administrator,/nThe Motion and Temperature sensor readings are respectively as follows: "+read_motion()+read_motion() #email sent
  106. mail.send(msg) #sends email
  107. return 'Mail Sent!' #confirmation
  108.  
  109. if __name__ == "__main__":
  110. app.run(debug=True,host="0.0.0.0") #hosts website
  111.  
  112.  
  113. except Exception as ex:
  114. print("error occurred",ex)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement