如何使用owfs读取iButton温度记录器?
我安装了owfs
,现在想从iButton温度记录器中读取数据。
owfs
可以让我把iButton当作一个文件系统来使用,这样我就能看到所有的数据。不过,我在找出最好的方法来访问这些数据时遇到了一些麻烦。我可以通过cat
命令来获取单个读数,比如说cat onewire/{deviceid}/log/temperature.1
,但是onewire/{deviceid}/log/temperature.ALL
这个文件是“坏掉了”(可能是因为文件太大了,因为histogram/temperature.ALL
可以正常工作)。
我写了一个Python脚本来读取所有文件,虽然能工作,但速度非常慢。有没有更好的方法?有没有人能提供一些例子?
我使用的是Ubuntu 8.04,但无法运行Java的“one wire viewer”应用。
更新:使用owpython
(和owfs一起安装的),我可以获取当前的温度,但不知道怎么访问记录的日志:
>>> import ow
>>> ow.init("u") # initialize USB
>>> ow.Sensor("/").sensorList()
[Sensor("/81.7FD921000000"), Sensor("/21.C4B912000000")]
>>> x = ow.Sensor("/21.C4B912000000")
>>> print x.type, x.temperature
DS1921 22
x.log
会出现AttributeError
错误。
3 个回答
2
2
我觉得没有什么聪明的方法。根据API文档,owpython并不支持这个功能。我想/proc
是你最安全的选择。也许可以看看owpython模块的源代码,看看能不能搞清楚它是怎么工作的。
3
我也遇到过使用owfs的问题。我觉得它对一个简单的问题提供了过于复杂的解决方案。现在我在用DigiTemp的代码,使用起来没有问题。我发现它既灵活又可靠。比如,我每分钟把房间的温度存储到一个日志文件里,方法是运行
/usr/local/bin/digitemp_DS9097U -c /usr/local/etc/digitemp.conf \
-q -t0 -n0 -d60 -l/var/log/temperature
为了做到这一点,我下载了源文件,解压缩后做了以下操作。
# Compile the hardware-specific command
make ds9097u
# Initialize the configuration file
./digitemp_DS9097U -s/dev/ttyS0 -i
# Run command to obtain temperature, and verify your setup
./digitemp_DS9097U -a
# Copy the configuration file to an accessible place
cp .digitemprc /usr/local/etc/digitemp.conf
我还手动编辑了我的配置文件,以适应我的设置。最后的结果是这样的。
TTY /dev/ttyS0
READ_TIME 1000
LOG_TYPE 1
LOG_FORMAT "%b %d %H:%M:%S Sensor %s C: %.2C F: %.2F"
CNT_FORMAT "%b %d %H:%M:%S Sensor %s #%n %C"
HUM_FORMAT "%b %d %H:%M:%S Sensor %s C: %.2C F: %.2F H: %h%%"
SENSORS 1
ROM 0 0x10 0xD3 0x5B 0x07 0x00 0x00 0x00 0x05
在我的情况下,我还创建了一个/etc/init.d/digitemp文件,并设置它在启动时运行。
#! /bin/sh
#
# System startup script for the temperature monitoring daemon
#
### BEGIN INIT INFO
# Provides: digitemp
# Required-Start:
# Should-Start:
# Required-Stop:
# Should-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 6
# Description: Start the temperature monitoring daemon
### END INIT INFO
DIGITEMP=/usr/local/bin/digitemp_DS9097U
test -x $DIGITEMP || exit 5
DIGITEMP_CONFIG=/root/digitemp.conf
test -f $DIGITEMP_CONFIG || exit 6
DIGITEMP_LOGFILE=/var/log/temperature
# Source SuSE config
. /etc/rc.status
rc_reset
case "$1" in
start)
echo -n "Starting temperature monitoring daemon"
startproc $DIGITEMP -c $DIGITEMP_CONFIG -q -t0 -n0 -d60 -l$DIGITEMP_LOGFILE
rc_status -v
;;
stop)
echo -n "Shutting down temperature monitoring daemon"
killproc -TERM $DIGITEMP
rc_status -v
;;
try-restart)
$0 status >/dev/null && $0 restart
rc_status
;;
restart)
$0 stop
$0 start
rc_status
;;
force-reload)
$0 try-restart
rc_status
;;
reload)
$0 try-restart
rc_status
;;
status)
echo -n "Checking for temperature monitoring service"
checkproc $DIGITEMP
rc_status -v
;;
*)
echo "Usage: $0 {start|stop|status|try-restart|restart|force-reload|reload}"
exit 1
;;
esac
rc_exit