Arduino and Gmail

Teaching Arduino at the Hack Factory

Getting Arduino to communicate with Gmail, from the class that I taught at Twin Cities Maker

The basics were getting Arduino to talk to Gmail and then turn on a LED.

Step one: Load you Arduino with PhysicalPixel. It’s under examples, Communication.
Step two: Install Python and Pyserial
Step three download the ArduinoandGmail.py code

Step Four: Edit the Python Code. You will need to edit two things:

s = serial.Serial('/dev/tty.usbserial-A6008dvx', 9600)
Between the first tick marks enter your serial connection, you can find this by going to the Arduino IDE and then Tools, Serial Port, what is checked is what goes between the tick marks.

#inser username then password
rc, resp = M.login('USERNAME', 'PASSWORD')
print rc, resp

Enter you username and password.

Step Five: Run the python script and you should be good to go, the python will send letter ‘H’ when you have 1 or more unseen emails and ‘L’ when you have 0.

Step Six: hook up a LED and Resistor to Pin 13, or hook something else up that you want to turn on.

Step Seven: Get the LED to turn off, Join up with others talking about Inbox Zero or you can change the threshold to what ever level you want and ignore Inbox Zero.

the Python Code:

# Code my Mike Hord, Kelly, and Paul Sobczak
# Gmail and Arduino 3/19/11
# Twin Cites Maker

import serial
import time
import imaplib
import re
import string

# interval change for less often
INTERVAL = 1 # check every INTERVAL minutes

last_check = time.time() – INTERVAL*60 # subtract so that we check first time

#change between the ticks your serial link on arduino
s = serial.Serial(‘/dev/tty.usbserial-A6008dvx’, 9600)

time.sleep(1.5)
s.write(‘L’)
IMAP_SERVER=’imap.gmail.com’
IMAP_PORT=993
M = imaplib.IMAP4_SSL(IMAP_SERVER, IMAP_PORT)

#insert username then password
rc, resp = M.login(‘USERNAME’, ‘PASSWORD’)
print rc, resp

# main loop
while True:
if time.time() – last_check < INTERVAL*5:
continue
last_check = time.time()

status_string = M.status(‘INBOX’, ‘(UNSEEN)’)[1]
print status_string[0]
p = re.compile(‘\d+’)
unread_count = string.atoi(p.findall(status_string[0])[0],10)
print unread_count
if unread_count > 0:
s.write(‘H’)
else:
s.write(‘L’)

This code was written Mostly by Mike Hord who operates the Uptownmaker Blog, Kelly and myself.

Update: This is now hosted on a Google Project Page

Post a Comment

Your email is never shared. Required fields are marked *

*
*