# Raspberry Pi E-mail Notifier Using LEDs

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/000/719/medium800/raspberry_pi_IMG_46501.jpg?1396765310)

# Overview

Raspberry Pi’s popularity makes things so easy that it is almost scary. I set forth on a simple starter project of having the Raspberry Pi show me when new Gmail messages arrive.&nbsp;After some searching, it seems that lots of people are already talking about how to do this and there are some great examples. [Michael over at MitchTech](http://mitchtech.net/raspberry-pi-physical-gmail-notifier/ "Link: http://mitchtech.net/raspberry-pi-physical-gmail-notifier/")&nbsp;had the most ready to go code which I pilfered from.&nbsp;

This project uses two LEDs one green and one red. When a new mail message has arrived the green LED will illuminate. When there are no unread messages the red LED will illuminate.

# To Follow This Tutorial You Will Need

- [Pi T-Cobbler Plus,](https://www.adafruit.com/product/2028)&nbsp;[Pi Cobbler Plus for Model B+ / Pi 2](https://www.adafruit.com/products/2029)&nbsp;or the original&nbsp;[Pi Cobbler](https://www.adafruit.com/product/914)
- [(1) Full sized breadboard](https://www.adafruit.com/product/239)
- [Hook-up Wire](https://www.adafruit.com/index.php?main_page=adasearch&q=hook-up+wire+spool "Link: https://www.adafruit.com/index.php?main\_page=adasearch&q=hook-up+wire+spool")
- [A Raspberry Pi](https://www.adafruit.com/?q=raspberry%20pi)&nbsp;(compatible with all 26pin and 40pin Pi releases to date)

HINT: We suggest purchasing the [Raspberry Pi 3 Model B Starter Pack](https://www.adafruit.com/product/3334) along with a [Raspberry Pi](https://www.adafruit.com/product/3775) which includes all the necessary materials for this tutorial.

# Raspberry Pi E-mail Notifier Using LEDs

## Wire the Cobbler to the LEDs

Modern Raspberry Pi's use a 40-pin header. The first generation of Pi's used a 26-pin header. This tutorial works with all of these versions using the same code and GPIO pins. We have provided wiring examples of both types using the same GPIO pins (#18 and #23).

When connecting the GPIO cable, make sure that you note the red or white wire on the ribbon, that's pin #1 of the cable. That end goes at the side closest to the SD Card and is labeled&nbsp; **P1** on the Pi. The other side connects to the cobbler and can only be inserted one way due to the notch in the cable.  
  
Place the cobbler onto the bread board straddling the center line. Connect the&nbsp; **GND** pin (ground) to the blue power rail on the side of the breadboard. You'll need two resistors (any values&nbsp;from 330 ohm up to 1000 ohm are fine).

Connect the first resistor to the cobbler row marked&nbsp; **#18** , and the other end to a row that isn't used by the cobbler.

Connect the second resistor to the cobbler row marked # **23** and the other end to another empty row.

# Raspberry Pi Cobbler Plus - 40-pin for Pi v2, v3, Zero
![](https://cdn-learn.adafruit.com/assets/assets/000/024/094/medium800/raspberry_pi_email_blinkies_bb.png?1427489683)

# Raspberry Pi Cobber - 26-pin - for Pi v1 only
![](https://cdn-learn.adafruit.com/assets/assets/000/024/147/medium800/raspberry_pi_little_cobbler_bb.png?1427733019)

# Step-by-Step Hookup on a 26-pin Pi Cobbler
![](https://cdn-learn.adafruit.com/assets/assets/000/000/716/medium800/raspberry_pi_resistors.jpg?1396765277)

Now grab a red LED and a green LED. Look for the long pins on the LEDs; those are the positive (+) legs.

Connect the long (+) leg of the red LED to the resistor connected to&nbsp; **#23** &nbsp;(GPIO #23), and the long leg of the green LED to&nbsp;the resistor connected to&nbsp; **#18**.

The short legs plug into the blue striped rail on the side of the breadboard.

![](https://cdn-learn.adafruit.com/assets/assets/000/000/720/medium800/raspberry_pi_leds.jpg?1396765317)

The above images are for an original Pi Cobbler. For newer, 40-pin models like the A+/B+/Pi 2, you'll probably want to do something more like this, using wires to connect the resistors to the LEDs (click for a larger image):

That's it! You've just wired two LEDs with current-limiting resistors to the GPIO pins of the Pi.

# Raspberry Pi E-mail Notifier Using LEDs

## Necessary Packages

# Update Your Pi to the Latest Raspbian
Your Pi will need to be running the latest version of Raspbian. This tutorial was written using Raspbian Stretch (Nov. 2018). Checkout our guide for&nbsp;[Preparing an SD Card for your Raspberry Pi](https://learn.adafruit.com/adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi)&nbsp;if you have not done so already. After the installation is complete be sure and run the following commands to make sure your packages are up to date.&nbsp;

```auto
$ sudo apt-get install update -y
$ sudo apt-get install upgrade -y
```

# Install pip3
pip3 is already installed with a full Raspbian installation, but the Raspbian Lite does not include pip3 so it needs to be installed as shown below.

```auto
$ sudo apt-get install python3-pip

```

# Install adafruit-blinka
```auto
$ sudo pip3 install adafruit-blinka
```

# Install imapclient
```auto
sudo pip3 install imapclient
```

# Raspberry Pi E-mail Notifier Using LEDs

## Python Script

![](https://cdn-learn.adafruit.com/assets/assets/000/000/718/medium800/raspberry_pi_IMG_46501.jpg?1396765307)

Now you're ready to download some Python code and check your Gmail account. The following python script can be downloaded directly onto your raspberry pi, customized with your email settings and executed to illuminate the LEDs.

https://github.com/adafruit/Adafruit_Learning_System_Guides/blob/main/Raspberry_Pi_E-mail_Notifier_Using_LEDs/Raspberry_Pi_E-mail_Notifier_Using_LEDs.py

# Download the Code
Let's put this file right in your home directory for simplicity. The wget command makes this easy.

```auto
$ cd
$ wget https://raw.githubusercontent.com/adafruit/Adafruit_Learning_System_Guides/master/Raspberry_Pi_E-mail_Notifier_Using_LEDs/Raspberry_Pi_E-mail_Notifier_Using_LEDs.py
```

# Customize Mail Variables
Don't forget to set the `USERNAME` and `PASSWORD` to match your GMail account. (Remember, if you're using two-factor authentication under GMail, you'll need to generate an [application-specific password](https://support.google.com/mail/answer/1173270?hl=en) for this. If you're using a different e-mail provider, you may need to check their documentation for what `HOSTNAME` to use. It's usually something like `imap.youremailproviderhere.com`.)

Finally, my INBOX never goes to zero unread messages. We have a variable called `NEWMAIL_OFFSET` which you can customize to whatever your current unread message count is. When running this python script there will be a number output to the console showing the current number of unseen messages.

# Running the Code
```auto
$ python3 ./Raspberry_Pi_E-mail_Notifier_Using_LEDs.py
```

Send yourself some emails to see the green LED light up!

You can stop the script at any time by pressing **Ctrl-C**.

# Raspberry Pi E-mail Notifier Using LEDs

## Remote SSH

As a first step, you'll want a terminal on your Pi. There are a variety of options, explored in more detail in the [Getting a Terminal on Your Raspberry Pi](../../../../what-is-the-command-line/getting-a-terminal-on-your-raspberry-pi) section of our [introduction to the command line](../../../../what-is-the-command-line/overview):

1. Plug in an HDMI monitor and keyboard
2. [Use a console cable](../../../../adafruits-raspberry-pi-lesson-5-using-a-console-cable/overview)
3. [Log in via **S** ecure **SH** ell (SSH)](../../../../adafruits-raspberry-pi-lesson-6-using-ssh)

Since this project uses the GPIO pins for its own purposes, you'll want to choose between working directly on the Pi (this is fine if you're already set up!) or connecting via SSH (a good idea if you don't have an extra monitor / input devices laying around, and want to work from the comfort of your desktop or laptop).

Examples here will assume a working Raspbian installation with an SSH connection. Not sure how to get started?

For installating Raspbian, check out our [guide to preparing an SD card for the Pi](../../../../adafruit-raspberry-pi-lesson-1-preparing-and-sd-card-for-your-raspberry-pi/overview). If you need help finding the Pi on your network and connecting to it, check out our Pi Finder utility.

[Adafruit Pi Finder](https://github.com/adafruit/Adafruit-Pi-Finder)
...and here's our [guide to using SSH](../../../../adafruits-raspberry-pi-lesson-6-using-ssh).

# Raspberry Pi E-mail Notifier Using LEDs

## Prepare Python

In order for our Python code to work, we'll want to make sure a few libraries are installed.  
  
First, from either the keyboard/monitor or SSH console type in:

```auto
sudo apt-get install python-pip
```

![](https://cdn-learn.adafruit.com/assets/assets/000/024/090/medium800/raspberry_pi_Screenshot-2015-03-27-10_24_13.png?1427473557)

...you'll be asked if you want to continue. Type "Y" for yes, and hit enter.

![](https://cdn-learn.adafruit.com/assets/assets/000/024/091/medium800thumb/raspberry_pi_screencast-2015-03-27-10_24_35.jpg?1448317266)

This part will probably take a little while.

Then you can install the&nbsp;[IMAPClient](http://imapclient.readthedocs.org/en/latest/) Python library, which lets Python talk to most e-mail services:

```auto
sudo pip install imapclient
```

![](https://cdn-learn.adafruit.com/assets/assets/000/024/092/medium800/raspberry_pi_Screenshot-2015-03-27-11_01_04.png?1427477493)

Danger: 

The current version of the script should actually work with any e-mail provider that provides [IMAP](https://en.wikipedia.org/wiki/Internet_Message_Access_Protocol) access. A few useful links:

- [Yahoo! Mail IMAP Settings](https://help.yahoo.com/kb/mobile/imap%C2%A0settings-yahoo-mail-sln4075.html)
- [Hotmail / Outlook.com IMAP Settings](http://www.howto-outlook.com/howto/accountsettings.htm#outlookcom-imap)
- [iCloud: Mail server settings for email clients](https://support.apple.com/en-us/HT202304)

I **f you have two-factor authentication enabled on your GMail account** , you'll need to generate an application-specific password to use IMAP. I[followed Google's detailed instructions](https://support.google.com/mail/answer/1173270?hl=en) and was up and running in no time.

Not sure if you have two-factor auth enabled? That probably means you don't, so don't worry about it for now.


## Related Guides

- [Breakbeat Breadboard](https://learn.adafruit.com/breakbeat-breadboard.md)
- [HalloWing All-Seeing Skull](https://learn.adafruit.com/hallowing-all-seeing-skull.md)
- [Moonshine Voice Control on Raspberry Pi](https://learn.adafruit.com/moonshine-voice-control-on-raspberry-pi.md)
- [File Glider](https://learn.adafruit.com/file-glider.md)
- [ABC Soundboards with NeoTrellis](https://learn.adafruit.com/abc-soundboards-with-neotrellis.md)
- [Light Up Paper Dragon Wall Sconce](https://learn.adafruit.com/light-up-paper-dragon-wall-sconce.md)
- [Moon Phase Clock for Adafruit Matrix Portal](https://learn.adafruit.com/moon-phase-clock-for-adafruit-matrixportal.md)
- [Adafruit Speaker Bonnet for Raspberry Pi](https://learn.adafruit.com/adafruit-speaker-bonnet-for-raspberry-pi.md)
- [Circuit Playground Express Rocket Lamp](https://learn.adafruit.com/cpx-rocket-lamp.md)
- [LoRa and LoRaWAN Radio for Raspberry Pi](https://learn.adafruit.com/lora-and-lorawan-radio-for-raspberry-pi.md)
- [Adafruit AMG8833 8x8 Thermal Camera Sensor](https://learn.adafruit.com/adafruit-amg8833-8x8-thermal-camera-sensor.md)
- [Matrix Portal Creature Eyes](https://learn.adafruit.com/matrix-portal-creature-eyes.md)
- [CircuitPython OLED and Dual Knob Sketcher](https://learn.adafruit.com/circuitpython-oled-knob-sketcher.md)
- [Daily Cheer Automaton](https://learn.adafruit.com/daily-cheer-automaton.md)
- [Matrix Portal New Guide Scroller](https://learn.adafruit.com/matrix-portal-new-guide-scroller.md)
