Make a Raspberry Pi Gmail Notification Light
Make a Raspberry Pi Gmail Notification Light
sudo mkdir gmail_python The "mkdir" command stands for "Make Directory". Anything following this will be used for the directory name. You should now be able to see your directory: ls If you made a mistake, you can easily remove this directory: sudo rm -r gmail_python Now navigate into the new directory: gmail_python/ Create a new Python script: sudo nano check_messages.py This will create the script and open it ready for editing in . You could of course use another program, such as , although this tweet sums up my feelings about that: Joking aside, check out this for a full breakdown. Press CTRL + X to exit Nano and get back to the terminal.
GPIO.setwarnings()
GPIO.setmode(GPIO.BCM)
GPIO.setup(, GPIO.OUT)
g = gmail.login(, )
unread_messages = g.inbox().mail(unread=)
total_messages =
message unread_messages:
total_messages +=
total_messages > :
GPIO.output(, )
:
GPIO.output(, ) You will need to enter your username and password for this to work. You can view the full source code for the gmail plugin if you would like to. If you do not feel comfortable doing this (or you are using two-factor authentication) you will need to connect to Gmail using . This is a bit involved for this tutorial, however Google has an excellent . Let's breakdown the code. First some modules are imported. Modules in Python are small pieces of code written for a purpose (similar to libraries in the Arduino IDE). RPi.GPIO is a Pi specific module for accessing the GPIO, gmail is the module you downloaded previously, and time is a module built into Python to provide timing functions. Now "GPIO.setmode" and "GPIO.setup" are used to tell the Pi that pin 14 is an output, and that you want to use "Broadcom Pin Numbering" (). This line connects to your gmail account. It creates an object called "g", and calls the login method of the gmail module imported previously. Don't forget to enter your gmail email and password. g = gmail.login(, ) Now retrieve all the unread messages and store them in a variable called "unread messages": unread_messages = g.inbox().mail(unread=) Notice how "unread=True" is passed as a parameter -- you can change this to retrieve messages based on different parameters, such as sender or subject. Check out the for lots more information. Next, a for loop is used to loop over every message: message unread_messages:
total_messages += For loops are very useful. They repeat a block of code several times, often with a slightly different value each time. This for loop goes over every message in unread_messages and increments the "total_messages" variable. Finally, some simple "if" statements are used. If there are unread messages, turn the LED on, otherwise turn if off. Remember that Python is case sensitive, and uses white spacing. If you are having problems getting the code to run, try . Paste your Python in and press the "validate above python code" button. This should then tell you what (if any) errors are present in your Python. Switch to the Terminal and run your script: python check_messages.py This command will run your script. Try manually changing some emails in your inbox to unread status and running the script again -- you should see the LED turn on or off to reflect your inbox.
MUO
Make a Raspberry Pi Gmail Notification Light
In this quick and easy Raspberry Pi project, you'll learn how to make a Gmail email notification light. If you have any unread emails, a Python script turns the LED on. In this quick and easy Raspberry Pi project, you'll learn how to make a Gmail email notification light. If you have any unread emails, a Python script turns the LED on. This project requires very few parts, and can be completed in under an hour! You can of course dress up your LED any way you like, such as a MineCraft redstone block, or other object 3D printed in clear plastic. Here's the end result:What You Need
1 x Raspberry Pi 1 x breadboard 1 x 220 ohm resistor 1 x 5mm LED 1 x Gmail account Male to female hook up wires Any Raspberry Pi will work for this project -- even the Pi Zero! Only one GPIO pin is needed, and it's not particularly CPU intensive. If you have a you have more than enough parts to complete this.Build Plan
This is a really simple project. A Light Emitting Diode (LED) is connected to a GPIO (General Purpose Input Output) Pin on the Pi. A very simple Python script will run regularly to check to unread emails and turn the LED on or off accordingly.The Hardware
Connect the positive anode (long leg) of the LED to the resistor and then to GPIO pin 14. You could use any GPIO pin, however look at the first, as they vary slightly between models. Connect the negative cathode (short leg with flat edge) to ground.Pi Setup
Providing your Pi has an operating system (OS) installed there's not a lot of setup needed (not sure what you need? ). Open a new terminal (Top left > menu > Accessories > Terminal) on the Pi (checkout these ). You need to create a new folder to store the Python script. Enter the follow command: This stands for "Print Working Directory", and will show you what folder you are in (by default this be "/home/pi"). Navigate into the documents folder and create a new directory (folder) called "gmail_python": Documents/sudo mkdir gmail_python The "mkdir" command stands for "Make Directory". Anything following this will be used for the directory name. You should now be able to see your directory: ls If you made a mistake, you can easily remove this directory: sudo rm -r gmail_python Now navigate into the new directory: gmail_python/ Create a new Python script: sudo nano check_messages.py This will create the script and open it ready for editing in . You could of course use another program, such as , although this tweet sums up my feelings about that: Joking aside, check out this for a full breakdown. Press CTRL + X to exit Nano and get back to the terminal.
Python Setup
Now that the Pi is setup, it's time to write the code. This project requires the excellent by . Download the library from Github and extract the contents. Inside there should be a folder called "gmail". Copy this whole folder into "/home/pi/Documents/gmail_python". Switch back to the command line and open your script again (if you press the up key you can scroll through your previously entered commands): sudo nano check_messages.py Notice how that is the same command you used to create the file -- if a file already exists it will be opened, otherwise it will be created. Here's the Python: gmail, RPi.GPIO GPIO, timeGPIO.setwarnings()
GPIO.setmode(GPIO.BCM)
GPIO.setup(, GPIO.OUT)
g = gmail.login(, )
unread_messages = g.inbox().mail(unread=)
total_messages =
message unread_messages:
total_messages +=
total_messages > :
GPIO.output(, )
:
GPIO.output(, ) You will need to enter your username and password for this to work. You can view the full source code for the gmail plugin if you would like to. If you do not feel comfortable doing this (or you are using two-factor authentication) you will need to connect to Gmail using . This is a bit involved for this tutorial, however Google has an excellent . Let's breakdown the code. First some modules are imported. Modules in Python are small pieces of code written for a purpose (similar to libraries in the Arduino IDE). RPi.GPIO is a Pi specific module for accessing the GPIO, gmail is the module you downloaded previously, and time is a module built into Python to provide timing functions. Now "GPIO.setmode" and "GPIO.setup" are used to tell the Pi that pin 14 is an output, and that you want to use "Broadcom Pin Numbering" (). This line connects to your gmail account. It creates an object called "g", and calls the login method of the gmail module imported previously. Don't forget to enter your gmail email and password. g = gmail.login(, ) Now retrieve all the unread messages and store them in a variable called "unread messages": unread_messages = g.inbox().mail(unread=) Notice how "unread=True" is passed as a parameter -- you can change this to retrieve messages based on different parameters, such as sender or subject. Check out the for lots more information. Next, a for loop is used to loop over every message: message unread_messages:
total_messages += For loops are very useful. They repeat a block of code several times, often with a slightly different value each time. This for loop goes over every message in unread_messages and increments the "total_messages" variable. Finally, some simple "if" statements are used. If there are unread messages, turn the LED on, otherwise turn if off. Remember that Python is case sensitive, and uses white spacing. If you are having problems getting the code to run, try . Paste your Python in and press the "validate above python code" button. This should then tell you what (if any) errors are present in your Python. Switch to the Terminal and run your script: python check_messages.py This command will run your script. Try manually changing some emails in your inbox to unread status and running the script again -- you should see the LED turn on or off to reflect your inbox.