Posts Tagged ‘ python

Use arduino to check gmail !


Plop,

This is what I got :
A LED that lights up when I got unread mail in my INBOX on Gmail.

This is what you need :
- an arduino
- python 2.7.2
- python-pyserial 2.6
- something for output, a LED is good

This is how I did :

1. The arduino code :


int outPin = 9; // Output connected to digital pin 9
int mail = LOW; // Is there new mail?
int val; // Value read from the serial port

void setup()
{
    pinMode(outPin, OUTPUT); // sets the digital pin as output
    Serial.begin(9600);
    Serial.flush();
}

void loop()
{
    // Read from serial port
    if (Serial.available())
    {
        val = Serial.read();
        Serial.println(val);
        if (val == '1') mail = HIGH;
        else if (val == '0') mail = LOW;
    }

    // Set the status of the output pin
    digitalWrite(outPin, mail);
}

2. The python code :


#!/usr/bin/python2
# Check new mail on gmail, pass it to arduino
import time, imaplib, re, serial, sys

SERIALPORT = "/dev/ttyACM0" # Change this to your serial port!

# Set up serial port
try:
    ser = serial.Serial(SERIALPORT, 9600)
except serial.SerialException:
    sys.exit()

conn = imaplib.IMAP4_SSL("imap.gmail.com", 993)
conn.login('username', 'YOURPASSWORD')
unreadCount = re.search("UNSEEN (\d+)", conn.status("INBOX", "(UNSEEN)")[1][0]).group(1)
# to avoid race
time.sleep(1)
# for debugging
#print unreadCount
# Output data to serial port
if unreadCount != '0':
    ser.write('1')
else:
    ser.write('0')
# Close serial port
ser.close()

3. Add a cronjob every minute :
$ crontab -e
*/1 * * * * python2 /path/to/script/checkmail.py >> /dev/null 2&>1

I added a resistance, otherwise it’s too much light !

ENJOY ! :D

Read emails from gmail in OpenBox menu

Hello,
I found a nice little script that works well to have gmail inside the Openbox menu ! oO
Here is what it looks like :
openbox python check gmail menu

And here is how to install it without any hassle :)
Download the tarball (GPL v2 license)
$ wget http://www.nakamura-gebiet.de/scripts/checkmail.tar.bz2
Install a needed dependency :
$ yaourt -S pyosd
Extract the tarball :
$ tar xvjf checkmail.tar.bz2
Edit the well commented configuration file :

$ $EDITOR checkmailrc
$ mv checkmailrc ~/.checkmailrc

Hey, your pass is in cleartext in here !
$ chmod 600 ~/.checkmailrc
$ mv checkmailrc.py.english ~/.scripts/checkgmail.py

Or anywhere else you would like you script to be located (author suggests ~/.config/openbox/scripts)
$ chmod +x ~/.scripts/checkmailrc.py
Now we add the line to the Openbox menu
$ $EDITOR ~/.config/openbox/menu.xml
Add this line where you want the gmail menu :
<menu id=”mails-menu” label=”Gmail” execute=”python2 ~/.scripts/checkmail.py”>
$ openbox –reconfigure

Now we need to get it updated as a cronjob :
$ crontab -e
Add this line to update every 10 minutes :
*/10 * * * * “export DISPLAY=O.0; python2 /path/to/script –refresh > /dev/null 2>&1″

Now open your openbox menu and enjoy your new quick and easy way to check your emails ! :D

Tested with openbox 3.4.11.2, python 2.7.2 on Archlinux 64bits.
(source of the script)