Raspberry Pi E-mail Notifier Using LEDs A basic

Powiadamiacz E-mail on the Raspberry Pi with LEDs

The popularity of Raspberry Pi has led to the fact that to make many things much easier. The example presented is simple design of the runway from the Raspberry Pi, which has to notify about new Gmail messages. After a short search, was found a fair amount of people who know how to do it, and some of his great examples. Michael from possessed the most developed code, which for this project was borrowed.


Powiadamiacz e-mail Raspberry Pi


The project uses two LEDs - green and red. The green led will light up when get a new message. At that time, when all messages are read, the red led turns on.


To complete this tutorial you will need:


Connecting the Cobbler to LEDs

New Raspberry Pi uses a 40-pin connector. The first generation PI used a 26 pin connector. The tutorial can use all these versions using the same code and GPIO pins. Common examples of connections of both types using the same GPIO pins (#18 and #23).


When you connect the GPIO cable, make sure you notice a red or white cable to the ribbon, it's the#1 pin of the cable. The end of this is connected closer to the SD card and is labeled P1 on the PI. The other side connects to the cobbler and can only be installed one way, due to the special nozzle.


Place the cobbler on the contact plate so that it was on both sides of the middle line. Connect the pin GND (ground) to the blue power rail on the Board contact. You will need two resistors (any values from 330 Ohm up to 1000 ohms is correct).


Connect the first resistor to pin cobbler marked #18 and its second end to a terminal which is not used.


Connect the other resistor to pin cobbler marked #23, and the other end to the other, not employed contact.


Raspberry Pi Cobbler Plus and 40-pin for Raspberry Pi 3B+ / 3B / 2B / Zero



Raspberry Pi Cobbler - 26-pin - only for Raspberry Pi A / B



Connection step by step in 26-pin Cobblerze PI



Now take the red and green led. Their long legs anodami (+).

Connect the anode of the red led to the resistor connected to pin #23 (GPIO #23), and the anode of the green led to the resistor connected to pin #18.


Shorter leg (cathode) connect to the solder points on the blue strip on the contact plate.



In these photos they are original Cobblery PI. In case of later, 40-pin models A+/B+/Pi 2, it is likely that you are doing something similar, using wires to connect resistors to LEDs (click on photo to enlarge):

Failed! Just connect two LEDs with current limiting resistors to the GPIO pins on the PI.


Required packages

Update your Pi to the latest version of Raspbian

Your PI should work on the latest version of Raspbian. This tutorial was written using Raspbian Stretch (November 2018). Check out our guide to Install operating system on Raspberry Pi, if you haven't already. After the installation is complete, enter the following commands to make sure your packages are up to date.


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


Install pip3

pip3 is already installed in the full version of Raspbian, Raspbian but Lite does not contain pip3, therefore it must be installed as shown below


$ sudo apt-get install python3-pip


Installing the adafruit-blinka


$ sudo pip3 install adafruit-blinka


Installation impaclient


sudo pip3 install imapclient


The Python Script



Now you can download any Python code to check your Gmail account. You can download the following script directly on the Raspberry Pi, edit it and run it, so that the LEDs burned.


import time 
import board 
from imapclient import IMAPClient 
from digitalio import DigitalInOut, Direction 

MAILBOX = 'Inbox' 
MAIL_CHECK_FREQ = 60 # check mail every 60 seconds 

# The following three variables must be customized for this 
# script to work 
USERNAME = 'your username here' 
PASSWORD = 'your password here' 
NEWMAIL_OFFSET = 1 # my unread messages never goes to zero, use this to override 

# setup PI pins as output for LEDs 
green_led = DigitalInOut(board.D18) 
red_led = DigitalInOut(board.D23) 
green_led.direction = direction.OUTPUT 
red_led.direction = direction.OUTPUT 

def mail_check(): 
 # login to mailserver 
 server = IMAPClient(HOSTNAME, use_uid=True, ssl=True) 
 server.login(USERNAME, PASSWORD) 

 # select our MAILBOX and looked for unread messages 
 unseen = server.folder_status(MAILBOX ['UNSEEN']) 

 # number of unread messages 
 # print to console to determine NEWMAIL_OFFSET 
 newmail_count = (unseen[b UNSEEN']) 
 print('%d unseen messages' % newmail_count) 

 if newmail_count > NEWMAIL_OFFSET: 
 green_led.value = True 
 red_led.value = False 
 green_led.value = False 
 red_led.value = True 


while True: 


Code download


For simplicity, place this file in your home directory. This facilitates the wget command.


$ cd 
$ wget


Changing variables via e-mail.


Don't forget to set the USER NAME and PASSWORD to match with your GMail account. (Remember, if you use two-factor authentication in Gmail, you then have to create a specific to use a password. If you use e-mail in your domain, you will probably have to check their documentation to know what HOSTNAME to use. Usually it goes like this


NEWMAIL_OFFSET is a variable, which you can set the current number of unread messages. During the run the python script, there will be access numerowe to the console showing that the number nieodczytanych messages.


Running the code


 $ python3 ./


Send each other some letters, to see how green led lights up!


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


Remote control via SSH


As a first step, you should have a terminal on your PI. There are various options, in our case is the introduction of multiple command-line:


  1. Connect the HDMI monitor and keyboard
  2. Use the Converter USB-UART
  3. Enter through the Secure SHell (SSH)


Because this project uses the GPIO pins for its own purposes, you will choose between the direct effect on PI (this is a great option if you already have everything ready!) and connect via SSH (a good idea if you don't have extra monitor/input device nearby, and you want to work on your own desktop or laptop).


The following examples will offer a sound installation of Raspbian with SSH.


Preparing Python


Below python code to work with, will need to make sure that you have installed, there were a few libraries.


First, or from the keyboard/monitor or SSH console, type:


sudo apt-get install python-pip



... you will be asked whether you want continue. Press "Y" for "Yes" and press enter.



This part will probably take some time.


Then you can install the IMAPClient Python library that allows to communicate with Pythonowi services e-email.


sudo pip install imapclient



Older versions of this guide used a Python module called "feedparserem" for reading Atom feed of Gmail


The current version of the script should work with emails in each domain, which provides access to IMAP. Some useful links:


If you have activated two-step authentication on your GMail, you will have to create a specific to use a password to use IMAP. Detailed instructions from Google is very useful in this case.


You're not sure that you have enabled two-factor authentication? This probably means that this is not you, so don't worry about it.