char1es.net

Time well spent?

I just got to Macau this evening.   So very good to be back here.  I left Cambodia yesterday.   I really enjoyed my time there.   So drastically different from Bangladesh, but in some ways very similar.  I put some photos up on flickr.  Probably the most notable event though was that I have a neice now, which makes me an Uncle.

I spent another day in Kaula Lumpur, however that was mostly a non event.  I stayed out at LCCT and while I did go in to the town center, I didn’t see much.  This morning I camped out in a Starbucks taking advantage of their free wifi.  I’ve been thinking about a redesign of the old blog for a while now, something a little more minimalist and this seemed like a good way to use a slow morning.  I’ve been reading Dive into mark and I’m liking a lot of his thoughts, especially things like Simplicity is hard lets go shopping.  So I started hacking this morning and by check in time I had something that looks like his site, maybe too much, even if it is the MIT license.   I’m not sure how important blog design is these days, I mean I use RSS feeds for most of my friends blogs.  It’s fun to have something that looks good though, or at least that I think looks good.  If it doesn’t look good, well, stop using Microsoft explorer, or let me know.

twitterd in Python

So a while ago I decided that twitter is the new finger albeit more palitable for the masses and wrote twitterd in perl. I’ve been using this to update my twitter and facebook status ever since. A couple of months ago I decided to rewrite it in Python. Next stop C?

#!/usr/bin/python
"""
twitterd
Author: Nathan Charles <ncharles at gmail dot com>
Version: 0.1
This program twits the contents of a file when it's time stamp is updated.  
The functionality is similar to that of finger/.plan in days of yore.
Requires:
   pytwitter
   configobj
This program has no warranty to the full extent of the law
"""

import pytwitter
from sys import exit, argv
from os import path
from time import sleep

POLLINT = 60
READLEN = 140

def setEnv():
    from configobj import ConfigObj
    from os import environ
    import getopt
    env = {}
    HOME = environ.get("HOME")
    config = ConfigObj(HOME + '/.twitrc')
    env["watchfile"] = HOME + '/.plan'
    try:
        env["username"] = config['username']
        env["password"] = config['password']
    except:
        #.twitrc doesn't exist not nessarily a problem
        pass

    try:
        env["optlist"], args = getopt.getopt(argv[1:], 'dfh',["username=","password="])
    except getopt.GetoptError:
        usage()
        print "called exception"
        exit(1)
    for o,a in env["optlist"]:
        if o == "--username":
            env["username"]=a
        if o == "--password":
            env["password"]=a

    if not "username" in env:
        print "username is not set"
        exit(1)
    if not "password" in env:
        print "password is not set"
        exit(1)
    return env

def watch(username, password, watchfile):
    """Updates a twitter feed with the contents of a file when the timestamp is updated.
    """
    try:
        timeStampInitial = path.getmtime(watchfile)
    except:
        #file doesn't exist setting to 0 because it might exist in the future
        timeStampInitial = 0
    while 1:
        try:
            timeStampIncrimented = path.getmtime(watchfile)
        except:
            #file still doesn't exist
            timeStampIncrimented = 0
        if (timeStampInitial==timeStampIncrimented):
            sleep(POLLINT)
        else:
            timeStampInitial = timeStampIncrimented
            #update status
            statusTxt = file(watchfile).read(READLEN)
            client = pytwitter.pytwitter(username=username, password=password)
            rc = client.statuses_update(status=statusTxt)
            print rc

def usage():
    """Prints when called with no arguments or with invalid arguments
    """
    print """usage: twitterd [options]
   -d    daemonize
   -f    run in forground
   -h    help

   twitterd must have twitter username/password set. this can be set via .twitrc

   --password    set username
   --username   set password  
    """

if __name__ == "__main__":
    g = setEnv()

    if g["optlist"]:
        for o,a in g["optlist"]:
            if o == '-h':
                usage()
                exit(1)
            if o == '-d':
                #run in background
                import daemon
                daemon.daemonize()
            if o == '-f':
                #run in forground
                print "Running in Foreground"
    else:
        usage()
        exit(1)

    watch(g["username"], g["password"], g["watchfile"])

Low cost data logging with Arduino

I’ve been wanting to play around with an Arduino for a while, so I bought a Duemilanove without a set goal in mind. Recently I’ve been working with Solar thermal and I needed to measure several temperatures simultaneously so decided this would be as good a project as any. I bought some “4700 ohm” thermistors and hooked them up with some other 4700 ohm resistors in a voltage divider configuration. The output of which is hooked up to the analog inputs on the Arduino. I found a Thermistor Example in the Arduino playground which I had to modify somewhat to make work with my configuration. I was confused for about a day as to why my thermistor equation T=\frac{\beta }{\ln \left ( \frac{R_{T}}{R_{25C}}\right )+\frac{\beta}{298.15}}-273.15 was giving me garbage. After doubting my ablity to do math pretty severely I finally figured out that my math was indeed correct and as it turns out the “4700 ohm” thermistor package was incorrectly labeled and were actually only 470 ohm. Anyway, I modified the code so it would work with my thermistors and pass multiple temperatures back across the serial line.

Arduino Thermistor Sketch

Then I wrote some quick and dirty python code to read the values coming off the serial line and write it to a csv file which I could open with OpenOffice.

import serial
import time
ser = serial.Serial('/dev/tty.usbserial-A6008dxP', 9600, timeout=1)
logfile = open('test.csv', 'a')

while 1:   # read a '\n' terminated line 
   line = ser.readline()   # read a '\n' terminated line
   if not line:
      break
   words = line.split()
   now = time.strftime("%d/%m/%Y %H:%M:%S", time.localtime())
   a =  "%s, %s" % (now, line)
   if line.find(',') != -1:
        logfile.write(a)
        logfile.flush()

logfile.close()
ser.close()

The resolution isn’t that great because the inputs are only a 10bit ADCs but it’s more than enough to get trends. Without a more accurate thermometer, I can’t tell for certain how accurate it is, but as far as I can tell it reads faster and more accurately than the mercury thermometer we have. In university I would have done this with a fairly expensive LabVIEW system. The impressive thing is that I can get good enough information for what I need with Python and less than $30US in parts including the Arduino.