I’ve had a busy past couple of weeks, speaking at PechaKucha, teaching a class on Arduinos and Wireless Communications, and finding a new job. I’ve been so busy lately I have to schedule my fun, which is a little sad but apparently necessary! This week I dove back into the Raspberry Pi.
I got my RPi a few weeks ago from Adafruit, set it up, got it connected and installed some software but then forgot about it. I have a short term plan, which is:
I want a Raspberry Pi camcorder. What I envision is a system that is a RPi connected to a webcam. I flip a power switch and a red light comes on, indicating power and readiness. I then hit a candy-like button and the red light toggles to green, and the webcam starts recording. I hit the button again, and the green light toggles to red, and the Pi stops recording, as well as uploading the new video to a central repository (dropbox, fileserver, youtube, etc).
The first step in this project, aside from a little planning, is figuring out how the GPIO pins on the RPi work. As mentioned before, the RPi has a series of pins that allow you to logically control external devices, components, etc. I’ll need to connect these to components on my breadboard for the power switch, blinkenlights, etc.
Adafruit has a nice tutorial section for learning about the RPi, and one of the first tutorials for GPIO is an email checker. It connects to the ‘net, grabs a count of your emails from gmail, and then either lights a red LED (no mail), or a green LED (mail!). I thought this one was great because it includes some pieces of what I want to accomplish, namely LED and internet communcations. My learning process can take me to some interesting but unexpected places, so here’s the story of what happened!
The first section of the tutorial has you install some necessary libraries. I already had SSH set up on my RPi, because its a PITA to hook up an external keyboard and mouse to the Pi and move stuff around on my desk to make it work. VNC would work too, I guess, but I digress. I also had to make sure Python was installed and up to date, as well as the python RPi GPIO libraries. Everything progressed nicely and there were no problems.
The second part has you connect up the hardware. The Adafruit RPi starter pack comes with (almost) everything you need, including the Pi, a ribbon cable, a breadboard, and a breakout board so you can easily split out the GPIO pins onto your breadboard. I quickly connected everything up, but in fairness I have SOME small degree of electronics knowledge. Let me know if you need some pointers
One cool thing: I can now (sort of) read resistor codes without having to look them up! I needed 2 1kOhm resistors in series with the LEDs to limit the current.
The third section is the coding section. It suggest you copy / paste from the site, but you know… I like doing things the hard way, so I typed everything out myself. No problems here. The problem came when I tried to run my code! I am no python expert, and the errors can be … cryptic, to say the least. This is what I got when I ran my code:
root@raspberrypi:/home/pi/tutorials# ./rpi-gmail.py
./rpi-gmail.py:17: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(GREEN_LED, GPIO.OUT)
./rpi-gmail.py:18: RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(RED_LED, GPIO.OUT)
Traceback (most recent call last):
File “./rpi-gmail.py”, line 21, in <module>
newmails = int(feedparser.parse(“https://” + USERNAME + “:” + PASSWORD + “@mail.google.com/gmail/feed/atom”)['feed']['fullcount'])
File “/usr/local/lib/python2.7/dist-packages/feedparser-5.1.3-py2.7.egg/feedparser.py”, line 375, in __getitem__
return dict.__getitem__(self, key)
KeyError: ‘fullcount’
I ask you, dear readers: What the hell does this mean? I surely did not know, so I had to go out and figure it out.
The warnings I wasn’t overly concerned about, warnings are usually just that, so I started looking towards the bottom. There was some indication of the error, KeyError ‘fullcount’. The offending line was trying to decode a URL, using ‘feed’ and ‘fullcount’. So I had to go off and learn about the feedparser code, and about the atom specification!
The feedparser code splits off the returned code into a dictionary, which is sort of like a key / value link in python (see here). Problem is, I’m no python wiz! And it looked to me like these were two dimensional arrays from C. Furthermore, I went off and looked at the Atom spec, which is a lovely IETF specification. None of these things told me what fullcount was, so I decided to do the usual boneheaded thing, and figure out myself what was going on.
I added a print statement in the python code to see what was being returned by the URL call. And what came back looked something like this:
{‘feed’: {‘summary’: u’<h1>Unauthorized</h1>\n<h2>Error 401</h2>’}, ‘status’: 301, ‘version’: u”, ‘encoding’: u’UTF-8′, ‘bozo’: 1, ‘headers’: {‘content-length’: ’147′, ‘x-xss-protection’: ’1; mode=block’, ‘x-content-type-options’: ‘nosniff’, ‘expires’: ‘Fri, 03 May 2013 13:36:04 GMT’, ‘server’: ‘GSE’, ‘connection’: ‘close’, ‘cache-control’: ‘private, max-age=0′, ‘date’: ‘Fri, 03 May 2013 13:36:04 GMT’, ‘x-frame-options’: ‘SAMEORIGIN’, ‘content-type’: ‘text/html; charset=UTF-8′, ‘www-authenticate’: ‘BASIC realm=”New mail feed”‘}, ‘href’: u’https://mail.google.com/mail/feed/atom’, ‘namespaces’: {}, ‘entries’: [], ‘bozo_exception’: NonXMLContentType(‘text/html; charset=UTF-8 is not an XML media type’,)}
I ask you again dear readers: what the hell is this? If you recognize it, you are a wizard and I am not worthy! But I am dumb, so I dug further. Strange, fullcount does not appear in the text above! Interesting. Hmm… what happens if I try this URL in a browser window? Ohhhh… it’s like an XML file! And the feedcount is a field in the feed description, so it’s just looking to pull the feedcount tag out and store it in a variable. But wait… 2 problems. One, the browser URL is asking me for my password again. And the returned fields do not match the ones that I’m getting returned in my print statement. Is it a cookie thing? What’s …
Oh, duh. I’m using the wrong password in my script file! I made a simple change, and voila! BLINKENLIGHTS!
Once it was debugged, I made one final modification, a simple loop that blinks the indicator light a number of times equal to your email count.
This debugging seems a lot easier in typing it up, than it took when I was messing with it. It took over an hour to set up, with about an hour of that in debugging. However, I learned a lot of neat stuff!
- Package management for RPis
- Atom specification, what it is and how it works
- How gmail provides an API to pull your emails (useful!)
- Python dictionaries
- Python module storage and installation
“Well, after this I should think nothing of falling down stairs.” – Alice. In summary, when you fall down the rabbit hole, you never know exactly where you are going to end up, but it’s probably a strange and fantastic place!






























