我有一个温度和压力计,我想用它来跟踪温度随时间的变化。由于随着时间的推移我可能会使用多个传感器,因此我希望能够将我的 BMP085 传感器引用为tp
. 换句话说,我想调用tp.temp
或tp.pressure
获取当前温度等。问题是每次调用时都不会更新tp.temp
。.pressure
建议?
#!/usr/bin/env python
#temperature logger for the BMP085 Temperature and Pressure Sensor on the Raspberry Pi
from Adafruit_BMP085 import BMP085
from time import sleep
import pickle, sys, os
class tps():
def __init__(self):
#temperature/pressure sensor setup
self.bmp = BMP085(0x77)
self.temp = self.bmp.readTemperature()*1.8+32
self.pressure = self.bmp.readPressure()*0.0002953
class data():
def __init__(self):
self.tp = tps()
self.savedata()
def savedata(self):
# if os.path.exists("data.dat")==True:
# if os.path.isfile("data.dat")==True:
# fileHandle = open ( 'data.dat' )
# olddata = pickle.load ( fileHandle )
# fileHandle.close()
print self.tp.temp, self.tp.pressure
sleep(4)
print self.tp.temp, self.tp.pressure
# newdata = [self.tp.temp, self.tp.pressure]
# self.datadump = [olddata]
# self.datadump.append(newdata)
# fileHandle = open ( 'data.dat', 'w' )
# pickle.dump ( self.datadump, fileHandle )
# fileHandle.close()
data()