Python and the Gmail API

The other day an idea popped in my head for a side project that would involve the Gmail API. Whenever this happens, I normally hit up the developer site, find a simple tutorial, and immediately get my hands dirty.

A quick search led me to this page. It’s called a “Quickstart: Run a Gmail App in Python.” Perfect. I’ll run this script and be on my way in no time. Well, not so fast.

This tutorial assumes I’m doing development on my local machine where it can pop open a web browser and authorize the API access. Most of the time I don’t operate that way – I use a remote Linux server of some kind.

To get this quick start guide working on a remote server:

  1. In the Google Developers Console, generate a client ID for a native application (this may work with other types of client IDs, but I know it works with a native one)
  2. Save the client_secret JSON to the script directory
  3. Add the ability to read command line arguments and call the function ‘tools.run_flow()’ instead of just ‘run’
  4. Run the script with “–noauth_local_webserver” as an argument

Now when you run the script, you’ll get a link to copy into any browser to authorize the access. Then, you’ll get an auth code to paste back into your script.

I’ve included both scripts so that you can compare easily. I hope this saves others from frustration and I also hope Google improves the tutorial.

quickstart_original.py
quickstart.py

Raspberry Pi + Garage door opener: Part 1

I want to be able to control my garage door from anywhere. Solution? Dedicated Raspberry Pi.

Pi with breakout board
Pi with breakout board attached to garage remote

 

How it was made:
  1. The garage door opener remote was removed from its casing and I located the button used to open the door. My particular model had three buttons.
  2. I soldered a jumper on the button leads to always create a circuit, removing the functionality of the button so it’s “always on” when power is applied. The button was this style – a standard electronics button.
  3. I removed the coin battery and soldered on two leads that will be powered by the Raspberry Pi. When the Raspberry Pi sends power, this will be no different than someone normally pressing the remote button.
  4. I wrote code to control the pin that outputs power.
Notes:
  • Garage door openers can vary, but I’m willing to bet a lot (if not most) use a CR2032 battery. The Pi puts out 3.3 volts, (.3 V higher), but I haven’t noticed any issues.

I’ve provided the simplest example here. The next step would be to have the code driven by a web interface that could be loaded on a phone or a web browser.

There will be an exciting part 2 to this article where I take this one step further!

GitHub: https://github.com/twstokes/garage-pi

Garage remote circuitry
Garage remote circuitry
Pi with breakout board
Pi with breakout board

 

2009 Mac Pro video card upgrade

After upgrading to Yosemite, I noticed that graphics performance was starting to lag a bit on my 2009 Mac Pro while driving two monitors at fairly low resolutions. At one point I had two GT 120s running separate monitors, but after one replacement and two failures, I was back down to one.

GT 120
GT 120

I decided to look into upgrade options and was happy to see that OS X now supports non-EFI video cards. The only catch: You won’t see anything displayed until OS X loads your drivers. Who cares?

I settled on an EVGA GeForce GTX 660 2GB, which Best Buy happened to sell. I can feel the judgement from the hardcore nerds for buying something at Best Buy. Nevertheless, this was much cheaper than trying to buy another GT 120 to replace my second one and offers WAY more power. I don’t game on my Mac – I just want the OS to feel snappy and fluid.

GeForce GTX 660
GeForce GTX 660

Like many powerful cards, the GTX 660 requires the PCIe 6 pin power connector from the power supply or motherboard. I’ve ordered this, but until it gets here, I’ve rigged up an external power source.

Steps to install:

  1. Attach one of these cables to your Mac Pro motherboard OR rig up an external PSU like I did
  2. Insert the new card and attach the other end of the 6 pin PCIe power cable
  3. Put the card retainer back in place, fasten everything down and reassemble
  4. If running an external PSU, make sure it is powered up before powering up the Mac Pro

Thoughts after installation:

  • Why didn’t I do this sooner
  • OS X is silky smooth
  • The fans will ramp up audibly if doing anything intensive in apps that utilize the GPU, but that’s OK
  • Everything just works – no drivers to install
  • My biggest bottleneck is now my disk – an SSD is nextMac About

Heaven Benchmark results:

Mode: 1680×1050 fullscreen on one monitor

Before:

  • FPS: 5.9
  • Score: 148
  • Min FPS: 3.3
  • Max FPS: 11.8

After:

  • FPS: 52.8
  • Score: 1330
  • Min FPS: 8.3
  • Max FPS: 103.3

New GTX 660
New GTX 660
IMG_0801
External PSU 6 pin power cable running through an open slot.
IMG_0802
External PSU

 

Adjusting a blank to provide room for the external power
Adjusting a blank to provide room for the external power cable

IMG_0805 IMG_0809 IMG_0810

Temporary setup
Temporary setup with external power supply. A PSU tester is connected to keep the supply running without being attached to a motherboard.
IMG_0862
Final 6-pin PCIe cable installed
IMG_0864
No more rig!

 

House analytics

My Dad and I built the alarm system for their house in late 2009. He handled hardware, I did software. From time to time I export the data it collects just for the heck of it, since it’s been running non-stop for years.

The back door at their house has been opened 41,267 since the end of November 2009. That’s kind of surprising to me.

It’s interesting to see some patterns in the graph – it’s how many times the door has been opened per month. You can definitely tell what year I bought my house. :)

Back Door Graph

Mounting partitions contained in a disk image in Linux

Say you have a disk image created using ‘dd’. You want to mount a partition contained on it but you realize it’s not as simple as just mounting the image and exploring the path.

The kpartx utility can help with that:

sudo kpartx -av disk.img

This should have created some partition mappings to /dev/mapper. Now, just mount the correct mapping the same way you would mount anything else. For instance:

sudo mount -t ntfs -o ro,noexec /dev/mapper/loop0p1 mounted

Drawing and connecting nodes with SVG

This is something I created over a year ago and I’ve decided to share the code in case someone finds it useful.

nodes

It’s nothing more than an example of using JavaScript to dynamically add connectable SVG objects.

Usage: 

  • Click a node and drag
  • Double-click to generate a node
  • Click a node, hold down shift, then release over another node to connect

Demo (Should be supported by most modern browsers)

Download ZIP