perkee's blog

I heard you like science. tags
Mar 01
Permalink

Tangible Gmail (0)

I’m working on a gmail notifier, borrowing heavily from Jamie Matthews.

I am using his second Python Script.  I had never used Python before today, but I got the hang of it.  The whole “Leading whitespace is significant, but there are no brackets to guide you” thing threw me for a minute or two.  I think.

Here’s what I did to the Python script to make it more awesome

import serial, sys, feedparser

#Settings - Change these to match your account details
USERNAME="ahsperkins"
PASSWORD="yeah, nuh uh, you ain't getting it that easy"
PROTO="https://"
SERVER="mail.google.com"
PATH="/mail/feed/atom"

SERIALPORT = "/dev/tty.usbserial-A6006hmD" # Change this to your serial port!

# Set up serial port
try:
ser = serial.Serial(SERIALPORT, 9600, timeout=None)
print "I think serial setup worked"
except serial.SerialException:
sys.exit()
print "serial setup did not work"

newmails = int(feedparser.parse(PROTO + USERNAME + ":" + PASSWORD + "@" + SERVER + PATH)["feed"]["fullcount"])

# Output data to serial port
ser.write(chr(newmails))
#I've changed this to let me use the actual number of unread mail messages.
print newmails #Pretty useful for debugging, but take it out later if you'd like
# Close serial port
ser.close()

Thus far, my problem is that the notifier only seems to get updates when I have the Arduino development environment runing with the serial monitor open.  I, like Jamie, am on a Mac.  I was actually thinking about rewriting the Python as a shell script, but Mac’s version of echo is a bit kooky, I think.

Pseudocode for the schell script would go like this:

pipe the gmail atom feed from curl to grep
use grep to extract the number of unread mails.
turn that string into a byte
send that byte to my serial port.

Haven’t yet tried it; I don’t know how to do it without using grep twice: once to get the line containing the unread mail string, and again to get just that string. My problem there is that I want to specify a regex that is only found in between two other patterns, but I only want to deal with the middle one, you know?

I made the arduino code take serial info and drive an led. It’s a modification of the Dimmer example in the Communication folder.

/*
 *Scaled Dimmer by Perkee/Perko
 *Check out http://perkee.tumblr.com/post/82642891/arduino-gmail
 *
 *
 * Dimmer
 * by David A. Mellis
 *
 *
 * Demonstrates the sending data from the computer to the Arduino board,
 * in this case to control the brightness of an LED.  The data is sent
 * in individual bytes, each of which ranges from 0 to 255.  Arduino
 * reads these bytes and uses them to set the brightness of the LED.
 *
 *EDIT: It scales them before writing to the LED
 * http://www.arduino.cc/en/Tutorial/Dimmer
 */
//This moves a meter needle, changes an LED brightness, etc. 
//depending on the number of emails
const int fadePin = 11;

//This goes HIGH if there are any unread emails
const int binaryPin = 13;

//This is used to scale the output.
//Otherwise your meter would go from 0 to 255 emails.
//Depending on who you are, that's too many or too few

const int maxEmail = 10;

byte val;

void setup()
{
  // begin the serial communication
  Serial.begin(9600);
  pinMode(fadePin, OUTPUT);
  pinMode(binaryPin, OUTPUT);
}

void loop()
{
  // check if data has been sent from the computer
  if (Serial.available() > 0)
  {
    // read the most recent byte (which will be from 0 to 255)
    val = map(constrain(Serial.read(),0,maxEmail),0,maxEmail,0,255);
    // set the brightness of the LED
    analogWrite(fadePin, val);
    if (val > 0)
    {
      digitalWrite(binaryPin, HIGH);
    }else {
      digitalWrite(binaryPin, LOW);
    }
  }
}
Comments (View)
blog comments powered by Disqus