Raspberry Pi email notifier LED using Python

This needs a rewrite…

https://learn.pimoroni.com/tutorial/sandyj/getting-started-with-unicorn-phat

curl https://get.pimoroni.com/unicornhat  | bash

Python script to light all LEDs with random colors:

import time
import random
import unicornhat as uh

uh.set_layout(uh.PHAT)
uh.brightness(0.7)

for x in range(8):
[tab]y in range(4):
[tab][tab]a = random.randint(0, 255)
[tab][tab]b = random.randint(0, 255)
[tab][tab]c = random.randint(0, 255)
[tab][tab]uh.set_pixel(x, y, a, b, c)
uh.show()
time.sleep(5)

Put that into a python script; call it pilights.py or something. Then run sudo python pilights.py (it has to be run with sudo)

Set up the python library:

sudo pip install –upgrade google-api-python-client

Then go here: https://developers.google.com/gmail/api/quickstart/python

To set up API access to Google/Gmail, go here and create a project. Create the OAuth client ID, download the json file, rename it to client_secret.json, and upload it to the Pi (using scp)

Create the quickstart script using the Google quickstart link above. Run the script as quickstart.py –noauth_local_webserver and copy/paste the link into the browser on your PC. Choose the Google account, then copy/paste the verification code back into the SSH session.

Then make a copy of the quickstart script and delete everything after the main function. Replace it with this:

def checkUnread():

[tab]credentials = get_credentials()
[tab]http = credentials.authorize(httplib2.Http())
[tab]service = discovery.build(‘gmail’, ‘v1′, http=http)

[tab]results = service.users().labels().get(userId=’me’, id=”INBOX”).execute()
[tab]unread = results.get(‘threadsUnread’)
[tab]return unread

 

if __name__ == ‘__main__’:
[tab]checkUnread()

Call it emailunread.py or similar.

Copy the lights script above to lights_unread.py. Add import emailunread to the top and declare a variable like z = emailunread.checkUnread()

Then do whatever you want with z to control the lights, e.g. replace the first for loop line with for x in range(z)

For a full list of API functions, https://developers.google.com/resources/api-libraries/documentation/gmail/v1/python/latest/gmail_v1.users.html

You may have to rename the credentials (.json file) and move it to /root/.credentials/ because running the script requires root privileges (to turn on the LEDs).

Advertisements

Comments are closed.

Blog at WordPress.com.

Up ↑

%d bloggers like this: