Friday, February 29, 2008

Will asked about the code I was using in my ambient email notifier. The full code is a bit difficult to figure out because I've got it tied to a system tray icon thingi, which can go in another post another day, but here are some relevant bits.

First, you can get the number of emails with a particular label in gmail with the following Python code:

This means the python script has control over the colour and you can test that it's working by simply opening up a terminal on COM3 and typing away (A is 01000001 in ASCII, meaning pin 1 is switched on, B is 01000010 so pin 2 is on, etc.)

17 comments:

thanks i will try to start this project in a week or 2 i am interested in making a little mailbox to mount onto of my flat screen monitor when there is mail the little arm swings up when ii have mail and after i have read the mail it flips down again im sure this will require harder coding but im sure one of my teachers could help me if im stuck could you possibly help me?

It will display the build status of our subversion repo in the end but for now it's responding to a python dbus script which watches dbus for events from Pidgin and flashes an LED red for a new IM from an employee or green for an IM from anyone else :)

I'd like to build something like this myself, but I'm not familiar with python at all..

Nerval (or anyone), can I ask you to help me a bit with this maybe? The circuitry and picaxe progging should be no problem for me but I don't know where to start for a python script. I reckon Tom has posted some code snippets here on this blog for use as a gmail notifier with windows, but I'd rather like to have the pidgin notifier you mention. I further plan to build some nice email notification LED into my eeepc netbook at a later time, but first need to get things working with some kind of prototype before I open up my eeepc (obvious).

I'd even try to learn python language for this, but currently my free time is spare due to having to mess with Softgrid and SQL server at work so learning a language would take forever until I'd arrive at something useful I'm afraid...

If anyone can help me w/coding or preferably supply me with a working standalone python script to check for mail annd send data via serial port I'd be very grateful.

I promise I'll build & donate a free email LED notifier (built in SMD form factor on tiny custom PCB) to the person helping me with this project.

Hi,Thanks for your quick reply. So if I just copy & paste the three given code blocks the script will work? I ask since you mention calling some kind of function and sending serial data several times, I bet additional code is needed for this?

The USB virtual serial port, pySerial and linux specific details should be no problem, just the python script is a miracle to me.

To clarify, the code in the post is cut down from the real script as the other bits are/were particular to my situation and not going to help anyone in general. I.e. it's setup for my GMail labels and did have stuff in there for the Windows system tray.

To answer you question, cutting and pasting won't work as is, you'll need to "glue" it together into something that suits your circumstances. And the multiple calls I refered to are to GMail to get unread message counts in multiple labels, there is only one call to update the serial port.

Now I've installed python-feedparser and pyserial, slightly modified the gmail feed URL line to fit my locale needs, changed the virtual serial port to the one I'm using ("/dev/ttyUSB3") and and replaced the "username" and "password" in the "msgCount" call with my own ones.

I've no idea how to monitor the serial port in Linux, but I'm guessing you can redirect it or open a terminal to it or something...

The notifier is essentially a hardware serial port "sniffer", but it has no buffering and shows only the lower 3 bits of the byte transmitted.

I did all the development of this in Windows, and tested that the "receiver" side was working by sending data to the email notifier with HyperTerminal.

Sending a single byte was never reliable, so that's why the loop is there to just send it 10 times. I'm sure there's a better way, changing buffer sizes or something like that, but it worked fine like that as there is only one byte in each message.

First, not sure why you're getting a count of 0, when you have 3 unread messages. Occassionally I have noticed I have email but the feed is empty, so maybe that's happening in this case for you too. It's never lasted long enough to force me to debug it.

Second, there's a bug in that code I gave you, the easiest fix is to change the last line of the msgCount function to just:

return len(inbox["entries"])

Sorry about that, some stuff still left around from when I had the system tray notifier going too.

hm, changing msgCount to the simpler variant didn't do it. I get back "message count: 0" when trying to run the script.

when I open "https://username:password@mail.google.com/mail/feed/atom/" with firefox, the feed is there, now with four unread messages. If I append something like "/inbox" to above url I still see the feed page but with no messages.

I suspect that in my case, feedparser browses either wrong or nonexistent email folders to retrieve its "inbox" value, and thus contains this "0" value..

The sourcecode of my rss mail feed has a tag "fullcount" that is incremented with each new incoming mail so it should be possible for feedparser to read this tag?

Got it to work now, but only by burning another feed containing the gmail feed with "feedburner". This new feed now can be read by "feedparser.parse" without problems, even SSH authorization is no problem (luckily).It seems that my original "gmail" atom feed is sub-standard..

Now I'll try to set up email forwarding and build a circuit to test the com.write part

Do you have a paypal acc Tom? Then I'd like to pay you a beer or something.

Good to hear you got the RSS stuff straightened out. Good luck with the circuit, that's the fun bit :)

As for the beer money, I appreciate the thought but don't worry about it, it is my pleasure to help out. If you insist on spending some money, find a charity or open source project, they'll have a better use for it than me.