Tuesday, June 2, 2009

OWFS, GLPK, and Python

OWFS: Two weeks ago I installed OWFS and got it working with the sensors from the command line. This week I experimented with the python bindings to OWFS. Via Python I could easily connect to the sensors and read the temperature. What seemed difficult via the Python bindings was changing the temperature scale from the default of Celsius to common weather temperature of Fahrenheit. Via the command line this was an easy task, via Python binding it was a perplexing situation. After reading the documentation it appears the Python binding is not full featured enough to do this. So I figured I could write a simple Python line to issue the command line setting to complete this task. What prevent this was the binding to OWFS did not leave an obvious path to make this change and did not provide a command to find the path. So my conclusion was to write a Python script where half of it issues commands to the OS as if it were a shell script and then pull the data into Python logging and graphing. Below are some examples of what I needed I needed to do.

First I have to mount the file system for the controlling device. Since this is a USB device I need to do this as root. For now I just run my IDE Eric4 via gksudo until I can think of a better way
os.system('/opt/owfs/bin/owfs -u /var/1-wire/mnt')
So next I enumerate the interfaces on the wire
dirlist = os.popen('ls /var/1-wire/mnt/alarm/').read()
print "these are the interfaces"
print dirlist
PreIfaces = dirlist.split('\n')
IfaceLst = []
for listing in PreIfaces:
if re.match('10\.\w', listing):
print listing, "is a single interface"
listing = str(listing)
IfaceLst.append(listing)
So here is where I change the temperature scale and then see if my change took place

os.system('echo "F" > /var/1-wire/mnt/settings
/units/temperature_scale')
TempScale = os.system('cat /var/1-wire/mnt
/settings/units/temperature_scale && echo')
print TempScale
The rest of my code is learning the best (or most Pythonic) way to pass the list to the function os.popen so I can programmaticly get a temperature on all three interfaces without having to name each one. I think I will also just log the data to three text files using the CSV module for this current version. Then for version 2 of the script I may move logging to SQLite. I have the book
Beginning Python Visualization for guidance with making graphs in Python.

GLPK: So on the Ubuntu Linux machine I got PyMathProg installed. In my past post I stated I had the PyMathProg installed but that was the earlier version (since it was an Ubuntu package), that does not work with the examples on the current PyMathProg site: http://pymprog.sourceforge.net/. So tonight I uninstalled the Ubuntu package version and installed the version from the tarball I downloaded. Then I ran the basic PyMathProg examples and it seems to work now. So give another cheer for progress.

No comments:

Post a Comment