Archive

Posts Tagged ‘Electronics’

Yet Another Gmail Notifier

February 9, 2009 Leave a comment

Came across Jamie Matthews’ cool gmail notifier. Just the kind of thing i wanted to run on the ARM7 board that i’m trying out right now. Not much of a challenge for the ARM MCU’s processing power but it was fun making it work. I used the on-board seven-segment LED to display the mail count (it was a single display so the mail count is limited to 9).

gmailscreenshot

LPC 2129 Evaluation Board

LPC 2129 Evaluation Board

 

 

 

 

 

 

 

 

 

 

 

 

 

 

The python script was slightly modified:

1) In windows, just change the com port address to COMx where x is the port number as seen in the Device Manager

2)I sent the mail count (assumed to be less than 9) instead of the Y/N string.

To run the script periodically, i used  Andreas Baumann’s free Z-cron to schedule it once every five minutes. But the annoying thing was the command prompt that kept popping up when the script ran. I’m sure it could have been run as a windows service in the background but did not have the patience to look it up.

The setup

The python script:

import urllib2, re, serial, sys

 #Settings - Change these to match your account details
USERNAME="yourID@gmail.com"
PASSWORD="your password"

PROTO="https://"
SERVER="mail.google.com"
PATH="/gmail/feed/atom"

SERIALPORT = "COM5" # Change this to your serial port!

# Set up serial port
try:
        ser = serial.Serial(SERIALPORT, 9600)
        #print ser.portstr #For Debug:Check if port name is correct !
except serial.SerialException:
        sys.exit()

# Get Gmail Atom feed
passman = urllib2.HTTPPasswordMgrWithDefaultRealm()
passman.add_password(None, SERVER, USERNAME, PASSWORD)
authhandler = urllib2.HTTPBasicAuthHandler(passman)
opener = urllib2.build_opener(authhandler)
urllib2.install_opener(opener)
page = urllib2.urlopen(PROTO + SERVER + PATH)

# Find the mail count line
for line in page:
        count = line.find("fullcount")
        if count > 0: break

# Extract the mail count as an integer
newmails = int(re.search('\d+', line).group())

# Output data to serial port
if newmails > 0:
                ser.write(str(newmails))
                #print "No.of mails=%d" %newmails
else: ser.write(str(0))

# Close serial port
ser.close()

The C program on the LPC 2129:

/*Tested on the ARM starter kit (http://www.emblitz.com/Embedded_ARM_Starter_kit.html)*/

 #include <LPC21xx.H>
 /*The 7 segment pattern for  digits 0 through 9*/
 const unsigned char bitMask8[] = {
   0x80,  // binary 10000000
   0x40,  // binary 01000000
   0x20,  // binary 00100000
   0x10,  // binary 00010000
   0x08,  // binary 00001000
   0x04,  // binary 00000100
   0x02,  // binary 00000010
   0x01   // binary 00000001
};

 void ser_init(void); //initialize serial port
 void send_8bit_serial_data(unsigned char); //display data on the 7 segment

 int main(void)
  {  char no_of_mails[10]={0xfc,0x60,0xda,0xf2,0x66,0xb6,0xbe,0xe0,0xfe,0xf6};
     char data;
      //mapping of pins to serial in parallel out shift register
		/*P1.16-->(~QH)--port dir= i/p
	              P1.17-->SRCLK--port dir= o/p
	              P1.18-->RCLK--port dir= o/p
	              P1.19-->SD1--port dir= o/p
	           */
     IODIR1=0x000E0000;//set port P1 direction to reflect the pin connections detailed above
     ser_init();
	 send_8bit_serial_data(~(no_of_mails[0])); //Reset LED
   	 while(1)
   		{   while(!(U1LSR & 0x01)); //wait till data arrives
	     	data=U1RBR-48; //convert ascii back to integer
			send_8bit_serial_data(~(no_of_mails[data]));
    	}

      return 0;  //this is never reached
  }

void ser_init(void)
{
  PINSEL0 = 0x00050000;                  /* Enable RxD1 and TxD1              */
  U1LCR = 0x83;                          /* 8 bits, no Parity, 1 Stop bit     */
  U1DLL = 97;                            /* 9600 Baud Rate @ 15MHz VPB Clock  */
  U1LCR = 0x03;                          /* DLAB = 0                          */
}

void send_8bit_serial_data(unsigned char data)
{ /*The 7 segment is driven  bit-banging  style using  SN74HC595D, */
    int x;
 	IOCLR1=0xffffffff;
   // Loop through all the bits, 7...0
   for(x = 7; x >=0; x--)
   {
       if(data & bitMask8[x])
       {
           IOSET1=0x00080000;      // we have a bit, make P1.19 high
		   IOSET1=0x000a0000; 		//make p1.17=SRCLK aslo hign
		   IOCLR1=0x00020000;		//toggle back p1.17
       }
       else
       {
           IOCLR1=0x000F0000;        // no bit, make P1.19 low
		   IOSET1=0x00020000;
		   IOCLR1=0x00020000;
       }

   }

   IOCLR1=0x000F0000; //TOGGLE P1.18=RCLK
   IOSET1=0x00040000;
   IOCLR1=0x000F0000;
}

If you wanna take this further, check out this page for an  amazing  LCD based notifier.

Advertisements