- Scanned version of original article published in Electronics For You : pdf (9.7MB)
- Draft of article sent to EFY for publishing : pdf
- gmailtolcd application : zip (please rename the file to gmailtolcd.zip)
My article titled “Using ‘serdisplib’ library to drive mobile phone LCD” was published in the “Do-It-Yourself” section of Electronics For You (May 2008 issue) magazine. This was the first time when I was paid money for writing an article. I have been reading this magazine since I was 12 and was delighted to see my name in one of the articles of this magazine. The utility of mobile phone lcd interfacing is demonstrated using a “gmailtolcd” application. The “gmailtolcd” application fetches information about new emails from gmail POP server and then displays the information about new emails on the mobile phone LCD.
I contributed the code to add support for Nokia 6100 LCD in serdisplib library project. This is my first contribution to a open source project. I have been using open source stuffs from last 8 years. I had never thought that I would ever get involved in contributing to a OSS project. It was an amazing experience to see my name in “AUTHORS” file shipped with serdisplib package :).
gmailtolcd.py code listing
# # GmailToLCD version 1.0 # # Opens Gmail (http://www.gmail.com) mailbox and displays information # about arrival of new mails on Nokia 6100 (S1D15G10 controller) LCD # using serdisplib (http://serdisplib.sourceforge.net) library. # # Author: # Abhishek Dutta # email@example.com # http://abhishekdutta.org # Aug 15, 2007 # # ************************************************************************* # * This program is free software; you can redistribute it and/or modify # * it under the terms of the GNU General Public License as published by # * the Free Software Foundation; either version 2 of the License, or (at # * your option) any later version. # * # * This program is distributed in the hope that it will be useful, but # * WITHOUT ANY WARRANTY; without even the implied warranty of # * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # * General Public License for more details. # * # * You should have received a copy of the GNU General Public License # * along with this program; if not, write to the Free Software # * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA # * 02111-1307, USA. Or, point your browser to # * http://www.gnu.org/copyleft/gpl.html # ************************************************************************* # # Revision History # - Sep. 03, 2007 # The GMAIL POP server considers the emails in "Sent Mail" folder (ie: the email messages # you send from gmail) as new messages. Hence, this script filters out all the new (not actually) # messages whose "From: " field email address matches the email address of the mailbox # being checked. This modification was made to only show the new mail messages arriving in the # mailbox. # # Copyright (c) 2007 by Abhishek Dutta # import poplib # for pop mailbox access import Image, ImageFont, ImageDraw import sys # for accessing argv import re # for regular expression matching import os # for os.system() import time # for sleep() if (len(sys.argv) < 3): print "GmailToLCD version 1.0" print "" print "Usage: $ python gmailtolcd.py USERNAME PASSWORD" print "" print "View README file for details" else: print "GmailToLCD\tver. 1.0" os.system("./multidisplay -n n6100 -f app_template.png") print "Querying mailbox ..." # Gmail POP3 access requirements pop_gmail = 'pop.gmail.com' username = sys.argv password = sys.argv port = 995 # time (in seconds) to delay between each mailbox query delay = 60 # Add string "@gmail.com" if not supplied by user if not(username.endswith('@gmail.com')): username = username + "@gmail.com" # regular expression for matching "From: " and "Subject: " # fields in the new retrived email pat_from = re.compile('From:*') pat_subject = re.compile('Subject:*') # loop until user presses Ctrl + C key while 1: print "Checking for new message ...\t", # access the gmail inbox using POP3 SSL # NOTE: The details of latest email (from, subject) # message will be shown on the LCD mbox = poplib.POP3_SSL(pop_gmail,port) mbox.user(username) mbox.pass_(password) #mbox.set_debuglevel(3) numMessages = len(mbox.list()) if not(numMessages == 0) : # Counts the new mails in 'Inbox' folder new_msg_count = 0; print "Total ",str(numMessages)," new messages. (in Inbox & Sent Mail)" # Query all the messages from server for i in range(numMessages): for j in mbox.retr(i+1): # extract the matching lines m1 = pat_from.match(j) m2 = pat_subject.match(j) #print j if m1: # print j # index of first occurence of inverted comma " a1 = j.find('"',0,len(j)) # extract the name of sender which begins after inverted comma name = j[(int)(a1+1):(int)(a1+15)] # Gmail POP server sends the new messages in # "Send Mail" folder as new mails to this script # Hence, discard messages whose "From" field # indicate the user executing this script pos_st_em = j.find("<",0,len(j)) pos_ed_em = j.find(">",0,len(j)) from_email_addr = j[(int)(pos_st_em+1):pos_ed_em] if not(from_email_addr == username) : print "\tNew Email in Inbox - From: ",name,"\t", new_msg_count = new_msg_count + 1; else: # the message received belongs to # "Sent Mail" folder break elif m2: # Find the index of space between 'Subject:' and the subject a1 = j.find(" ",0,len(j)) a1 = a1+1 # MAX CHARS in line 1 of LCD 'Subject' field = 13 line1 = j[a1:(int)(a1+13)] # MAX CHARS in line 2 of LCD 'Subject' field = 21 line2 = j[(int)(a1+13):(int)(a1+13+21)] print "Subject: ",line1, print line2 # Display alert if new message has arrived # in 'Inbox' folder only if not (new_msg_count == 0) : # Open the template image file im = Image.open("app_template.png") draw = ImageDraw.Draw(im) # use a truetype font # NOTE: These font's must exist in the directory from # which gmailtolcd.py is being executed # Liberation Mono True Type Font (GPL Font release by Redhat) font = ImageFont.truetype("LiberationMono-Regular.ttf", 10) font12 = ImageFont.truetype("LiberationMono-Regular.ttf", 12) # append a '0' to message count if numMessages < 10 : msg_count = "0"+str(numMessages) else: msg_count = numMessages # Write no. of new mails to image # MAX CHARS = 2 draw.text((109, 62),str(msg_count) , font=font12,fill=228) # Write name of sender to image # MAX CHARS = 13 draw.text((38, 80), name, font=font,fill=228) # Write subject to the image # MAX CHARS (Total) 34 # line 1 of subject field MAX CHARS = 13 draw.text((48, 100), line1, font=font,fill=228) # line 2 of subject field MAX CHARS = 21 draw.text((2, 114), line2, font=font,fill=228) del draw #write new the image to file im.save("gmailbox.png", "PNG") print ("Updating LCD display") os.system("./multidisplay -n n6100 -f gmailbox.png") else: #new messages in "Send Mail" folder but not in "Inbox" print "No New Messages in 'Inbox' folder" else: print "No New Message in 'Inbox' and 'Send Mail'" mbox.quit() print "Sleeping for",delay,"seconds ..." try: time.sleep(delay) except (KeyboardInterrupt, SystemExit): # show BYE message in LCD before exiting os.system("./multidisplay -n n6100 -M 'Bye ! Bye !'") # reset the display before exiting print("Resetting LCD display ...") os.system("./multidisplay -n n6100 -Q") sys.exit("Bye")