Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/pi python
- # coding: utf8
- from imapclient import IMAPClient, SEEN
- SEEN_FLAG = 'SEEN'
- UNSEEN_FLAG = 'UNSEEN'
- class GmailWrapper:
- def __init__(self, host, userName, password):
- # force the user to pass along username and password to log in as
- self.host = host
- self.userName = userName
- self.password = password
- self.login()
- def login(self):
- print('Logging in as ' + self.userName)
- server = IMAPClient(self.host, use_uid=True, ssl=True)
- server.login(self.userName, self.password)
- self.server = server
- # The IMAPClient search returns a list of Id's that match the given criteria.
- # An Id in this case identifies a specific email
- def getIdsBySubject(self, subject, unreadOnly=True, folder='INBOX'):
- # search within the specified folder, e.g. Inbox
- self.setFolder(folder)
- # build the search criteria (e.g. unread emails with the given subject)
- self.searchCriteria = [UNSEEN_FLAG, 'SUBJECT', subject]
- if(unreadOnly == False):
- # force the search to include "read" emails too
- self.searchCriteria.append(SEEN_FLAG)
- # conduct the search and return the resulting Ids
- return self.server.search(self.searchCriteria)
- def markAsRead(self, mailIds, folder='INBOX'):
- self.setFolder(folder)
- self.server.set_flags(mailIds, [SEEN])
- def setFolder(self, folder):
- self.server.select_folder(folder)
- HOSTNAME = 'imap.gmail.com'
- USERNAME = 'user@gmail.com'
- PASSWORD = 'password'
- def feedByGmail():
- gmailWrapper = GmailWrapper(HOSTNAME, USERNAME, PASSWORD)
- ids = gmailWrapper.getIdsBySubject('feed cats')
- if(len(ids) > 0):
- try:
- feed()
- gmailWrapper.markAsRead(ids)
- except:
- print("Failed to feed cats, they're starvingggg")
- import RPi.GPIO as GPIO
- import time
- def feed():
- # let the library know where we've connected our servo to the Pi
- GPIO.setmode(GPIO.BCM)
- GPIO.setup(18, GPIO.OUT)
- try:
- servo = GPIO.PWM(18, 50)
- servo.start(12.5)
- # spin left, right, then left again rather than in a continuous circle
- # to prevent the food from jamming the servo
- for index in range(0, 3):
- dutyCycle = 2.5 if (index % 2 == 0) else 12.5
- servo.ChangeDutyCycle(dutyCycle)
- time.sleep(0.8)
- finally:
- # always cleanup after ourselves
- servo.stop()
- GPIO.cleanup()
- if __name__ == '__main__':
- # kick off the feeding process (move the servo)
- # we now use our new feedByGmail method to handle the feeding
- feedByGmail()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement