How to Flash an LED on Your Raspberry Pi When You Get New Email

Full article

This week we’ll create an email notification system using the Raspberry Pi. The idea is to check for new email, and flash an LED when we get one.

Connecting to Gmail

The circuit will be extremely straight-forward, so let’s focus on the more difficult part first – connecting to an email service.

We need to create a secure connection to our email provider, so we can find out when new mail arrives. Do a quick search, and you’ll likely find scripts like this one where you just connect with your username, password and a few other pieces of info depending on who the provider is. But what you can do will be extremely limited, and the code will be fragile.

Find the Official API

The preferred option is to check whether your email provider has already provided an “official” way to connect to their system and retrieve data from it. These are commonly referred to as APIs, or application programming interfaces.

When a company provides an API, that means they’ve put real time and effort into exposing certain areas of their system to you (although you’ll still have to do some work of your own, as we’ll see), and you can access those systems in relative confidence that how you’re connecting won’t just change or break.

(Note: At the beginning of the week, I thought I’d implement all three of those, to demonstrate how each works. But understanding an API enough to properly implement it takes time, and I just didn’t have enough of it. Gmail is the only service I use, so that’s the one I focused on.)

Authenticating

With Gmail, the API was my only option. Every time I tried a Python script I found, I’d get the following error:

The problem is that I have 2-Step Verification enabled, and the script can’t get past that. That’s okay… 2FA is a good thing, and we don’t want to disable that. There’s another, more secure and stable, way to access a Gmail account.

Google provides an API for connecting to most of their systems (including Gmail), along with tutorials to implement it in multiple languages (including Python). We can use the API to access messages or pretty much any other aspect of our email account.

This process involves entering a few details on their side, and then they assign you some special numbers (a “client id” and a “client secret”). You download those numbers in a special file called “client_secret.json” and include it with your script, which in turn helps prove that the script is authorized to access your account.

Follow their Python Quickstart. Note the other languages on the left too, if you’d rather try one of those. After you authenticate, their sample script prints a list of your email tags:

Getting the Unread Mail Count

Once authenticated, their API gives you access to all kinds of useful info about your account. We’ll just modify the sample script they provided.

Most of their script is just about authenticating to Gmail, so don’t touch any of that. The following two lines got the list of labels above, and those are the ones we’ll change.

But what do we change them to? To find out which API calls we need to make, we’ll delve into the APIs Explorer for the Gmail API. It’s a nice tool, where you can browse through all the available API calls, and even try them out, all from within your browser.

The one we need is gmail.users.messages.list, so we can get a list of messages (and then count them). But we’re not interested in *all *email, just a subset. How do we do that?

Check out that screen capture above again. There’s a hidden label called “UNREAD” that’ll work nicely. The API allows you to place filters (such as userId='me') inside the call to list(), and one of them is a query string (denoted as q=... below).

By using is:inbox + ``is:unread we can get emails with both of those labels. The json response we get back includes two keys – one is a list of messages, while the other is the number of messages. We can use the latter to get our message count.

Here’s the main portion of the script. I split most of the Gmail stuff into a separate file when I intended to implement several different email providers. I ended up not having enough time, but it’s still nice to have the code somewhat organized. The code that does the actual authorization to Gmail is in a third file, which you can find along with the entire project on Github.

Designing the Circuit

Once we’ve got a script that’ll connect to an email account, retrieve the data we’re interested in, and turn a GPIO pin on or off based on what we find, the next step is to create the circuit.

It’s a fairly simple one, limited to just an LED and a resistor, connecting board pin 35 to ground. I’ve also added a button (and second resistor) to the circuit, connected to board pin 12, that allows us to immediately check for new email without waiting for the interval. That way, we can change CHECK_INTERVAL to some less-frequent number like 60 (a minute), but then press the button if we don’t feel like waiting. That’s what the add_event_detect line is for in the above code.

Here are some photos of the actual circuit.

Questions? Comments? Hit me up in the comments section below, or on Twitter.

More Reading

Some extra reading if you’re interested. Just some stuff I came across while doing research this week.

Affiliate Links

I occasionally include affiliate links for products I own and have found to be of good quality, or after checking reviews and feeling confident they are good products.
These links don't increase your cost at all, but using them helps pay for this blog and the time I put into it. Thanks!

Check out what I'm up to

About Me

I write about whatever happens to be on my mind when I sit down. I'm a father of 5, developer by profession, and aspiring woodworker.
I enjoy the Pi and other programming projects, as long as there's a purpose. (read more...)

Support What I Do

It takes time to write a decent tutorial and answer questions from readers (but I enjoy it!),
so if you found something helpful today and want to say "thanks", here's how. 😁