Advertisement
Guest User

Untitled

a guest
Jan 10th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. from imapclient import IMAPClient
  4. import time
  5.  
  6. import RPi.GPIO as GPIO
  7.  
  8. DEBUG = True
  9.  
  10. HOSTNAME = 'imap.gmail.com'
  11. USERNAME = 'example@gmail.com' # use your full gmail address
  12. PASSWORD = 'password'
  13. MAILBOX = 'Inbox'
  14.  
  15. NEWMAIL_OFFSET = 0 # my unread messages never goes to zero, yours might
  16. MAIL_CHECK_FREQ = 30 # check mail every 60 seconds
  17.  
  18. GPIO.setwarnings(False)
  19. GPIO.setmode(GPIO.BCM)
  20. GREEN_LED = 4
  21. RED_LED = 17
  22. GPIO.setup(GREEN_LED, GPIO.OUT)
  23. GPIO.setup(RED_LED, GPIO.OUT)
  24.  
  25. def loop():
  26. server = IMAPClient(HOSTNAME, use_uid=True, ssl=True)
  27. server.login(USERNAME, PASSWORD)
  28.  
  29. if DEBUG:
  30. print('Logging in as ' + USERNAME)
  31. select_info = server.select_folder(MAILBOX)
  32. print('%d messages in INBOX' % select_info['EXISTS'])
  33.  
  34. folder_status = server.folder_status(MAILBOX, 'UNSEEN')
  35. newmails = int(folder_status['UNSEEN'])
  36.  
  37. if DEBUG:
  38. print "You have", newmails, "new emails!"
  39.  
  40. if newmails > NEWMAIL_OFFSET:
  41. GPIO.output(GREEN_LED, True)
  42. GPIO.output(RED_LED, False)
  43. else:
  44. GPIO.output(GREEN_LED, False)
  45. GPIO.output(RED_LED, True)
  46.  
  47. time.sleep(MAIL_CHECK_FREQ)
  48.  
  49. if __name__ == '__main__':
  50. try:
  51. print 'Press Ctrl-C to quit.'
  52. while True:
  53. loop()
  54. finally:
  55. GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement