Advertisement
Guest User

Untitled

a guest
May 17th, 2017
524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1.  
  2. Save New Duplicate & Edit Just Text Twitter
  3. 1
  4. 2
  5. 3
  6. 4
  7. 5
  8. 6
  9. 7
  10. 8
  11. 9
  12. 10
  13. 11
  14. 12
  15. 13
  16. 14
  17. 15
  18. 16
  19. 17
  20. 18
  21. 19
  22. 20
  23. 21
  24. 22
  25. 23
  26. 24
  27. 25
  28. 26
  29. 27
  30. 28
  31. 29
  32. 30
  33. 31
  34. 32
  35. 33
  36. 34
  37. 35
  38. 36
  39. 37
  40. 38
  41. 39
  42. 40
  43. 41
  44. 42
  45. 43
  46. 44
  47. 45
  48. 46
  49. 47
  50. 48
  51. 49
  52. 50
  53. 51
  54. 52
  55. 53
  56. 54
  57. 55
  58. 56
  59. 57
  60. 58
  61. 59
  62. 60
  63. 61
  64. 62
  65. 63
  66. 64
  67. 65
  68. 66
  69. 67
  70. 68
  71. 69
  72. 70
  73. 71
  74. 72
  75. 73
  76. 74
  77. 75
  78. 76
  79. 77
  80. 78
  81. 79
  82. 80
  83. 81
  84. 82
  85. 83
  86. 84
  87. 85
  88. 86
  89. 87
  90. 88
  91. 89
  92. 90
  93. 91
  94. 92
  95. 93
  96. 94
  97. 95
  98. 96
  99. 97
  100. 98
  101. 99
  102. 100
  103. 101
  104. 102
  105. 103
  106. 104
  107. 105
  108. 106
  109. 107
  110. 108
  111. 109
  112. 110
  113. 111
  114. 112
  115. 113
  116. 114
  117. 115
  118. 116
  119. 117
  120. 118
  121. 119
  122. 120
  123. 121
  124. 122
  125. 123
  126. 124
  127. 125
  128. 126
  129. 127
  130. 128
  131. 129
  132. 130
  133. 131
  134. 132
  135. 133
  136. 134
  137. 135
  138. 136
  139. 137
  140. 138
  141. 139
  142. 140
  143. 141
  144. 142
  145. 143
  146. 144
  147. 145
  148. 146
  149. 147
  150. 148
  151. 149
  152. 150
  153. 151
  154. 152
  155. 153
  156. 154
  157. 155
  158. 156
  159. 157
  160. 158
  161. 159
  162. 160
  163. 161
  164. 162
  165. 163
  166. 164
  167. 165
  168. 166
  169. 167
  170. 168
  171. 169
  172. 170
  173. 171
  174. 172
  175. 173
  176. 174
  177. 175
  178. 176
  179. 177
  180. 178
  181. 179
  182. 180
  183. 181
  184. 182
  185. 183
  186. 184
  187. 185
  188. 186
  189. 187
  190. 188
  191. 189
  192. 190
  193. 191
  194. 192
  195. 193
  196. 194
  197. 195
  198. 196
  199. 197
  200. 198
  201. 199
  202. 200
  203. 201
  204. 202
  205. #!/usr/bin/python3.4
  206.  
  207. #Imports
  208. import RPi.GPIO as GPIO
  209. import time
  210. import sys
  211. import os
  212. import os.path
  213. import smtplib
  214. from email.mime.multipart import MIMEMultipart
  215. from email.mime.text import MIMEText
  216. from email.mime.base import MIMEBase
  217. from email import encoders
  218. from flask import Flask
  219. from flask import Flask,flash,redirect,render_template,request,session,abort
  220.  
  221. #Setup
  222. GPIO.setmode(GPIO.BCM)
  223. GPIO.setwarnings(False)
  224. #Red LED
  225. GPIO.setup(21,GPIO.OUT)
  226. #Green LED
  227. GPIO.setup(17,GPIO.OUT)
  228. #Button
  229. GPIO.setup(18,GPIO.IN)
  230. #PIR Sensor
  231. sensor = 27
  232. GPIO.setup(sensor, GPIO.IN)
  233. #temp drivers
  234. os.system('modprobe w1-gpio')
  235. os.system('modprobe w1-therm')
  236. temp_sensor = '/sys/bus/w1/devices/28-0000073b8bb3/w1_slave'
  237. #Variables
  238. current = 0
  239. previous = 0
  240. #flask
  241. #app = Flask(__name__)
  242.  
  243. #@app.route('/')
  244. #def home():
  245. # if not session.get('logged_in'):
  246. # return render_template('login.html')
  247. # else:
  248. # return "Welcome, you may start the program"
  249.  
  250. #@app.route('/login',methods =['POST'])
  251. #def do_admin_login():
  252. # if request.form['password'] == '123' and request.form['username'] == 'admin':
  253. # session['logged_in'] = True
  254. # else:
  255. # flash('wrong password')
  256. # return home()
  257. #if __name__ == "__main__":
  258. # app.secret_key = os.urandom(12)
  259. # app.run(debug=True,host ='0.0.0.0',port = 4000) #localhost:4000
  260.  
  261. #Start up function
  262. def startUp():
  263. #Printing to user for necessary information
  264. print("Welcome !")
  265. print("Please log in on: localhost:4000")
  266. print("Tempretaure in Celsius and Fahrenheit: ")
  267. #print(read_temp())
  268. print("Motion will be detected ... Press ctrl+c to exit")
  269. #LEDs turn on for a few seconds
  270. GPIO.output(21,GPIO.HIGH)
  271. GPIO.output(17,GPIO.HIGH)
  272. time.sleep(5)
  273. #LEDs turn off
  274. GPIO.output(21,GPIO.LOW)
  275. GPIO.output(17,GPIO.LOW)
  276.  
  277. #Function to send the email
  278. def sendEmail():
  279. #from/to/msg details
  280. fromaddr = "piprojectest@gmail.com"
  281. toaddr="piprojectest@gmail.com"
  282. msg = MIMEMultipart()
  283.  
  284. #assigning the previous details plus subject,body and attachment
  285. msg['From'] = fromaddr
  286. msg['To'] = toaddr
  287. msg['Subject'] = "Approval Needed"
  288. body = "Review the person detected. If approved press the button in 30seconds time"
  289. msg.attach(MIMEText(body, 'plain'))
  290. filename = "image.jpg"
  291. attachment = open('/home/pi/Assignment/image', "rb")
  292. part = MIMEBase('application', 'octet-stream')
  293. part.set_payload((attachment).read())
  294. encoders.encode_base64(part)
  295. #attaching file with its own details
  296. part.add_header('Content-Disposition',"attachment; filename = %s" % filename)
  297. msg.attach(part)
  298. #connecting to the server
  299. server = smtplib.SMTP('smtp.gmail.com',587)
  300. server.starttls()
  301. #logging into the email
  302. server.login("piprojectest@gmail.com","embedded2017&")
  303. text= msg.as_string()
  304. #sending and quit
  305. server.sendmail("piprojectest@gmail.com", "piprojectest@gmail.com",msg)
  306. server.quit()
  307.  
  308. #deciding if access was to be given
  309. def grantAccess():
  310. print("Press the button for access in less than 30 seconds")
  311. #setting the time
  312. timeout = time.time() + 60*0.5 #30s from now
  313. while time.time() < timeout:
  314. #if button is pressed
  315. if(GPIO.input(18) == False):
  316. print("Access Given")
  317. #green LED indicating access
  318. GPIO.output(17,GPIO.HIGH)
  319. time.sleep(3)
  320. GPIO.output(17,GPIO.LOW)
  321. break
  322. else:
  323. #if 30s passed the accessed is denied
  324. print("Access Denied")
  325. #red LED is lit
  326. GPIO.output(21,GPIO.HIGH)
  327. time.sleep(3)
  328. GPIO.output(21,GPIO.LOW)
  329.  
  330. #function for detecting motion
  331. def detection():
  332. global current
  333. global previous
  334.  
  335. #continous loop
  336. while True:
  337. try:
  338. time.sleep(0.1)
  339. #reading the sensor
  340. current = GPIO.input(sensor)
  341. #if the variable changed, motion was detected
  342. if current != previous:
  343. previous = current
  344. if(current == 1):
  345. print("Motion detected")
  346. #calling the camera function
  347. snap()
  348. #giving it time to save the image
  349. time.sleep(3)
  350. #function to send the mail
  351. sendEmail()
  352. #LED notification
  353. GPIO.output(17,GPIO.HIGH)
  354. #function for access
  355. grantAccess()
  356. GPIO.output(17,GPIO.LOW)
  357. else:
  358. #waiting for detection
  359. print("Waiting")
  360.  
  361. except KeyboardInterrupt:
  362. #to exit the program interrupting with keyboard
  363. print("Goodbye")
  364. GPIO.cleanup()
  365. sys.exit(0)
  366.  
  367. def snap():
  368. #command to take a photo and save it to the specific directory
  369. os.system('fswebcam -r 320x240 -S 3 --jpeg 50 --save /home/pi/Assignment/image .jpg')
  370.  
  371. #function to read the temp sensor
  372. def temp_raw():
  373. f=open(temp_sensor,'r')
  374. #reading the file lines
  375. lines = f.readlines()
  376. #closing file
  377. f.close()
  378. #returning the lines read
  379. return lines
  380.  
  381. def read_temp():
  382. #calling the fucking for the raw readings
  383. lines = temp_raw()
  384. #reading the sensor until theres a result
  385. while lines[0].strip()[-3:] != 'YES':
  386. time.sleep(0.2)
  387. lines=temp_raw()
  388. #result found and reading it
  389. temp_output =lines[1].find('t=')
  390. #if valid
  391. if temp_output != -1:
  392. #formatting the reading to celcius and farenheit
  393. temp_string = lines[1].strip()[temp_output+2:]
  394. temp_c = float(temp_string)/1000.0
  395. temp_f = temp_c *9.0/5.0 +32.0
  396. return temp_c,temp_f
  397.  
  398. #calling the functions in order from one function
  399.  
  400. def main():
  401. #startUp()
  402. detection()
  403.  
  404. #main function being called
  405. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement