char1es.net

solpy

Something I’ve been working on for while is a program called solpy. solpy is is a python library to model solar system power performance similar to PVWatts or NREL’s System Advisor Model(SAM). This is primarily a research and analysis tool and there is no guarantee on the calculations. I initially started writing this while working in Bangladesh as a fairly crude calculator to go from a fisheye panorama to a csv of vectors for shading calculations, however there were several pieces that were added to make it a bit more useful. Daniel Thomas did work adding the Tang evacuated glass tube model. I cherry picked some pieces of Brandon Stafford’s pysolar for solar positioning although made some changes for my purposes. There is also a simple module for reading TMY3 data. This tool is rudimentary, but functional. It integrates with ipython for some command line functionality.

ipython example

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.