Last active
September 10, 2023 07:27
-
-
Save bwiggs/9919a86145a5c3ae9146 to your computer and use it in GitHub Desktop.
For use on a raspberry pi. - Checks a gmail account for any unread messages. If there's a message then it turns on a red LED, if there are no emails then it shows a green led.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from imapclient import IMAPClient | |
import time | |
import RPi.GPIO as GPIO | |
DEBUG = True | |
HOSTNAME = 'imap.gmail.com' | |
USERNAME = '[email protected]' # use your full gmail address | |
PASSWORD = 'password' | |
MAILBOX = 'Inbox' | |
NEWMAIL_OFFSET = 0 # my unread messages never goes to zero, yours might | |
MAIL_CHECK_FREQ = 30 # check mail every 60 seconds | |
GPIO.setwarnings(False) | |
GPIO.setmode(GPIO.BCM) | |
GREEN_LED = 4 | |
RED_LED = 17 | |
GPIO.setup(GREEN_LED, GPIO.OUT) | |
GPIO.setup(RED_LED, GPIO.OUT) | |
def loop(): | |
server = IMAPClient(HOSTNAME, use_uid=True, ssl=True) | |
server.login(USERNAME, PASSWORD) | |
if DEBUG: | |
print('Logging in as ' + USERNAME) | |
select_info = server.select_folder(MAILBOX) | |
print('%d messages in INBOX' % select_info['EXISTS']) | |
folder_status = server.folder_status(MAILBOX, 'UNSEEN') | |
newmails = int(folder_status['UNSEEN']) | |
if DEBUG: | |
print "You have", newmails, "new emails!" | |
if newmails > NEWMAIL_OFFSET: | |
GPIO.output(GREEN_LED, True) | |
GPIO.output(RED_LED, False) | |
else: | |
GPIO.output(GREEN_LED, False) | |
GPIO.output(RED_LED, True) | |
time.sleep(MAIL_CHECK_FREQ) | |
if __name__ == '__main__': | |
try: | |
print 'Press Ctrl-C to quit.' | |
while True: | |
loop() | |
finally: | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment