Sunday, January 27, 2013

Raspberry Pi Resources

If you haven't heard of RebelMouse yet you should check it out. It's an interesting concept in organizing your online presence into a dynamic front page. It's also being widely used to "curate" pages around certain topics. Aggregating content for a particular topic on to one, consistent looking home page. I decided to give this a try and create a "site" that pulls in tutorials and projects involving the Raspberry Pi. Check it out at Let me know what you think.

Monday, January 21, 2013

Resistors: MacGyver Style

MacGyver Style?

I know there are quite a few international viewers to this blog and I don't know what kind of international penetration MacGyver had but if you don't know him you can read more here. In every episode he would "solve complex problems with everyday materials he finds at hand, along with his ever-present duct tape and Swiss Army knife." Basically, he was an extreme hacker.

DIY Resistors

With that in mind the thought occurred to me today; "What would MacGyver do if he needed a resistor?" Well, he'd make one of course. I did just that and so can you with just a few simple materials. You might have seen the demonstration before where you can create a variable resistor with a pencil and a piece of paper. Basically you color a thick pencil line on your paper and then use your multimeter to measure the resistance between two points along the thick-colored pencil line. The closer you put the probes the lower the resistance. If we're just looking to throw together a fixed resistor in a pinch we can do that too. All we need is:

  • A graphite pencil
  • Some hookup wire
  • Duct tape of course
Now that you have your supplies let's build this thing.
  1. First you'll need to remove some insulation off the ends of your wire. Use your wire strippers if you want but if you want the full MacGyver experience use your teeth.
  2. Rip a piece of duct tape so you're left with a piece about 1cm wide by 4cm long.
  3. Using your pencil, color on the sticky side of the piece of duct tape to create a thick line that will serve as the resistor material. Another way to do this is to color on a surface like paper or wood and then press the tape onto the surface and pull off the graphite. Be careful not to touch the graphite once you've penciled it on because you need every bit of it.
  4. Lay the wires on to the sticky side of the tape with the leads near each other but not touching and centered on the thick penciled line. Remember, the closer the leads are together the smaller the resistance.

  5. Fold the duct tape in half and firmly press it together.

  6. Measure the resistance and marvel at your MacGyver skills.

My Resistor Isn't Working

The most common problem is that you didn't get enough graphite to transfer onto the tape. You need to really lay down as thick a coat as you can. It's tough on the tape surface but doable. Another issue you might encounter is not putting the wire leads close enough together. I put mine about 2 millimeters apart and got a 20K resistor as seen above.

Resistor Quality

Now of course this DIY resistor is not going to have very tight tolerances and it's pretty difficult to get down to lower resistances unless you really step up your manufacturing process. Transferring graphite onto paper and measuring resistance directly on the paper I was able to see a pretty linear measurement of about 1K/mm. That trend held through about 3K and then it got less linear and the resistance started to increase less with distance. A 2cm distance between the leads on paper gave me about 11K. I tried paper, cardboard, and duct tape as substrates and found paper to provide the lowest resistances.


So that's one way to make a resistor. Give it a try and let me know how it turns out. If you make any breakthroughs that make it easier or give you better results I'd love to hear about it.

Sunday, January 6, 2013

Raspberry Pi - First Impressions

I got my first Raspberry Pi for Christmas and I just got around to playing with it yesterday. All I can say is that it is everything I dreamed it would be an more. They have managed to squeeze some serious awesomeness into a small form factor. These are just my first impressions in working with it.

Made for Kids?

So one of the big marketing items for the Pi is that it's great for kids to get started into programming. I think that myth has been debunked enough around the internet and the community is growing faster everyday that is deciding what it's really for. I'm sure younger kids who are computer savvy could get this up and running but here in the US I doubt it would be the go-to platform for introducing kids to programming.


The setup was very easy. I installed the Raspbian "Wheezy" image on a 4GB microSD card and was able to boot right up to it. Everything worked right out of the box for the most part. The only minor issue I had was that I didn't configure the keyboard on the first boot so some of my special characters were off. That was a quick fix. Other than that I was immediately able to get on the internet and tweet my success. Audio and video to my new 24" HDMI Asus monitor worked perfectly with no tweaking.


I spent the first 3 hours or so working directly on the Raspberry Pi. It is pretty responsive but having more than 1 or 2 things open at once and you'll notice painful latency compared to your zippy desktop/laptop/iPad. I installed the Chromium browser and opening a new tab took 5 seconds or more. It didn't take long to realize that the desktop environment on the Pi won't be replacing my normal machine anytime soon. That said, still very impressive for such a little package.

Node.js Anyone?

After a few hours of interacting with the desktop I realized it was time to give that up and SSH into my Pi from my Ubuntu machine. About 2 hours later I had a working version of Node.js running a simple "Hello from the Raspberry Pi" Express web server application. As a nerdy type it was really cool to build a node app on my Ubuntu machine, SCP it to the Raspberry Pi, start it, and then view the simple application from my iPad. 2013 is going to be so awesome!

Now What?

So now that I've spent about 6 to 8 hours testing the water I think I'm ready to get into some serious project hacking. I found the pi-gpio module for Node that should provide access to the GPIO pins on the Pi. Once I found that the ideas just started to flow into my mind. With Node running a web server app and pi-gpio giving me access to the I/O pins the possibilities are endless. I have a few simple ideas to get started and will do a post as soon as one of them works.

Thursday, January 3, 2013

Connect to MongoLab with Pymongo

If you've read my blog before you'll know that I'm a huge fan of Heroku. Recently I've been working on a project that is hosted with Heroku and uses the MongoLab add-on (also a huge fan of MongoDB). The project stores documents in the database and then displays them to the user. Because of the nature of the project I didn't want to create an authentication/authorization system to post new documents to the database from the web. Pushing new docs from a command line utility on my computer was good enough. Why not use mongoimport? Well, the input file to mongoimport has to be formatted so that each JSON object is contained on a single line. You can imagine how tedious it would be to format a complicated document onto a single line and readability is just terrible. What I wanted was to create a nice, readable .json file with a document in it for the database and then do something like
someutil --server --port 8000 -db test --user "username" --pw "password" --file widget.json
The utility would then parse the .json file and update my MongoLab db with the object in widget.json

Enter Python

I just recently started tinkering with Python and love it for it's simplicity so I decided to write up a quick app to post JSON documents to a MongoLab instance. All told it took me about an hour to figure things out and I want to share the code as it may be useful to somebody else trying to connect to a MongoLab database using Python. If you want to skip the explanation just scroll to the bottom for the full source code listing.
The first thing we need to do is import the necessary modules
import sys
import pymongo
from pymongo import MongoClient
import json
The pymongo driver is the officially supported MongoDB driver from 10gen and works nicely. We'll need the MongoClient member from that module as well as the sys module for reading the file and the json module for parsing it. I'll skip the dirty argument parsing code I wrote but have included it below for reference. We'll pretend that all of the arguments are hard-coded.
The first thing we need to do is create a connection to our database.
connection = MongoClient("", 37000)
db = connection["testdb"]
# MongoLab has user authentication
db.authenticate("username", "password")
I just made up the server and port values but you can get those straight from your MongoLab dashboard where it shows the connection URI. I also made up the database name testdb for this example but if you're using Heroku you can get the database name from your MongoLab dashboard as well.  Below is a sample from my MongoLab dashboard.
MongoLab Dashboard
Once you have a connection you can access your collections.
widgets = db.widgets
This will set the widgets variable to a collection in your testdb database called widgets. Now we're ready to read the .json file into a variable.
json_data = open("widget.json")
new_widget = json.load(json_data)
At this point you should have an object in new_widget that is ready to be loaded into the Mongo database. Now you could check first to see if one already exists with similar attributes and update it but for now we'll just create a brand new document each time and let the database assign it an _id
That's it! That's all of the python code required to read a .json file and create a new document in a MongoLab database collection. I used the following file to test my code.
  "Name": "My Widget",
  "Type": 3,
  "Price": 19.95,
  "Description": "A simple widget that does practically nothing",
  "Parts": [
    {"Name": "Screws", "Quantity": 100},
    {"Name": "Speaker", "Quantity": 1},
    {"Name": "Batteries", "Quantity": 2}
And here is the resulting document showing up in my MongoLab database.
MongoLab Document
Here is the full listing of the quick and dirty application I wrote to test the concept.

import sys
import pymongo
from pymongo import MongoClient
import json

numArgs = len(sys.argv)
args = {}
numDocsUpdated = 0
processingArg = False
currentArg = ""

for i, arg in enumerate(sys.argv):
  if i == 0:

  if processingArg == True:
    args[currentArg] = arg
    processingArg = False

  if arg.startswith("--"):
    currentArg = arg.lstrip('-')
    args[currentArg] = ""
    processingArg = True

connection = None
db = None
widgets = None

if (args.get("file") == None):
  print "You need to specify a JSON file to upload using the --file argument"

if (args.get("server") != None and args.get("db") != None and args.get("port") != None and args.get("user") != None and args.get("pw") != None):

  # Use the command line args
  connection = MongoClient(args.get("server"), int(args.get("port")))
  db = connection[args.get("db")]

  # MongoLab has authentication
  db.authenticate(args.get("user"), args.get("pw"))

  # Just connect to the local machine database
  connection = MongoClient()
  db = connection.test_db

widgets = db.widgets

json_data = open(args.get("file"))
widget = json.load(json_data)

# Add the widget to the database

print "Adding item to database"
numDocsUpdated = numDocsUpdated + 1;

print ""
print ""

print "DBLoader updated", numDocsUpdated, "documents in the database"
Hopefully these instructions can be useful to someone out there. If you found this helpful the please let me know about it. Likewise, if you have any questions about this I'd love to help you get it working in your environment.