Advertisement
Guest User

Cat Feeder

a guest
May 13th, 2018
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. #!/usr/bin/pi python
  2. # coding: utf8
  3. from imapclient import IMAPClient, SEEN
  4.  
  5. SEEN_FLAG = 'SEEN'
  6. UNSEEN_FLAG = 'UNSEEN'
  7.  
  8. class GmailWrapper:
  9. def __init__(self, host, userName, password):
  10. # force the user to pass along username and password to log in as
  11. self.host = host
  12. self.userName = userName
  13. self.password = password
  14. self.login()
  15.  
  16. def login(self):
  17. print('Logging in as ' + self.userName)
  18. server = IMAPClient(self.host, use_uid=True, ssl=True)
  19. server.login(self.userName, self.password)
  20. self.server = server
  21.  
  22. # The IMAPClient search returns a list of Id's that match the given criteria.
  23. # An Id in this case identifies a specific email
  24. def getIdsBySubject(self, subject, unreadOnly=True, folder='INBOX'):
  25. # search within the specified folder, e.g. Inbox
  26. self.setFolder(folder)
  27.  
  28. # build the search criteria (e.g. unread emails with the given subject)
  29. self.searchCriteria = [UNSEEN_FLAG, 'SUBJECT', subject]
  30.  
  31. if(unreadOnly == False):
  32. # force the search to include "read" emails too
  33. self.searchCriteria.append(SEEN_FLAG)
  34.  
  35. # conduct the search and return the resulting Ids
  36. return self.server.search(self.searchCriteria)
  37.  
  38. def markAsRead(self, mailIds, folder='INBOX'):
  39. self.setFolder(folder)
  40. self.server.set_flags(mailIds, [SEEN])
  41.  
  42. def setFolder(self, folder):
  43. self.server.select_folder(folder)
  44.  
  45. HOSTNAME = 'imap.gmail.com'
  46. USERNAME = 'user@gmail.com'
  47. PASSWORD = 'password'
  48.  
  49. def feedByGmail():
  50. gmailWrapper = GmailWrapper(HOSTNAME, USERNAME, PASSWORD)
  51.     ids = gmailWrapper.getIdsBySubject('feed cats')
  52.     if(len(ids) > 0):
  53.         try:
  54.             feed()
  55.             gmailWrapper.markAsRead(ids)
  56.         except:
  57.             print("Failed to feed cats, they're starvingggg")
  58.  
  59. import RPi.GPIO as GPIO
  60. import time
  61. def feed():
  62.     # let the library know where we've connected our servo to the Pi
  63.     GPIO.setmode(GPIO.BCM)
  64.     GPIO.setup(18, GPIO.OUT)
  65.  
  66.     try:
  67.         servo = GPIO.PWM(18, 50)
  68.         servo.start(12.5)
  69.  
  70.         # spin left, right, then left again rather than in a continuous circle
  71.         # to prevent the food from jamming the servo
  72.         for index in range(0, 3):
  73.             dutyCycle = 2.5 if (index % 2 == 0) else 12.5
  74.             servo.ChangeDutyCycle(dutyCycle)
  75.             time.sleep(0.8)
  76.     finally:
  77.         # always cleanup after ourselves
  78.         servo.stop()
  79.         GPIO.cleanup()
  80.  
  81. if __name__ == '__main__':
  82.     # kick off the feeding process (move the servo)
  83.     # we now use our new feedByGmail method to handle the feeding
  84.     feedByGmail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement