{else}

How to make an LED Arduino Duemilanove gmail checker for your windows computer

Written by Matthew Forr View Comments

Recently I attended a workshop organized by Baltimore Node; my local hackerspace. The goal of the workshop was to build a simple device that would change colors when one would twitter a color to it.

The workshop was a resounding success except on the windows side due to difficulties with Python. I enjoyed it and was able to get the software working on my 12” Apple Powerbook but being the practical person I am I want something that’s usable in my day-to-day life. IE a feed checker, in this case a gmail checker.

My end goal is to make a device that will let me know when someone has twittered (@reply or direct message) about the multiple accounts I am to watch for my day job so I’ve started down that path.

This tutorial is to build upon what I learned in aforementioned workshop and some code from this project.

So materials.

Step 1: Setup the LED

Solder the wires to the LED, pretty straightforward.

The black wire goes to the connection with a minus sign (-)
The blue wire to the B+
The red wire to the R+
And the white wire to the G+

Step 2: Hook the LED to the Arduino

Hook the wires up.

The red wire goes to digital pin 9, white to 10 and blue to 11.

Step 3: Download code to the Aruino

If you do not have the Arduino environment installed do so. You can get it from here.

Plug the Arduino in and download the following code to it. If you aren’t sure how it’s really easy, just copy and past this code into the environment, make sure the Arduino is plugged in and hit the upload button. You should see some lights on the unit blink and when it is done the environment will indicate so.

*Make sure you have the correct port selected by going to Tools>Port


/*
* Serial RGB LED TOO
*—————————
* Serial commands control the brightness of R,G,B LEDs
*
* Command structure is “#RRGGBB”
*
*
* Created 18 October 2006
* copyleft 2006 Tod E. Kurt

* http://todbot.com/
*/

#define slen 7 // 7 characters, e.g. '#ff6666'
char serInStr[slen]; // array to hold the incoming serial string bytes

int redPin = 9; // Red LED, connected to digital pin 9
int greenPin = 10; // Green LED, connected to digital pin 10
int bluePin = 11; // Blue LED, connected to digital pin 11

void setup() {
pinMode(redPin, OUTPUT); // sets the pins as output
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(9600);
analogWrite(redPin, 127); // set them all to mid brightness
analogWrite(greenPin, 127); // set them all to mid brightness
analogWrite(bluePin, 127); // set them all to mid brightness
Serial.println("enter color command (e.g. '#ff3333') :");
}

void loop () {
//read the serial port and create a string out of what you read
int spos = readSerialString();

if(spos==slen && serInStr[0] == '#') {
long colorVal = strtol(serInStr+1,NULL,16);
Serial.print("setting color to r:");
Serial.print((colorVal&0xff0000)>

>16);
  Serial.print(” g:”);
  Serial.print((colorVal&0x00ff00)>>8);
  Serial.print(” b:”);
  Serial.println((colorVal&0x0000ff)>>0);
  memset(serInStr,0,slen);    // indicates we’ve used this string
  //spos = 0;
  analogWrite(redPin, (colorVal&0xff0000)>>16 );
  analogWrite(greenPin, (colorVal&0x00ff00)>>8 );
  analogWrite(bluePin, (colorVal&0x0000ff)>>0 );
  }

  delay(200);  // wait a bit, for serial data
}

//read a string from the serial and store it in an array
int readSerialString ()
{ int i=0;
if(!Serial.available())
{
return -1;
}
  while (Serial.available() && i

< slen)
{
int c = Serial.read();
serInStr[i++] = c;
}
Serial.println(serInStr);
return i;
}

Step 4: Setup Python with various Eggs and included libraries

Get Python setup and running. This is complicated so follow along.

Download and install the following:

Python 2.5.4
Pyserial 2.4
Pywin32 build 214 for python 2.5
Python-Twitter 0.6
Simplejson 2.0.9
Feedparser

For some of these you will need to be able to run Python from the command prompt. To do that right click on ‘My Computer’ > ‘Advanced’ > ‘Environmental variables’ and find “path” and add the line ;C:\DIRECTORYOFPYTHONHERE to the end of what is already there. More details can be found here.

Step 5: Execute the gmail checker Python script

Wow, that was a long step. Fortunately we are almost up and running. Open up a texteditor (Notepad++ is my favorite) and copy and paste this code into it:


import serial, sys, feedparser, time
#Settings - Change these to match your account details
USERNAME=“username@gmail.com”
PASSWORD=“enteryourpassword”
PROTO=“https://”
SERVER=“mail.google.com”
PATH=”/gmail/feed/atom”
SERIALPORT = “COM3” # Change this to your serial port!
# Set up serial port
try:
ser = serial.Serial(SERIALPORT, 9600)
except serial.SerialException:
print “no device connected - exiting”
sys.exit()

while 1:
      newmails = int(feedparser.parse(PROTO + USERNAME + “:” + PASSWORD + “@” + SERVER + PATH)[“feed”][“fullcount”])
 
      # Output data to serial port
      if newmails > 0:
      ser.write(”#ff0000”)
      print “some mail”
      else:
      ser.write(”#000000”)
      print “no mail”
      #print data to terminal

      time.sleep(30)


Now change the USERNAME, PASSWORD and SERIALPORT variables to match your account and whichever serial port your Arduino is hooked into.

Save the file, right click it and hit ‘Open with IDLE’. Hit Run.

Step 6: Finished product

If all has gone well for you so far you should have an LED that turns off when you have no mail and turns red when you do have mail.