Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. import RPi.GPIO as GPIO
  2. import time
  3. import smtplib
  4. from email.MIMEMultipart import MIMEMultipart
  5. from email.MIMEText import MIMEText
  6. import datetime
  7.  
  8.  
  9.  
  10. body = "Email message"
  11. sender = "Room 202 Help"
  12. subject = "Room 202 Help Button"
  13. to = ""
  14. stmpAddress = "smtp.gmail.com"
  15. username = ""
  16. password = ""
  17. normalBody = "Help needed in room 202"
  18. importantBody = "Request Canceled Room 202"
  19. ledPin = 23
  20. buttonPin = 18
  21. importantPress = 2 #seconds
  22.  
  23.  
  24. GPIO.setmode(GPIO.BCM)
  25.  
  26. GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) #makes the pin stay high another words it is set to about 3 volts
  27. GPIO.setup(ledPin, GPIO.OUT) #makes the pin be an output
  28.  
  29. msg = MIMEMultipart()
  30. msg['From'] = sender
  31. msg['To'] = to
  32. msg['Subject'] = subject
  33. GPIO.output(ledPin,GPIO.LOW)
  34.  
  35. while True:
  36. buttonPress = GPIO.input(buttonPin)
  37. if buttonPress == False: #is the pin low?
  38. GPIO.output(ledPin,GPIO.HIGH)#turn on the led
  39. print('Button Pressed')#Yes it is low meaning it was shorted to ground
  40. buttonPressStart = datetime.datetime.now() #Time the button got pressed
  41. while GPIO.input(buttonPin) == False: # while it is still low
  42. time.sleep(0.1)#stay here till they let go of the button
  43. buttonPressEnd = datetime.datetime.now() # time the button was let go
  44.  
  45. diff = buttonPressEnd - buttonPressStart#the differnce of when the button got pressed and let go, duration on button press
  46.  
  47. if diff > datetime.timedelta(seconds = importantPress):
  48. msg.attach(MIMEText(importantBody, 'plain'))
  49. GPIO.output(ledPin,GPIO.LOW)#turn off the led if it's on
  50. print("Canceling")
  51. else:
  52. msg.attach(MIMEText(normalBody, 'plain'))
  53.  
  54. server = smtplib.SMTP(stmpAddress, 587)
  55. server.starttls()
  56. server.login(username, password)
  57. server.sendmail(username, to, msg.as_string())
  58. server.quit()#email sent
  59.  
  60.  
  61.  
  62. time.sleep(0.01)#sleep a short time so to not eat all resources
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement