gmail notifier project (for dummies)

I’ve been really interested in doing J4mie’s Physical Gmail Notifier ever since it came out in February. I only recently dropped into the project and got to learn a lot about python, plists, and arduino auto-reset functionality. I’m going to share what I’ve learned because I think there are lots of other people out there struggling to make it work. This page doesn’t really offer much that J4mie didn’t already say – it just says it in a different way.




I’ll post the code below – one issue with the way J4mie posted his was that it appended line numbers to everything and messed with formatting. Python is _really_ picky about formatting! All of the files are zipped HERE. It consists of three documents –


1) one python script that logs into your gmail rss feed and checks your total number of new messages. It then sends either an ‘m’ or an ‘n’ to the serial port.


2) one arduino sketch that tells the arduino to turn on or off a light based on the serial input


3) one .plist file that tells your mac (sorry pcs!) to run the python script every 60 seconds. If you run windows or linux then I’m sure there is something you can do to schedule this. You could also just set it to loop until quit. (to quit a script in terminal hit control-c)


Section 1- Python. You will need to change the python script so that it has your email and password, as well as your serial port info. This assumes you are checking a gmail account. It also works if you are using gmail as your email host for a private domain. The serial port info you can grab from the arduino program or by typing “ls /dev/tty.*” into the terminal. Save that file and put it somewhere on your computer. I picked my Documents folder. You will also need to download and install feedparser. This is what helps python parse the rss feed into useable chunks.


Section two – Arduino. Now go ahead and open the arduino sketch included in the zip file, paste it into your arduino window, and upload it to your arduino. You don’t need to change anything on this.


Section three – .plist. Next you should open the plist file and change it so that it points to the python script you changed earlier. If you want to change the frequency that it checks your mail at, that is the “integer” tag towards the end of the file. It is measured in seconds. The plist file should go in your user folder under Library/LaunchAgents. If the folder isn’t there then go ahead and make it.


Until you restart the plist won’t do anything, so the simplest thing to do is to restart. If not then you can manually run the python script. To do that type “python ” into terminal and drag in the python file. Hit return to run it. Once it has run, you can hit the up arrow to recall the last command typed, and return to run it again.



Section four – Wiring The wiring is very easy. Hook up a led and an appropriate resistor (i (should have) used a 22 47 ohm – if you are unsure – use a led calculator) between pin 12 and +5v. The negative (shorter) lead goes to the pin side. Done! (footnote: Make sure that not only are you within spec for your led, but you also aren’t sourcing more than 40ma from the pin. Otherwise you could do damage to your Arduino! My initial setup sources 100ma and could have fried the pin.)


Open up the serial monitor in arduino and wait for a signal to come in. It should come in once every 60 seconds, so be patient. If it isn’t sending data, then it could be either sending it on the wrong port (check your serial port settings) or the plist could not be working yet (restart, or run it manually) You should be seeing either an “n” or an “m”. That should also be turning on or off the LED


One final thing to look out for is that any arduino with auto-reset enabled will only work while the arduino application is running _with_ serial monitor open. Otherwise it will just reset itself every time the data comes in. To check out how to remove arduino auto reset from a bare bones board go here, for other boards, ask the google.




One thing about this is that I didn’t really like the bright light shining at me, so I mounted it under my desk, with it pointing up towards the wall. Now it’s a subtle glow, and it’s much easier to ignore when I have a lot of work.




Summary The plist file should run your python script every 60 seconds. The ‘n’ or ‘m’ should be sent from the python script and received by the arduino. Then the arduino tells the led to turn on or off. I tested both statuses (statii?) by logging in and marking messages as read or unread until I was happy with the results.


This has been the first project where I have been able to send serial data in a useable way. I’m excited about moving on to a version that scrapes email info and posts it to an lcd screen, or maybe a project that sends data back to the computer to do something. There is also the distinct possibility that you could script the python carefully enough so that the arduino was not needed for a serial lcd version. a serial port would be the only necessity…

54 replies on “gmail notifier project (for dummies)”

  1. Thankyou thankyou thankyou! I’d been trying to figure this out for aaaages, but I was having a lot of problems with https requests not working with python, and an oddball character in my usb serial name, but now it works!

    I think it was a combination of straight downloading the scripts from you, then installing python2.5, and using the command “ls /dev/tty.* | pbcopy” on the terminal so I could just paste my usb serial into the script, weird character and all!

    Thanks again to you and j4mie for taking the time to write it up.

  2. Oh, and to elaborate on the terminal command, it just copies everything that ls throws up onto the clipboard so you can paste it somewhere else.

  3. I’m having a problem with the python script. When I drag the script into terminal with python running I get the error message “File “”, line 1″, “SyntaxError: invalid syntax”. I’m sure I’m doing something wrong but unfortunately I don’t know what it is. With the script in BBedit I am able to get it to work by clickin on the #! menu and selecting to run it in the Terminal. I’ve tried adding the line “#!/usr/bin/env python” to the first line but I’ve had no luck.

    Again when I run the script from within BBedit everything works. With the .plist file in the Library/LaunchAgents nothing happens so I have a problem with my script. Any ideas?

    Thanks for the great tutorials!

  4. Okay, I think I figured it out using Google. To get it to work I added the statement “#!/usr/bin/env python” to the first line of the script. I then made the file executable by typing “chmod +x check-gmail.py”. Now when I drag the script file to the terminal it runs.

  5. neat and clearly written tutorial. thanks! :-)

    for windows users, you can do something like this:

    //////begin code
    Set objShell = CreateObject(“Wscript.Shell”)
    do while 1
    objShell.Run “C:\Python26\pythonw.exe gmail.py”
    Wscript.Sleep 60000
    loop
    //////end code

    save it as a .vbs file, and it should run fine. the above is assuming python is installed at the above location, and the vbs script file is in the same folder as the python script.

  6. Hey everyone, I’m in need of a genius to help me complete this project. I cant believe how cool of a project this is, but being a COMPLETE noob, i have no clue where i went wrong. I followed everything to the best of my ability but im stuck!
    I have the arduino all hooked up and programmed, the .plist in place and the python file edited to my information. unfortunately ive never used python before and i have no idea how to run or install everything. Ive tried everything i can think of and googling anything just confuses me more. I have no idea what feedparser is or how to install it. PLEASE HELP! ASAP :)

  7. hey paul!
    are you using osx or windows? What problems are you having? I’ll gladly help if you tell me how you are struggling. did you try running the python file? if you are running windows, then johnty wrote how to run a python script. Good luck!

  8. Hello everyone,
    I’m having the same problem as PaulAndrewF. I’m using OSX, and although i have the arduino working well, the feedparser is killing me. I can’t figure out how to install it, I keep getting errors. Am I suppose to use the python shell to install the feedparser? I found this line on the internet to install…

    python setup.py install

    but it doesn’t work. There’s not much information about installing the feedparser out there. Hope you guys could help me out.
    Thanks

  9. In reference to IvanM and PaulAndrewF’s problem: in order to install feedparser you must type python setup.py install into terminal. However first you must ensure that terminal is being pointed to the feedparser folder, you do this by chnging directory in terminal – cd Documents/feedparser etc- then run the setup command.

    I’ve had my own problem that maybe someone can help with. This script works fine and talks to the arduino when i run the script manually in terminal. However when I try and run the plist with launchd, it tells me that there is no serial module! – ‘ImportError: No module named serial’ – I don’t get why this is happeneing, since the module is obviously installed as it works perfect manually. Any ideas?
    Thanks btw for this easy to follow tutorial, perfect for noobs like me:)

  10. I noticed your 22 ohm resistor, IO pins are only rated to 40mA, I presume you were powering the LED solely through the IO pin?
    That would via calculation suggest your LED has a forward voltage of 4.12V if you’re powering it from 5V, which sounds rather high for a while LED, I’d have expected around 3.5V. In summary, are you overloading your IO pin?

  11. Hi rFree!
    Yes, the 22ohm resistor was chosen with no regard to the io pin rating. It was definitely way over 40Ma. It seems like a safer resistor would be more than double it. 47 seems safe.
    Thanks for pointing that out – I’d hate to see someone lose a pin following in my footsteps!

  12. i’m having issues like Chris when he attempted this project back in January. I am getting an error message “File “”, line 1″, “SyntaxError: invalid syntax”. I tried running his idea for the solution but i don’t know where exactly he is putting it and how the rest of the line is suppose to look. I don’t understand what the problem is. can anyone help?

  13. Hi Bradley86,

    I didn’t need to do what Chris did, but as I understand it it is 2 steps.
    1) paste the line “#!/usr/bin/env python” without the quotes to the first line of your .py file.
    2) the terminal command “chmod +x check-gmail.py” needs some explaining. chmod will change the permissions on the file named “check-gmail.py” I think you will have to cd into the directory that the python file is in for this to work. On osx you can type “cd” into terminal, then drag the folder containing your python file into the terminal window, then hit return. Now you can run the line “chmod +x check-gmail.py” being sure that the filename matches your filename.

    If that doesn’t work, can you give more details about what OS you are running this on? Good luck!

  14. My laptop is currently running MAC OS X Snow Leopard. I been trying both methods to run the project from this site as well as from J4mie’s page from the link at the top. Using the methods from J4mie, I got the plist file to launch via terminal however the board is unresponsive. The terminal says the plist file is loaded. The board that I have is the Arduino Duemilanove . It has a reset button on it, I was reading that the reset button can sometimes affect the board?? I have all the files written the way it is suggested from both sites (made different modied copies)

    i appreciate the reply back that soon as well. (this project is standing between me and a college degree ~ literally)

    Thanks

  15. Ahhh…. you are trying to get the plist working? I never found a way to do that. I ended up with a workaround so that I ran my python script in a for loop.

    The other issue – yes the auto reset functionality can definitely mess you up! Have you seen this page? https://blog.tinyenormous.com/2008/11/08/removing-the-auto-reset-in-bare-bones-board-arduino/

    It isn’t for your brand of arduino, but it explains what I went through. Basically the default behavior of the arduino seems to be to reset whenever serial data is received. You might need to get a different arduino, or you might need to do some soldering or cut some jumpers.

    Good luck Bradley. You’re close!

  16. Well this is what i have for my code….

    Arduino:

    int outPin = 12; // Output connected to digital pin 12
    int mail = LOW; // Is there new mail?
    int val; // Value read from the serial port

    void setup()
    {
    pinMode(outPin, OUTPUT); // sets the digital pin as output
    Serial.begin(9600);
    Serial.flush();
    }

    void loop()
    {
    // Read from serial port
    if (Serial.available())
    {
    val = Serial.read();
    Serial.println(val);
    if (val == ‘M’) mail = HIGH;
    else if (val == ‘N’) mail = LOW;
    }

    // Set the status of the output pin
    digitalWrite(outPin, mail);
    }

    ———————————————————

    PLIST: (org.eliott.check-gmail.plist)

    Label
    org.eliott.check-gmail
    OnDemand

    ProgramArguments

    /usr/bin/python2.5
    /Users/Nugget/Desktop/CAPSTONE/MyGmailChecker/eliott_check-gmail.py

    StartInterval
    15

    ———————————————————

    PY code: (eliott_check-gmail.py)

    import serial, sys, feedparser
    #Settings – Change these to match your account details
    USERNAME=”elbradleyiv@gmail.com”
    PASSWORD=”**********”
    PROTO=”https://”
    SERVER=”mail.google.com”
    PATH=”/gmail/feed/atom”
    SERIALPORT = “/dev/tty.usbserial-A6008m0K” # 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()

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

    # Output data to serial port
    if newmails > 0:
    ser.write(“m”)
    print “some mail”
    else:
    ser.write(“n”)
    print “no mail”
    #print data to terminal

    # Close serial port
    ser.close()

    do u see any issues???

  17. Yep! – your python code is spitting out an M or an N, and the arduino is looking for an m or an n. In my (arduino) code I fixed it by using the dec codes for the characters. here’s one of the two, you can look in the zip file for the rest.
    if (val == 110) // n = 110 in dec

    good luck! you should also test somehow to se what the arduino is getting, and what python is sending.

    Lastly, you don’t generally want the arduino serial terminal open when you are running, because it can do buggy things.

  18. in the post above I forgot to clarify that M is not the same as m, so everything could very well be coded properly with the exception that m will never equal M and n will never equal N.

  19. this is a very dumb question.. how do u test what the python is sending and what the arduino is getting? i talked to one of my friends and that is what he suggested too. Been trying to look that up.

    but i fixed my arduino code to like how u have it however in your code you initialized mail to be low (mail == low) but in the set up you set it to high which as your comment says it starts with the light on. Well the light stays on like motel 6. no changes unless I tell it to start off mail==low in setup().

  20. To test the python you are going to comment out this code
    try:
    ser = serial.Serial(SERIALPORT, 9600)
    except serial.SerialException:
    print “no device connected – exiting”
    sys.exit()
    and wherever it says ser.write or ser.close.

    Then type “python ” into your terminal, drag in the python file, and hit return. It should print “no mail” or “some mail” depending on the status.

    to test your arduino, connect it and open up the serial window in arduino.app type in your n or m into the window and see if it lights anything up. also try M,N,110, and 109.

    Lastly, if you are still trying to use the plist I can’t help you. I think the easiest thing to do will be to make your python script loop. Do a search for that. It’s simple.

  21. Thanks, I will try to do that and get back to ya. Thanks for the help!

  22. sorry for all the request for help… i commented the statements out, ran python in terminal, but when I dragged the file into terminal i get a

    File “”, line 1 ….

    SyntaxError: invalid syntax

    the path of the py file is
    /Users/Nugget/Desktop/CAPSTONE/MyGmailChecker/test_check-gmail.py

    i tried what u suggested earlier by inputing #!/user/bin/env python at the beginning of the py file before the imports and then changing the directory to where the py file is kept, [enter], then typing
    chmod +test_check-gmail.py
    then it gives me a list of commands for chmod such as [-fhv] [-R …

    ——————————————————

    on the brighter side, testing the arduino works when inputing m or n.

    btw sorry if this is all bothersome.

  23. Bradley – I think your problem must be in your python script. if you’re getting that error when you run
    “python /Users/Nugget/Desktop/CAPSTONE/MyGmailChecker/test_check-gmail.py”
    then you need to do some work on your script. Did you follow all of the directions on this page? Did you install feedparser? I’m afraid I can’t give you much more help. This sounds like you need to dig in and poke around in python a bit. Good luck.

  24. One more thought bradley – I’m not sure if you got through the python “pre requisites” i.e. You need to install apple developer tools before you can install darwin ports before you can install pyserial. Then you also have to install feedparser. I think I will do a guide to this in the near future, because it isn’t all that intuitive.

  25. wait wait i’m lost now. so what exactly is all the prerequisites to getting thru the python setup?

  26. SO i have great news…. and one bad news….

    great news:

    so i was able to test the python connecting to my gmail account and it does work now. whenever i tell it to run the program to see if it has mail or not, python will print “some mail” or ” no mail”.

    also testing the arduino board was a success as well. when i type in “m” in the serial monitor the led will light up, when i type in “n” in the serial monitor the led will turn off.

    bad news:
    the bad news is that when i run the program it in terminal with everything uncommented, it will say no device connected.

    i have the serial port written write in the py code with the right baud, however i don’t understand why it won’t communicate and find the port.

    do u have any suggestions?

  27. ok so i got it to recognize the serial port. but its not sending the information to the board for it to light up.

  28. okay again i just figured it out. so now it talks to the arduino and lights up when i run the program via terminal.

    however i have some simple questions. do i have to have the arduino software/app to make the board take in the information when the python program works?

  29. ah i guess thats what you meant by don’t use the plist. so i am trying to figure out a nice way to run the python in a loop.

  30. Bradley – what I did to loop it was a simple for loop.

    for i in range(1, 5):
    //put your code here

    Change the 5 to the number of times you want it to loop. I did it this way because it’s an easy way to keep it from being an infinite loop. -> there definitely are better ways to do this, but this will work.

  31. Bradley –

    “do i have to have the arduino software/app to make the board take in the information when the python program works?”

    no. you don’t. It could even complicate matters.

  32. One last thing Bradley – You might want to make the loop conditional on the arduino being plugged in. instead of a ‘for’ loop that would be a ‘while’ loop. Google python loops for more info.

  33. I have just tried to download the zip file but it ain’t there :( Any chance of resorting the info please?

  34. Hi,

    Great tutorial, helped a lot!

    I got my Python working, and it shows if I have mail or not. But when I run the Arduino sketch, the light turns on right away, even if I dont have mail. Do you know why it’s doing that?

    Thanks!

  35. Hi there,

    I’m trying to make a product which prints off emails or facebook comments. I understand that this code checks how many emails I have but could it check what the emails says and send it as a string to arduino?

    Another thing that would be good is if it could send twitter feeds as a string to arduino to print comments?

    Any help would be greatly appreciated.

    Steve

  36. Hi Steve, you just need to look around at some of my other projects. I have one that displays tweets and one that displays the subject line and sender of emails. They both sound right up your alley.

  37. Hey there,

    thank you very much for this nice tutorial. However, I encounter problems trying to run this on Mac Snow Leopard and python 2.5 or 2.6. The error message I get is:

    Traceback (most recent call last):
    File “/Users/ianus/TEMP/Arduino_Gmail_Checker/check-gmail.py”, line 18, in
    newmails = int(feedparser.parse(PROTO + USERNAME + “:” + PASSWORD + “@” + SERVER + PATH)[“feed”][“fullcount”])
    File “/Library/Python/2.5/site-packages/feedparser.py”, line 317, in __getitem__
    return UserDict.__getitem__(self, realkey)
    KeyError: ‘fullcount’

    Any ideas how to fix this? Thanks for your time. Stefan

  38. I just figured out that the new Arduino Uno might be the problem. It no longer needs the FTDI drivers. Maybe that is the problem. Any ideas on how to solve it? Thanks again for your time. I would appreciate an answer to my mail address. Cheers, Stefan

  39. Now the script works. The problem was that as a german gmail user you need a different feed url to connect to. With the following options it works:

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

    Thanks again for this nice tutorial. Stefan

  40. It works great when run through the Python shell – the light turns on when I have mail. However, if I close the shell window, the light turns off. When I run the Python program by double-clicking it – the light turns on, but immediately turns off. Is there a way to make it stay on? Thanks.

Comments are closed.